Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Model can have business logic?

Tags:

asp.net-mvc

I read a couple of articles which defines domain model (as in MVC) as something that holds business logic. I never considered a model to hold any methods other than the model properties.

I would like to know if actually there is a thought which supports having functions and business logic in the domain models.

Thanks in advance.

like image 404
w3dev Avatar asked Nov 23 '12 09:11

w3dev


People also ask

Can we write business logic in model?

A1: Business Logic goes to Model part in MVC . Role of Model is to contain data and business logic. Controller on the other hand is responsible to receive user input and decide what to do.

What is MVC business logic?

MVC enables the application to be extensible and modular by separating the application into three parts: the business logic part, which implements data retrieval and manipulation. the user interface part, which is what the application users see. the controller part, which routes requests to the proper objects.

Where do you put business logic in MVC?

The business logic should be placed in the model, and we should be aiming for fat models and skinny controllers. As a start point, we should start from the controller logic. For example: on update, your controller should direct your code to the method/service that delivers your changes to the model.

What is business logic in asp net?

The Business Logic layer handles all of the business rules, calculations and actual logic within your application that makes it actually "do" things and it may often use some of the objects retrieved from your data-access layer.


1 Answers

Of course business logic should be inside domain models. But, domain models are more than just entity framework entities. Domain models consists of many small classes which reflects business domain.

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

  • ViewModels which responsible for model for view.
  • Controllers which is thin and responsible for application flow.
  • Simple business logic such as required field can exist as attribute within entity framework model or ViewModels.
  • Complex business logic such as place order, booking ticket are promoted to be its own class such as PlaceOrderOperation or PlaceOrderCommand.
  • Simple query logic might be inside the Controller or short extension method to DbSet<Entity> type.
  • Complex Query also promoted to its own class such as GetMostPorpularProductsQuery assuming that the query is complex.
  • Infrastructure components may be extension to Entity Framework or MVC components such as ActionFilter, CustomRoute, CustomTemplate or its own classes such as EncyptionHelpers etc.

Conclusion

Building domain Model is more than just creating classes prefix with BusinessLogic such as UserBusinessLogic or with Services such as UserServices. It should consists of many small classes which responsible for one thing. Of course, you would require some usage of design patterns, choice of frameworks, infrastructure components such as error handling, localization, caching, etc.

Welcome to the trade-off world. :)

like image 152
Soe Moe Avatar answered Oct 31 '22 14:10

Soe Moe