Returning a result object can improve the flow of your business classes

It's not that handy to alway's throw an error if something unwanted happens in your business or data classes. Sometimes you just want to show a friendly message to the user and go on or maybe you have some actions to perform after each other and you want to check the result of the previous actions. If you think about it, maybe this happens on more places than you think, well in these cases you can use a resultmessage class with some build in logic to log the error to disk etc.

    public struct ReturnMessageBase
    {

        // If the result is succeeded or not
        public bool Succeeded { get; set; }

        // the friendly message of the result
        public string Message { get; set; }

        // if there  is an exception in the result
        public Exception Exception { get; set; }


    }

    ///
    /// 
    ///
    public class ReturnMessage
    {
        // privates
        private List returnMessages = new List();

        // properties
        public bool Succeeded { 
            get
            {
                return returnMessages.Where(a => a.Succeeded == false).Count() == 0;
            } 
        }

        public string Message { 
            get 
            {
                var teller = 0;
                string result = string.Empty;
                returnMessages.ForEach(  a =>{
                    if (!string.IsNullOrEmpty(a.Message))
                    {
                        result += "Bericht " + teller.ToString() + ": " + a.Message + Environment.NewLine;
                    }
                    teller++;
                });

                return result;
            } 
        }

        public IEnumerable Exceptions {
            get
            {
                var allExceptions = new List();
                returnMessages.ForEach( item =>
                {
                    if (item.Exception != null)
                    {
                        allExceptions.Add(item.Exception);
                    }
                });
                return allExceptions.AsEnumerable();
            }
        }

        // methodes
        
        /// Adds a message to the collections of 
        /// returnmessages, logs the error to disk
        ///
        ///If the action is succeeded
        ///The action message
        ///The action exception
        ///If the error has to be logged to disk
        public void AddReturnMessage(bool succeeded, string message = "", Exception exception = null, bool log = false)
        {
            returnMessages.Add(new ReturnMessageBase()
            {
                Succeeded = succeeded,
                Message = message,
                Exception = exception
            });
            if (log)
            {
                LogError(exception);
            }
        }

        ///
        /// Logs the error to disk
        ///
        ///
        private void LogError(Exception exception)
        {
           // your log implementation
        }
    }

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!