lundi 29 août 2016

Service Pattern - eliminating code repetition

Generally, all of the services in my service layer follow the same pattern:

public ApiResponseDto DoStuff (int parameter){
   var response = new ApiResponseDto();
   try{
       using (var db = new DbConext()){
           // do stuff
           // throw descriptive exceptions when needed
           return response;
       }
   }
   catch (Exception e) {
       ThrowError(e)
       response.Success = false; // etc
       return response;
   }
}

What I'm wondering is - is there a pattern that I can use to eliminate this code repetition? This same footprint is used ~200 times throughout the app I'm working on and it seems proper to try and reduce the repetition somehow.

Aucun commentaire:

Enregistrer un commentaire