Search code examples
servicedomain-driven-designfacade

Calling services within services


I have a service layer in my application which contains services such as AccountService, UserService and DocumentService.

I use StructureMap for my dependency injection so the constructor of my service might look like this:

public AccountService(IAccountRepository repo)
{
    this.accountRepository = repo;
}

Now if I need access to say the UserService whilst in there is it good form to have the following?

public AccountService(IAccountRepository repo, IUserService user)
{
    this.accountRepository = repo;
    this.userService = user;
}

Solution

  • Yes, it perfectly fine. You can download the code and watch the episodes Rob Conery prepared.

    public class CatalogService : Commerce.Services.ICatalogService 
    {
        ICatalogRepository _repository = null;
        IOrderService _orderService = null;
    
        /// <summary>
        /// Creates a CatalogService based on the passed-in repository.
        /// </summary>
        /// <param name="repository">An ICatalogRepository</param>
        public CatalogService(
            ICatalogRepository repository, IOrderService orderService) 
        {
            _repository = repository;
            _orderService = orderService;
    
            if (_repository == null)
                throw new InvalidOperationException("Repository cannot be null");
        }
        ...
    }
    

    He injects the OrderService in the CatalogService.