簡體   English   中英

如何在混合模式下使用存儲庫的依賴注入

[英]how to use dependency injection with repository in mix mode

我有問題在混合模式下使用依賴注入,意味着一個存儲庫函數使用繼承在第二個存儲庫中調用,但我不確定我在混合或出了什么問題,

  1. ISmallBizRepository interface
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
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
public class BaseQueryHelper
{
    private IQueryHelperRepository _repo;
    public BaseQueryHelper(IQueryHelperRepository repo)
    {
        _repo = repo;
    }

    protected IQueryHelperRepository TheRepository
    {
        get
        {
            return _repo;
        }
    }
}
  1. IQueryHelperRepository interface
public interface IQueryHelperRepository
{
    #region Firm
    IQueryable GetClient(Guid firmid);
    IQueryable GetClient(Guid firmid, Guid id);
    #endregion
}
  1. QueryHelperRepository class
   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
    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;
        }
    }
}

上面的存儲庫類是單獨工作的,但是當我們使用SmallBizRepository繼承QueryHelperRepository並使用這樣的函數時

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

SmallBizRepository

請幫助/指導我解決這個問題,感謝您的寶貴時間。謝謝。

在你的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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM