c# - Single responsibility principle in MVC -


i have mvc project following pattern

view <-> controller <-> service <-> repository/entities <-> database

for example, if have 2 tables (customer , order) in database, have 2 classes in repository layer (this class map 1:1 database table because i'm using ef code first) :

public class customer {      [key]      public int customerid { get; set; }      public int name { get; set; }      //rest of columns here }  public class order {      [key]      public int orderid { get; set; }      //rest of columns here } 

then have services :

public class customerservice : icustomerservice {      void addnewcustomer(customer obj);      void getcustomerorders(customer obj);      //rest of methods here }  public class orderservice : iorderservice {      void getorderbyid(int id);      void getcustomerorders(customer obj);      //rest of methods here } 

you notice have getcustomerorders.

my question :

  1. without breaking single responsibility principle rule, put getcustomerorders? in customerservice, orderservice, or both?

  2. did break single responsibility principle rule having more 1 service in controller? example :

     public class transactioncontroller : controller  {       //more 1 service inside class       private icustomerservice _customerservice;       private iorderservice _orderservice;        public projectcontroller()       {           this._customerservice = new customerservice();           this._orderservice = new orderservice();       }        public projectcontroller(customerservice customerservice, orderservice orderservice)       {           this._customerservice = customerservice;           this._orderservice = orderservice;       }        public actionresult index()       {            return view();       }        public actionresult createcustomer()       {            //rest of code here       }        public actionresult createorder()       {            //rest of code here       }  } 
  3. i have bunch of controller bloated action method, example productcontroller have :

     index  add  edit  delete  priority  addpriority  editpriority  deletepriority 

    if controller split

     productcontroller        index        add        edit        delete  productprioritycontroller        index        add        edit        delete 

    i see template project microsoft doesn't have more 1 crud operation inside controller (towards bottom example). bad design if have more 1 crud operation inside controller (top example)? thinking of splitting controller don't want bite ass later having maintain 50 controllers.

any appreciated , apologize bad english.

  1. i put in customerservice because depends on customer pass function.
  2. i think maximum services controller ~3/4 services in 1 controller. in case think good.
  3. the controller doesn't need implement business logic. should take data , post them right place. think should create manager / service / class handle business logic. crud operations, should in 1 controller (get/post , etc).

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -