7

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.

w3dev
  • 545
  • 2
  • 8
  • 21

3 Answers3

12

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. :)

Community
  • 1
  • 1
Soe Moe
  • 3,428
  • 1
  • 23
  • 32
  • I'm new to ASP.NET / EF and I don't really get the **Complex business logic** part. How is `PlaceOrderOperation` supposed to work? Like `new PlaceOrderOperation(...).execute()`? – cubuspl42 Oct 24 '17 at 22:30
  • @cubuspl42 Yes. [method object](https://refactoring.guru/replace-method-with-method-object) is much easier to work with (especially, you can split into many methods) – Soe Moe Dec 05 '17 at 17:03
4

An MVC Model can indeed have business logic. MVC responsibilities been discussed in more depth here and here is a discussion on anemic domain models - this might help clear things up for you?

From MSDN:

Models, which is provided for classes that represent the application model for your MVC Web application. This folder usually includes code that defines objects and that defines the logic for interaction with the data store. Typically, the actual model objects will be in separate class libraries. However, when you create a new application, you might put classes here and then move them into separate class libraries at a later point in the development cycle.

What might be confusing the issue is that many ASP.Net MVC implementations use View Models, which are classes used to transfer presentation tier data between View and Controller.

In a typical large project setup, we usually delete the Models folder, and instead move our EF data layer, Entities, and business / service logic into separate assemblies entirely.

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
0

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

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
Kamil Będkowski
  • 1,092
  • 4
  • 16
  • 36