Posts

Showing posts from January, 2019

CQRS db Contexts with .NET Core

We're developing a new app using .NET Core 2.2 and it's time for some redesign. When it came to EF db Contexts we wanted to separate our Reads from our Writes (a la CQRS) more explicitly. We needed to work with the same entities in both BUT we still wanted to optimize performance with Reads and customize Writes separately so below is a quick and easy implementation. Base Context Setup a Base Context class that you will inherit from; you can either inherit from DbContext or in this case IdentityDbContext since we're also leveraging Identity Core. public abstract class BaseContext <TContext> : IdentityDbContext<User> where TContext : DbContext { protected BaseContext (DbContextOptions<TContext> options) : base (options) { } protected override void OnModelCreating (ModelBuilder modelBuilder) { base .OnModelCreating(modelBuilder); } // TODO add DbSet<> for each entity/table (as needed) public DbSet<Audit> Audit { get ; s