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 UserServic
e 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.
精彩评论