ASP.NET MVC - Can a model have business logic?

I read a couple of articles that define a domain model (as in MVC) as something that contains business logic. I have never considered a model to store any methods other than model properties.

I would like to know if there really is a thought that supports functions and business logic in domain models.

Thanks in advance.

+7
source share
3 answers

Of course, business logic must be inside domain models. But, domain models are more than just entities. Domain models are made up of many small classes that reflect a business domain.

In my typical MVC application, I usually separate some types of business logic into these (but not limited to):

  • ViewModels , which is responsible for the model to view.
  • Controllers that are thin and are responsible for the flow of applications.
  • Simple business logic , such as a required field, can exist as an attribute within an entity model or ViewModels.
  • Comprehensive business logic , such as booking a seat, booking ticket, is promoted in such a way as to be its own class, such as PlaceOrderOperation or PlaceOrderCommand.
  • The logic of a simple request can reside inside a controller or short extension method to a type of DbSet<Entity> .
  • A complex query also advances to its own class, such as GetMostPorpularProductsQuery, assuming the query is complex. Components
  • Infrastructure can be added to Entity Framework or MVC components, such as ActionFilter, CustomRoute, CustomTemplate, or its own classes, such as EncyptionHelpers, etc.

Conclusion

Creating a domain model is more than just prefixing classes with BusinessLogic, such as UserBusinessLogic, or with services like UserServices. It should consist of many small classes that are responsible for one. Of course, you will need some use of design patterns, a choice of frameworks, infrastructure components such as error handling, localization, caching, etc.

Welcome to the world of compromise. :)

+12
source

An MVC Model can really have business logic. The responsibilities of MVC are discussed in more detail here and here's a discussion of anemic domain models - can this help clean things up for you?

From MSDN :

Models that are provided for classes representing the application model for your MVC web application. This folder usually includes code that defines objects and defines the logic for interacting with the data warehouse. Typically, actual model objects will be separate class libraries. However, when you create a new application, you can put the classes here and then move them to a separate library class at a later point in the development cycle.

What can confuse the problem is that many ASP.Net MVC implementations use View Models , which are classes used to pass view-level data between View and Controller.

In a typical large project setup, we usually delete the Models folder and instead completely transfer our EF data layer, entities and business logic to separate assemblies.

+4
source

Based on my experience, the best place to place business logic is the layer between controllers and models. Try some popular templates like repository or tasks / commands .

0
source

All Articles