I understand MVC is all about putting things in the correct place and the logic where it should be. My controller actions are getting filled up with business logic (not related to data storage) and I feel that I should start moving some of the logic to a different place.
Is there a convention for where I should place this logic? For example I have the following controller that’s located in the controllers file:
adminPowerController
public ActionResult Create(string test1)
// business logic
// business logic
// business logic
return View();
}
public ActionResult Index(string test1)
// business logic
// business logic
// business logic
return View();
}
The recommended place to put business logic is into a service layer. So you could define an interface which will represent the business operation:
and then have an implementation of this service. Finally the controller will use it:
and configure your DI framework to pass the proper implementation of the service into the controller.
Remark: In the example I provided I used AutoMapper to convert between a domain model into a view model which is passed to the view.