Answer the question
In order to leave comments, you need to log in
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; }
}
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(); } }
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question