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

이 직접 주문 저장소에 액세스 할 수있는 사용자 서비스 OK인가, 아니면 그것은 단지 주문 서비스에 대한 종속성이 있어야 쌓아 방법입니까?

+0

서비스가 데이터베이스 작업을 수행하기 전에 수행 할 비즈니스 로직을 가지고 있으므로 저장소 대신 서비스를 사용하는 것이 좋습니다. 하지만 두 서비스가 서로 의존해서는 안되기 때문에주의해야합니다. 그렇지 않으면 교착 상태에 빠지게됩니다. –

답변

1

IOrderRepositoryUserService에 액세스하고 있습니다. 귀하의 질문은 이것이 올바른 접근법인지 또는 IUserRepository에만 액세스해야하며 IOrderRepository 대신 OrderService으로 전화해야합니다.

IMO, 모든 서비스는 필요에 따라 모든 저장소를 호출 할 수 있습니다. 서비스와 저장소 사이에는 1 <-> 1 관계가 없습니다.

저장소는 데이터에 대한 액세스를 제공합니다. 여러 서비스에서 이러한 액세스가 필요한 경우 여러 서비스가 동일한 리포지토리를 사용할 수 있습니다. 이것은 매우 깨끗하고 설명 할 만해 보인다.