M
M
Mozzarella2018-01-09 21:35:57
WordPress
Mozzarella, 2018-01-09 21:35:57

How to write an interface for a database?

At the moment there is such a repository interface

public interface IRepository
    {
        
        DbSet<Sub> Subs { get; set; }
        IEnumerable<Comment> Comments { get; }
        IEnumerable<Review> Reviews { get; }
        IEnumerable<Application> Applications { get; }
        IEnumerable<Tender> Tenders { get; }
        IEnumerable<Offer> Offers { get; }
        IEnumerable<SubGroup> SubGroups { get; }
    }

And concrete implementation using EntityFramework
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IRepository
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
                base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
        }
        public DbSet<Post> Posts { get; set; }
        public DbSet<Attachment> Attachments { get; set; }
        public DbSet<User_Meta> User_Meta { get; set; }
        public DbSet<Post_Meta> Post_Meta { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<SubGroup> SubGroups { get; set; }
        public DbSet<Sub> Subs { get; set; }
        public IEnumerable<Comment> Comments { get { return Posts.OfType<Comment>().AsEnumerable(); } }
        public IEnumerable<Review> Reviews { get { return Posts.OfType<Review>().AsEnumerable(); } }
        public IEnumerable<Application> Applications { get { return Posts.OfType<Application>().AsEnumerable(); } }
        public IEnumerable<Tender> Tenders { get { return Posts.OfType<Tender>().AsEnumerable(); } }
        public IEnumerable<Offer> Offers { get { return Posts.OfType<Offer>().AsEnumerable(); } }
       
    }

Each table must have Create,Update,Delete methods, which IEnumerable does not provide. What would be the right way: to use type casting in the code when it is required to make changes in a certain table, or to take this into account in the interface?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2018-01-09
@evgshk

It will be correct not to interfere with DbContext with Repository.
Create an interface IRepository<T>, define there the signatures of the methods Add, Update, Delete etc. Implement this interface in an abstract base class RepositoryBase<T>, adding a typed property there DbSet<T>. Then, for each entity, create a specific class (CommentsRepo, PostsRepo ... etc.), inheriting from the abstract one RepositoryBase<T>. If necessary, you can also implement an interface for the same entity (if there is logic different from CRUD).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question