Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the business logic go in MVC?

I am a beginner to the ASP.Net MVC. After reading many tutorials and digesting its concepts, I have yet to see an approach that clearly demonstrates where does the business logic go.

My app will have a lot of use of jQuery AJAX usage (which will be calling Controller's Actions for various purposes such as dependent interaction, validation). I will definitely use the ViewModel concept, but I am still unclear where the business logic should reside. I do not want to put in the controller or a model. Should I put it in a separate service layer?

like image 970
sarsnake Avatar asked Nov 10 '11 23:11

sarsnake


2 Answers

I think you pretty much answered your own question, in a separate project.
Not in the controllers and absolutely not in the models.

Edit: Notice that the controller is highly coupled with the httpcontext so it will be a very smart thing to move the logic layer to a different dll-layer.

like image 152
gdoron is supporting Monica Avatar answered Oct 01 '22 13:10

gdoron is supporting Monica


The M in MVC is everything that are used to fetch and process the information that you use in your application. The business layer is therefore a part of it.

I would just start by creating a separate class library and put all of my logic in it. As long as your classes are fairly small and has a clear responsibility it's quite easy to refactor later if you need a separate webservice (another project needs access to the data).

I usually create a third class library and put all definitions in it (service interfaces and the domain models). by doing so you'll follow the separated interface pattern and make it even easy to swap the implementation later on (and to test your business layer)

like image 30
jgauffin Avatar answered Oct 01 '22 13:10

jgauffin