1

I am having a 3 layer architecture.

1) C# MVC Application - UI layer

2) Business Layer - consisting of service interfaces and its implementation and repository interfaces

3) Data Access Layer - consisting of Repository interface implementation

The application is divided into different modules. A module is nothing but a C# class library. Each module has its own business layer and data access layer. There is loose coupling between the layers, so each layer accesses the other through interface only. To give you an example, here is how the application is stacked up

// 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 {
}

Is it OK for the User service to directly access the Order repository or should it have dependency on the Order service only ?

devanalyst
  • 1,348
  • 4
  • 28
  • 55
  • It's good to use service instead of repository because services have business logic to perform before performing any database operation. But you need to be careful here that both services should not be dependent on each other else you will end up in deadlock. – Chetan Apr 08 '17 at 13:57

1 Answers1

4

You are accessing IOrderRepository in UserService. Your question is whether this is correct approach, or it should ONLY access IUserRepository and call OrderService instead of IOrderRepository.

IMO, any service may call any repository as per need. There is no 1 <-> 1 relation between Service and Repository.

Repository provide you an access to data. If such access is necessary in multiple services, then multiple services can consume same Repository. This looks very clean and explainable.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
  • Thanks for detailed answer. Though I have 1 more: is it OK to service access both repository and service of another module? In my case, for example, `UsersService` needs both `OrdersRepository` and `OrdersService`? Or it is bad smell? Thanks in advance – WelcomeTo Feb 17 '20 at 14:04
  • @MyTitle: **IMO:** 1) In case of **Layered Architecture**, accessing one service in other should not be an issue. It actually help code reuse. Such a usage should be minimum though. 2) In case of **DDD Architecture**, this is definitely code smell because here, Domain Services are part of your bonded context. If you need to access different service in one service, may be you badly designed your bonded context. Better solution here is to use Domain Events or some other tool provided by DDD. Hope that helps. – Amit Joshi Feb 17 '20 at 14:11