简体   繁体   中英

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

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. I'm getting stuck at the minute with the class signatures in my 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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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