First of all, I want to clarify a small incorrect concept of the business layer, since you want to use the repository template, and your setup is a candidate for Domain Driven Design , then the business layer is [Domain model ( Objects and objects of value , where you develop your business logic in an object-oriented style in objects and objects) and Application Layer for coordinating transactions and operations and commands at the domain level], so the proposed architecture will be something like this:
- Presentation (MVC) [OrderView, OrderPresentationModel, OrderController]
- Application [OrderService]
- Use UnitOfWork (transactions) and Repositories to execute domain logic
- DTOs [OrderDTO, CustomerDTO]
- Domain
- Objects and objects of value [Order, Customer, Linear address, Address]
- Repository Interfaces [IOrderRepository, ICustomerRepository]
- (optional) Working Interface Interface [IUnitOfWork]
- Infrastructure.DataAccess (using ORM technology or data access)
- Repositories [OrderRepository, CustomerRepository]
- (optional) Unit of work [UnitOfWork]
- Infrastructure.Common
Example script:
[Presentation] OrderController:
_orderService.CreateOrder(OrderDTO);
[Application] OrderService:
_unitOfWork.BeginTransaction(); var customer = _customerRepository.GetById(orderDTO.CustomerId); var order = new Order() { Customer=customer, Price=orderDTO.Price, ... } _orderRepository.Add(order); _unitOfWork.Commit();
About your questions:
1) Does MVC transaction management provide a good idea? At what stage should transactional management take place? It seems to me that MVC is not responsible for transactions, but how to avoid this?
No, I would prefer to split it at the application level to make the design flexible to support various presentations.
2) Should there be some kind of automatic way of processing transactions? This implementation of ActionFilter is semi-automatic, but transaction management is clearly located in the MVC section, which is not a business layer.
Use application level transactions.
3) Is the UnitOfWork class the same as the business layer class? - if so, does this mean that we can add our own methods of business logic to it? - if not, are we wrapping the unit of work with some other classes that contain business logic methods?
No, this is just a way to group tasks into transactions. Business logic is actually encapsulated in an entity, and if the logic is not associated with one entity, it must be implemented in the services of the domain [TransferService.Transfer (account1, account2, amount)], for approvals, access to repositories and transactions, the application layer is the place .