简体   繁体   English

扩展特定于实体的存储库类C#

[英]Extend Repository Class C# specific to Entity

I currently have a entity framework repository class that has been based upon the below code I found online. 我目前有一个实体框架存储库类,该类基于我在网上找到的以下代码。

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

This works fine, however, I'd like to create additional methods that perform specific functions for specific entities. 这很好用,但是,我想创建其他方法来为特定实体执行特定功能。 For example, if I had a blog post that needed to filter by Post Type, I could write in my controller 例如,如果我有一个博客帖子需要按帖子类型过滤,则可以在控制器中编写

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

However Ideally I would like to just extend the Base SqlRepository Class to create a BlogPostRepository to try and keep the code clean. 但是理想情况下,我只想扩展Base SqlRepository类以创建BlogPostRepository来尝试保持代码干净。 I'm getting stuck at the minute with the class signatures in my BlogPostRepository. 我一时被BlogPostRepository中的类签名卡住了。 How would I write a class that extends the below base class/ 我将如何编写一个扩展以下基类的类/

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

Create class like: 创建类,如:

public class BlogRepository : SqlRepository<Blog, MyContext>

Add a constructor to the derived class with necessary parameters that calls the constructor of the base class (Ref : Link ) 使用必要的参数向派生类中添加一个构造函数,该参数调用基类的构造函数(Ref: Link

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

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