6

I want to know the right concept about it. If I have a MVC application with Repository Pattern, where the BL should be?

  • Should it be inside the Model? Model should have all the business logic before call the unitofwork to insert or not the data into database?

  • Should it be in the controller? Before call the model?

  • Should I have a service layer to do the business logic and decide if I should call the Model to call the UnitOfWork to save the data?

A good explanation will help a lot too.

3 Answers 3

8

The short answer - it depends. If it's a fairly complex or sizable application, I like to create a service layer project with the repositories as dependencies. If it's a small application, I'll put the logic in the controller. In my opinion, if it takes more time and effort to create the service layer than it would be to create the application (i.e. one or two controllers), then it doesn't make sense to me to go that route. You also need to consider the likelihood that the application will grow. What might start small could grow into something much bigger and in that case, again, it might be more beneficial to create the separate service layer.

Sign up to request clarification or add additional context in comments.

4 Comments

This is the best answer. Controllers should be thin - their main purpose is selecting the correct View and transferring data between the View and the Models. If your application is small or simple (search, read only, etc.) then just put that logic in your controller. At the point where your controllers are doing more than just facilitating data transfer to and from the Views, then you should consider creating a service layer. Sometimes the best solution is keeping it simple. Creating a service layer just because is a million dollar solution to a 1,000 dollar problem.
@Matty-M thanks!! So If I have a service Layer, should the service layer have an Insert/Update/Delete method with a UnitOfWork inside it? Or a Model inside it wich call the unit of work?
@LeandroDeMelloFagundes That's what I do in my service layer. The repository itself will handle the crud operations, but I have similarly named methods in the service layer that handle any business logic and if necessary, create the unit of work and pass that to the repository (in cases where injecting the UOW isn't possible).
@MattyM so, your Model doesn't have any kind of methods? Only the properties wich represents the table columns? The insert/update/delete aren't in your Model right? Sorry for all this questions, but I really want to understand the better way to start it :)
4

The third one... and then some.

Your application structure could look like this (each in different projects):

  • Data storage layer (e.g. SQL database)
  • ORM (e.g. NHibernate or Entity Framework)
  • Domain (including abstract repositories and entities)
  • Service layer (and optionally business)
  • MVC application (which has it's own models relating to the entities)

but there are many ways to go about this depending on the complexity and size of your application.

Comments

2

There is no "correct" answer to this question, it is primarily opinion-based. You can read about my opinion in the following project wiki:

https://github.com/danludwig/tripod/wiki/Why-Tripod%3F

https://github.com/danludwig/tripod/wiki/Dependency-and-Control-Inversion

https://github.com/danludwig/tripod/wiki/Tripod-101

https://github.com/danludwig/tripod/wiki/Testing,-Testing,-1-2-3

https://github.com/danludwig/tripod/wiki/Command-Query-Responsibility-Segregation-(CQRS)

Another piece of advice I would like to offer is never put any business logic in viewmodels or entities. These classes should not have methods, only properties to contain data. Separate your data from behavior. Use models for data, and other types for behavior (methods).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.