1
我有3层架构。一个模块的业务层可以直接访问另一个模块的存储库吗?
1)C#MVC应用 - UI层
2)业务层 - 包括服务接口和其执行和存储库接口
3)数据访问层 - 由库接口实现的
该应用程序分为不同的模块。模块不过是一个C#类库。每个模块都有自己的业务层和数据访问层。层之间有松散的耦合,所以每层只通过接口访问另一层。给你举个例子,这里的应用程序是如何堆积起来
// UI layer
public class UserController: Controller
{
IUserService userService;
IOrderService orderService;
public UserController(IUserService userService, IOrderService orderService){
this.userService = userService;
this.orderService = orderService;
}
}
//Business layer - User module
public class UserService: IUserService
{
IUserRepository userRepository;
IOrderRepository orderRepository;
public UserService(IUserRepository userRepository, IOrderRepository
orderRepository){
this.userRepository = userRepository;
//Should this be here or replaced by order service ?
this.orderRepository = orderRepository;
}
}
//Business layer - Order module
public class OrderService: IOrderService
{
IOrderRepository orderRepository;
public UserService(IOrderRepository orderRepository){
this.orderRepository= orderRepository;
}
}
//Data access layer - User module
public class UserRepository: IUserRepository {
}
//Data access layer - Order module
public class OrderRepository: IOrderRepository {
}
是否确定为用户服务直接访问订单仓库还是应该有仅在订购服务的依赖?
使用服务而不是存储库是很好的,因为服务在执行任何数据库操作之前都要执行业务逻辑。但是,在这里你必须小心,这两个服务不应该相互依赖,否则你最终会陷入僵局。 –