如何在通用存储库中编写通用innerJoin?
我正在使用通用存储库通过实体框架开发自己的 BLL。通用存储库 但都是通用的存储库没有内部联接。下面是:
public interface IRepository
{
IQueryable<T> List<T>() where T : class;
T Get<T>(int id) where T : class;
void Create<T>(T entityTOCreate) where T : class;
void Edit<T>(T entityToEdit) where T : class;
void Delete<T>(T entityToDelete) where T : class;
}
如何将上面的代码转换为:
public interface IRepository
{
IQueryable<T> List<T>() where T : class;
T Get<T>(int id) where T : class;
void Create<T>(T entityTOCreate) where T : class;
void Edit<T>(T entityToEdit) where T : class;
void Delete<T>(T entityToDelete) where T : class;
void InnerJoin<T>(T entityName, TNew entityName2) where T : class, where TNew : class;
}
或者我认为我们可以使用像这样的 Fluent 接口模式:
public List<MyWorker> ListByID( int ID)
{
using (var Ctx = new DomainRepository<Worker>(new ProposalsEntities()))
return Ctx.Find<Worker>(q => q.ID== ID).ToList().InnerJoin(XEntity,x=>x.ID=q.ID).InnerJoin(YEntity,y=>y.ID=q.ID);
}
Yuo 可以给出另一个建议来解决这个奇妙的问题。我如何在通用存储库中编写上述连接代码?
i am using generic repository to develop my own BLL with entity framework.Generic Repository But all generic repository doesn't have inner join. Lokk below :
public interface IRepository
{
IQueryable<T> List<T>() where T : class;
T Get<T>(int id) where T : class;
void Create<T>(T entityTOCreate) where T : class;
void Edit<T>(T entityToEdit) where T : class;
void Delete<T>(T entityToDelete) where T : class;
}
How to convert above code to :
public interface IRepository
{
IQueryable<T> List<T>() where T : class;
T Get<T>(int id) where T : class;
void Create<T>(T entityTOCreate) where T : class;
void Edit<T>(T entityToEdit) where T : class;
void Delete<T>(T entityToDelete) where T : class;
void InnerJoin<T>(T entityName, TNew entityName2) where T : class, where TNew : class;
}
or i think that we can use Fluent interfacve pattern like that:
public List<MyWorker> ListByID( int ID)
{
using (var Ctx = new DomainRepository<Worker>(new ProposalsEntities()))
return Ctx.Find<Worker>(q => q.ID== ID).ToList().InnerJoin(XEntity,x=>x.ID=q.ID).InnerJoin(YEntity,y=>y.ID=q.ID);
}
Yuo can give another advise to achive this fantastical question. How can i write above join code in Generic Repository?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对于您的通用存储库是不可能的。过度抽象数据库的关系模型并不是一个好主意。它阻止您使用特定于数据库的功能,例如...连接。
您可以通过添加附加方法来修复存储库:(
此示例适用于 Linq 2 Sql)。
您需要能够向 callerd 提供可组合查询,而不是列表。然后调用者可以写:
如果允许注释: 通用存储库模式不是一个好主意。它的帮助不大,但造成的伤害却很大。
要么直接使用DataContext/EntityContext/Session,要么使用专门的存储库。
This is not possible with your generic repository. It is not a good idea to abstract the relational model of the database to much. It prevents you from using database-specific features like ... joins.
You can fix your repository by adding an additonal method:
(This example is for Linq 2 Sql).
You need to be able to provide callerd with a composable query, not a list. Callers can then write:
If the comment is permitted: The generic repository pattern is not a good idea. It does little help but does great damage.
Either directly use the DataContext/EntityContext/Session directly or use specialized repositories.
存储库不应公开连接功能。存储库提供的域对象可能由保存在多个表中的数据组成,但域层不应该知道这一事实,或者更一般地说,不应该知道数据在持久层中采用的格式。某些数据源甚至可能无法提供联接功能。
A repository shouldn't expose join functionality. The domain object that the repository provides might be composed of data that's persisted in multiple tables but the domain layer shouldn't be aware of that fact or, more generally, of the format the data takes in the persistence layer. Some data sources may not even be able to provide join functionality.