开发者

Calling services within services

开发者 https://www.devze.com 2023-02-08 21:23 出处:网络
I have a service layer in my application which contains services such as AccountS开发者_如何学JAVAervice, UserService and DocumentService.

I have a service layer in my application which contains services such as AccountS开发者_如何学JAVAervice, 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;
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消