Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jun 18, 2008 03:11 PM by MDeibert
Jun 13, 2008 02:25 PM|LINK
I've been searching the net for two days trying to get an answer to a question that no one seems to have the answer. Where do you put business logic in an asp.net MVC application? I've read articles that say "of course it goes in the controller". I've also
read articles that say "oh it must go in the model". I've also found some articles talking about "service classes" or a "service layer". I wish someone faaaaar smarter than me would answer this question. If I were to guess, I'd go with the service classes
but I don't know how to impliment. Would this be calls to static classes from the controller or the model? I'm so confused :-/
Can anyone shed some light and perhaps post a short code sample?
Thanks everyone [:)]
MVC .net business rules logic
Jun 13, 2008 03:40 PM|LINK
I can understand where you are coming from! Each and every time something new comes out, rather than explaining things straight-forward, you get a mix of different explanations as to how to do what you need to do, and a plethora of ways to accomplish it.
That aside ythe business logic in this type of framework typically (though not always as it depends on how your application is architected and designed) goes in the Model. The View is typically what you would use for the presentation - a web page / form
for example. The Controller for lack of a better explanation, is like a "coordinator" between two parties (the go-between), taking in requests from the View, passing them through to the Model, getting back a response and sending it back on to the View.
Of course this explanation is not infer that logic of "any kind" would not be part of a Controller class, but in the context where you might be talking about where you might put the business logic for your business class (such a Product class), the Product
class would represent the Model and would contain it's business logic.
As well, depending on the design of your application, the logic may further still be placed in yet another class in some other tier in the application and the business class, such as the Product class example I used above may be something as simple as a
data transfer object (DTO) and serve as the Model.
Anyway, I hope this helps a little more.
Jun 13, 2008 10:56 PM|LINK
There are several approaches for that.
First, if you are creating MVC app without separate class library for DAL, you might create LINQ dbml file and put it in Models folder. You might also create partial classes to extend classes in that file. Operation not related to database access you might
perform in a controller actions
Second, if you use separate class library, you might use repository pattern, as outlined in
mike hadlow blog post or you might want to use code sample from msdn code gallery "An Example of a Multi Tier
Architecture for Linq to Sql". There are several MVC sample apps that you can use to review various data access strategies, and I'll mention suteki shop by mike hadlow (on google code) and MVC storefront by Rob Conery. For a list of MVC based live apps,
take a look at Mike Bosch blog.
Entire MVC hype started with Scott Guthrie, and on his blog there are introductory posts on MVC. Code samples are outdated, but entire logic behind MVC and some recommendations can be found in MVC series on his blog . Also, take a look at videos recorded
by Scott Hanselman and MVC Storefront screencasts by Rob Conery.
Jun 14, 2008 04:04 AM|LINK
The best way to get answers to questions like that is to look at reference implementations.
Codecamp server is the best example that I have seen. Download it and look at what they do.
The short answer is that some of it goes in the model, and some into service classes. Putting domain logic in the controllers is a clear abuse of the MVC pattern. The controllers are only for presentation logic and controlling program flow.
Jun 17, 2008 02:44 PM|LINK
I'm reading about MVC "Action Filters". Could these be used some how for business rules? Well, yes I guess they could be, but would that cause any problems? Would that affect testing? Still trying to get my head around MVC. Thanks for all the replies everyone.
Jun 18, 2008 03:11 PM|LINK
I'm using the repository and service layer pattern from codecamp server project. -Very- confusing but I do have it working now. Thanks guys!