如何在混合模式下使用存储库的依赖注入

时间:2016-03-08 03:59:00

标签: c# .net oop dependency-injection

我有问题在混合模式下使用依赖注入,意味着一个存储库函数使用继承在第二个存储库中调用,但我不确定我在混合或出了什么问题,

  
      
  1. ISmallBizRepository interface
  2.   
public interface ISmallBizRepository
{
    #region Clients
    bool Insert(ClientsDM obj);
    bool Update(ClientsDM original, ClientsDM updated);
    ClientsDM GetClientsByPrimaryKey(Guid id);
    IQueryable GetClients(Guid id, Guid firmid);
    IQueryable GetClients(Guid firmid);
    bool DeleteClients(Guid id, Guid deletedby);
    #endregion
}
  
      
  1. SmallBizRepository class
  2.   
public  class SmallBizRepository : BaseQueryHelper,ISmallBizRepository
{
   private SmallBizDbContext _ctx;
   public SmallBizRepository(SmallBizDbContext ctx)
   {
        _ctx = ctx;
   }
   public SmallBizRepository(IQueryHelperRepository repo)
        : base(repo)
   {
   }
   #region Clients
    public IQueryable GetClients(Guid firmid)
    {
        return TheRepository.GetClient(firmid);
    }
    #endregion
}
  
      
  1. BaseQueryHelper class
  2.   
public class BaseQueryHelper
{
    private IQueryHelperRepository _repo;
    public BaseQueryHelper(IQueryHelperRepository repo)
    {
        _repo = repo;
    }

    protected IQueryHelperRepository TheRepository
    {
        get
        {
            return _repo;
        }
    }
}
  
      
  1. IQueryHelperRepository interface
  2.   
public interface IQueryHelperRepository
{
    #region Firm
    IQueryable GetClient(Guid firmid);
    IQueryable GetClient(Guid firmid, Guid id);
    #endregion
}
  
      
  1. QueryHelperRepository class
  2.   
   public class QueryHelperRepository : IQueryHelperRepository
   {
    private SmallBizDbContext _ctx;
    public QueryHelperRepository(SmallBizDbContext ctx)
    {
        _ctx = ctx;
    }
    #region Firm
    public IQueryable GetClient(Guid firmid)
    {
        var Clientjc = from a in _ctx.Client
                       join b in _ctx.AflAwmAddrBook on a.AddressBookId equals b.AddressBookId into ab
                       from b1 in ab.DefaultIfEmpty()
                       where (a.DeletedOn == null && a.FirmId == firmid)
                       orderby a.CreatedDate descending
                       select new
                       {
                           a.FirmId,
                           a.ClientId,
                           a.FirstName,
                           a.LastName,
                           a.Email,
                           a.Phone,
                           a.Address,
                           a.City,
                           a.ZipCode,
                           a.State,
                           a.Country,
                           a.UserId,
                           a.PayPalEmail,
                           a.EmailApprovedDate,
                           a.IsApproved,
                           a.DeletedBy,
                           a.DeletedOn,
                           a.CreatedDate,
                           a.AddressBookId,
                           AflAwmAddrBook_fullname = b1.fullname,
                           a.Picture,
                       };

        return Clientjc;
    }
    #endregion
    }
  
      
  1. BaseApiController class
  2.   
    public class BaseApiController : ApiController
    {
    private ISmallBizRepository _repo;
    private ModelFactory _modelFactory;

    public BaseApiController(ISmallBizRepository repo)
    {
        _repo = repo;
    }

    protected ModelFactory TheModelFactory
    {
        get
        {
            if (_modelFactory == null)
            {
                _modelFactory = new ModelFactory(Request, TheRepository);
            }
            return _modelFactory;
        }
    }

    protected ISmallBizRepository TheRepository
    {
        get
        {
            return _repo;
        }
    }
}

上面的存储库类是单独工作的,但是当我们使用QueryHelperRepository继承SmallBizRepository并使用这样的函数时

    #region Clients
    public IQueryable GetClients(Guid firmid)
    {
        return TheRepository.GetClient(firmid);
    }
    #endregion
SmallBizRepository中的

请帮助/指导我解决这个问题,感谢您的宝贵时间。谢谢。

1 个答案:

答案 0 :(得分:2)

在您的SmallBizRepository上,您应该有一个构造函数具有这样的依赖关系:

public class SmallBizRepository : BaseQueryHelper, ISmallBizRepository
{
    private SmallBizDbContext _ctx;
    public SmallBizRepository(SmallBizDbContext ctx, IQueryHelperRepository repo)
         : base(repo)
    {
        _ctx = ctx;
    }
    #region Clients
    public IQueryable GetClients(Guid firmid)
    {
        return TheRepository.GetClient(firmid);
    }
    #endregion
}