Mendz.Data and the Entity Framework

I originally thought I needed Mendz.Data.EntityFramework. After some minor re-design and simplification, I realized that all I really needed was just Mendz.Data. Immediately, Mendz.Data.EntityFramework became obsolete.

Defining a DbContext instance is a task by itself. Adding more to the steps is not always welcome. You may have an existing DbContext instance and you can use it as-is to create Mendz.Data-aware repositories derived from Mendz.Data.Common.DbRepositoryBase. The only requirement is that your DbContext instance should be semi-self-initializing -- via DbContext constructor or by implementing/overriding DbContext.OnConfiguring().

DbRepositoryBase can support any context type. After passing the desired context type, it's a quick go ahead to adding the Mendz.Data.Repository(.Async) CRUDS methods. DbRepositoryBase handles the instantiation and disposal of the context. The context can be a DbContext instance itself, or a Mendz.Data-aware context derived from Mendz.Data.Common's GenericDbDataContextBase or DbDataContextBase.

To create a Mendz.Data-aware repository that uses a DbContext instance:
  1. Create the DbContext instance as you normally would (ex.: DbContextInstanceName : DbContext). Make it semi-self-initializing. One way to do this is to make it Mendz.Data-aware by using Mendz.Data.Common.EntityFrameworkDataSettingOption during initialization/OnConfiguring.
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(DataSettingOptions
            .ConnectionStrings[EntityFrameworkDataSettingOption.Name]);
    }
    
    
  2. Create your repository as follows:
    
        public class ModelNameRepository 
            : DbRepositoryBase<DbContextInstanceName>
    
    
  3. After that, you can add the Mendz.Data.Repository(.Async) CRUDS interfaces in your repository's inheritance list. Implement the inherited CRUDS interfaces using DbRepositoryBase.DbDataContext, which, in this example, is actually your DbContextInstanceName.
Find sample codes for an EF context and EF repository using Mendz.Data at https://github.com/etmendz/Mendz.Data/wiki.

Comments