繁体   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