簡體   English   中英

如何在通用存儲庫中編寫通用innerJoin?

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

我正在使用通用存儲庫來使用實體框架開發自己的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 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可以再提出一個建議來解決這個奇妙的問題。 如何在通用存儲庫中編寫以上聯接代碼?

這對於您的通用存儲庫是不可能的。 將數據庫的關系模型抽象得太多不是一個好主意。 它阻止您使用特定於數據庫的功能,例如...連接。

您可以通過添加其他方法來修復存儲庫:

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

(此示例適用於Linq 2 Sql)。

您需要能夠為callerd提供可組合的查詢,而不是列表。 然后,呼叫者可以寫:

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

如果允許注釋:通用存儲庫模式不是一個好主意。 它沒有什么幫助,但造成了很大的破壞。

直接使用DataContext / EntityContext / Session或使用專用存儲庫。

存儲庫不應公開聯接功能。 存儲庫提供的域對象可能由持久保存在多個表中的數據組成,但是域層不應該知道這一事實,或者更一般地說,不應該知道持久層中數據采用的格式。 某些數據源甚至可能無法提供連接功能。

暫無
暫無

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

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