我有问题在混合模式下使用依赖注入,意味着一个存储库函数使用继承在第二个存储库中调用,但我不确定我在混合或出了什么问题,
- 醇>
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
}
- 醇>
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
}
- 醇>
BaseQueryHelper
class
public class BaseQueryHelper
{
private IQueryHelperRepository _repo;
public BaseQueryHelper(IQueryHelperRepository repo)
{
_repo = repo;
}
protected IQueryHelperRepository TheRepository
{
get
{
return _repo;
}
}
}
- 醇>
IQueryHelperRepository
interface
public interface IQueryHelperRepository
{
#region Firm
IQueryable GetClient(Guid firmid);
IQueryable GetClient(Guid firmid, Guid id);
#endregion
}
- 醇>
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
}
- 醇>
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;
}
}
}
上面的存储库类是单独工作的,但是当我们使用QueryHelperRepository
继承SmallBizRepository
并使用这样的函数时
#region Clients
public IQueryable GetClients(Guid firmid)
{
return TheRepository.GetClient(firmid);
}
#endregion
SmallBizRepository
中的。
请帮助/指导我解决这个问题,感谢您的宝贵时间。谢谢。
答案 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
}