design patterns - Re-factoring Web API controllers using Interfaces and/or Dependency Injection -


i have bunch of web api controllers example:

  public class productscontroller : apicontroller     {        private readonly context db = new context();         public product getproductbyid(int id)         {             var product = db.products.firstordefault((p) => p.id == id);             var category = db.category.firstordefault((p) => p.id == id);             if (product == null)             {                 throw new httpresponseexception(httpstatuscode.notfound);             }             if (category == somecategory)             {                 throw new httpresponseexception(httpstatuscode.badrequest,new httperror( "custom message"));             }             if (itsrainingtoday == someotherspaghetticode)             {                 throw new httpresponseexception(httpstatuscode.badrequest, new httperror("some other mess"));             }            //and on , forth             return product;         }     } 

as can see, big ugly mess, esp. if have hundreds of rules , dozens of controllers. efficient approach factor out these "business rules" can applied multiple controllers overrides? looking design pattern , example based on generic code.

i aware of unit of work, unity, , other methodologies. need guidance path follow.

take @ specification pattern. place 1 rule in each specification, such notnullproductrule, , chain needed specifications in controller.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -