How Do You Communicate Service Layer Messages/Errors to Higher Layers Using MVP?

后端 未结 3 1525
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 01:17

I\'m currently writing an ASP.Net app from the UI down. I\'m implementing an MVP architecture because I\'m sick of Winforms and wanted something that had a better separation

3条回答
  •  盖世英雄少女心
    2020-12-13 02:17

    As Cheekysoft suggests, I would tend to move all major exceptions into an ExceptionHandler and let those exceptions bubble up. The ExceptionHandler would render the appropriate view for the type of exception.

    Any validation exceptions however should be handled in the view but typically this logic is common to many parts of your application. So I like to have a helper like this

    public static class Try {
        public static List This( Action action ) {
          var errors = new List();
          try {
            action();
          }
          catch ( SpecificException e ) {
            errors.Add( "Something went 'orribly wrong" );
          }
          catch ( ... )
          // ...
         return errors;
        }
    }
    

    Then when calling your service just do the following

    var errors = Try.This( () => {
      // call your service here
      tasks.CreateMember( ... );
    } );
    

    Then in errors is empty, you're good to go.

    You can take this further and extend it with custome exception handlers which handle uncommon exceptions.

提交回复
热议问题