2017-04-08 75 views
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 { 
} 

是否确定为用户服务直接访问订单仓库还是应该有仅在订购服务的依赖?

+0

使用服务而不是存储库是很好的,因为服务在执行任何数据库操作之前都要执行业务逻辑。但是,在这里你必须小心,这两个服务不应该相互依赖,否则你最终会陷入僵局。 –

回答

1

您正在访问IOrderRepositoryUserService。你的问题是这是否是正确的方法,或者它应该只访问IUserRepository并且请拨打OrderService而不是IOrderRepository

IMO,任何服务都可以根据需要调用任何存储库。 Service和Repository之间不存在关系。

存储库为您提供对数据的访问。如果这种访问在多个服务中是必需的,那么多个服务可以使用相同的Repository。这看起来很干净和可以解释。

相关问题