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. :)
Soe moe
source share