存储库模式中的共享服务

时间:2014-02-06 11:29:27

标签: c# architecture repository-pattern unit-of-work service-layer

我想知道当一个服务内部说ICompanyService我需要调用IUserAccountService中的另一个方法时如何处理这种情况。 ?

因此,一般情况下,如果没有 UserAccount 公司就不应该存在。

IUserAccount实现服务类如下所示:

public class UserAccountService : CrudService<UserAccount>, IUserAccountService
{
    private readonly IRepository<UserAccount> _userAccountRepository;
    private readonly IUnitOfWorkFactory _unitOfWorkFactory;

    public CompanyService(IRepository<UserAccount> userAccountRepository,
                          IUnitOfWorkFactory unitOfWorkFactory)
        : base(userAccountRepository, unitOfWorkFactory)
    {
        _userAccRepository = userAccRepository;
    }

    public int RegisterUser(UserAccount user) {

        using (var uow=_unitOfWorkFactory.Create())
        {
           // Details omitted for brievity
           var userId = _userAccountRepository.Create(user);
           uow.Commit();
           return userId;
        }
    }

   //Other service methods

}

公司ICompanyService实施:

public class CompanyService : CrudService<Company>, ICompanyService
{
    private readonly IRepository<Company> _companyRepository;
    private readonly IUnitOfWorkFactory _unitOfWorkFactory;

    public CompanyService(IRepository<Company> companyRepository,
                          IUnitOfWorkFactory unitOfWorkFactory)
        : base(companyRepository, unitOfWorkFactory)
    {
        _companyRepository= companyRepository;
    }

    public int CreateCompanyWithUserAccount(Company company) {

        using (var uow=_unitOfWorkFactory.Create())
        {
           // Some validation with the company.Details omitted for brievity
           // Here I need an instance of IUserAccountService
           // Suppose I get it through DI or IoC
           var userAccountService = IoC.Resolve<IUserAccountService>();
    ###    // Is such approach good or bad?!    ###
           var userId = userAccountService.RegisterUser(company.UserAccount);
           // Map the user id to the company
           company.UserAccount.Id = userId;
           var companyId = _companyRepository.Create(company);
           uow.Commit();
           return companyId;
        }
    }

   //Other service methods

}
存储库下的

ORM 是: NHibernate

2 个答案:

答案 0 :(得分:0)

您可以依赖IRepository<UserAccount>

public class CompanyService : CrudService<Company>, ICompanyService
{
    private readonly IRepository<Company> _companyRepository;
    private readonly IRepository<UserAccount> _userAccountRepository;
    private readonly IUnitOfWorkFactory _unitOfWorkFactory;

    public CompanyService(IRepository<Company> companyRepository,
                          IUnitOfWorkFactory unitOfWorkFactory
                          IRepository<UserAccount> userAccountRepository)
        : base(companyRepository, unitOfWorkFactory)
    {
        _companyRepository= companyRepository;
        _userAccountRepository = userAccountRepository;
    }

    public int CreateCompanyWithUserAccount(Company company) {

        using (var uow=_unitOfWorkFactory.Create())
        {
           // Some validation with the company.Details omitted for brievity
           var userId = _userAccountRepository.Create(company.UserAccount);
           // Map the user id to the company
           company.UserAccount.Id = userId;
           var companyId = _companyRepository.Create(company);
           uow.Commit();
           return companyId;
        }
    }

   //Other service methods

}

IMO,最好是依赖存储库。在您的所有公司服务创建公司之后,它需要在数据库中执行一些工作,这就是存储库的用途。从我在代码中看到的内容来看,不需要涉及UserAccountService

答案 1 :(得分:0)

  1. 似乎UserAccountService实施中的构造函数错误:public CompanyService

  2. CompanyService实现中,您最好在构造函数中解析IUserAccountService依赖项权限,因此每次创建对象时都要执行一次,而不是每次调用方法时都这样做。

  3. 这些依赖项没有问题。如果IUnitOfWorkFactory实现的两个对象出现问题 - &gt;制作单身人士