简体   繁体   English

如何在通用存储库中编写通用innerJoin?

[英]How to write generic innerJoin in Generic Repository?

i am using generic repository to develop my own BLL with entity framework. 我正在使用通用存储库来使用实体框架开发自己的BLL。 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: 或者我认为我们可以使用Fluent interfacve模式,例如:

    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. Yuo可以再提出一个建议来解决这个奇妙的问题。 How can i write above join code in Generic Repository? 如何在通用存储库中编写以上联接代码?

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: 您可以通过添加其他方法来修复存储库:

IQueryable<T> Query<T>() { return dataContext.GetTable<T>(); }

(This example is for Linq 2 Sql). (此示例适用于Linq 2 Sql)。

You need to be able to provide callerd with a composable query, not a list. 您需要能够为callerd提供可组合的查询,而不是列表。 Callers can then write: 然后,呼叫者可以写:

from w in repo.Query<Worker>()
join e in repo.Query<XEntity>() on ...

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. 直接使用DataContext / EntityContext / Session或使用专用存储库。

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. 某些数据源甚至可能无法提供连接功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM