簡體   English   中英

擴展特定於實體的存儲庫類C#

[英]Extend Repository Class C# specific to Entity

我目前有一個實體框架存儲庫類,該類基於我在網上找到的以下代碼。

https://gist.github.com/ashrafeme/060f7773e25903ced986804ee7276f5f

這很好用,但是,我想創建其他方法來為特定實體執行特定功能。 例如,如果我有一個博客帖子需要按帖子類型過濾,則可以在控制器中編寫

 SqlRepository<Blog, MyContext> blogPostRepo = new SqlRepository<Blog, MyContext>(dbContext);
            LinkedList<Blog> blogPosts = (LinkedList<Blog>)blogPostRepo.Set<Blog>().Where(p => p.PostType.Id.Equals(2));

但是理想情況下,我只想擴展Base SqlRepository類以創建BlogPostRepository來嘗試保持代碼干凈。 我一時被BlogPostRepository中的類簽名卡住了。 我將如何編寫一個擴展以下基類的類/

 public class SqlRepository<TEntity, TContext> : IRepository<TEntity>, IDisposable
         where TEntity : class
         where TContext : DbContext
    {
enter code here

創建類,如:

public class BlogRepository : SqlRepository<Blog, MyContext>

使用必要的參數向派生類中添加一個構造函數,該參數調用基類的構造函數(Ref: Link

暫無
暫無

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

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