Bounded Contexts and EF First Code - How to Build Them?
IMHO, one of the brightest DDD concepts is the ability to separate contexts in an application. But I am confused as to how I can get everything to work together.
First, I know that one is not related to the other. But my question falls precisely on the infrastructure / ORM part.
For example, I have a domain object called Procedure (medical procedure). In the context of registration, only the Code and Name are important . But in the context of procedure management, I have many other fields, such as Price , that are relevant to this context.
How can I have two objects with the same name (in different contexts) with different properties using EF Code First? Basically, I want to keep all fields in one table, but only get 2 fields in one context and all fields in another. How can I achieve this?
source to share
Below is a description of Shrink EF Models with DDD Bounded Contexts .
Domain models:
namespace SO25454395.Domain.Registration
{
using System;
public class Procedure
{
public int Id { get; set; }
public Guid Code { get; set; }
public string Name { get; set; }
}
}
...
namespace SO25454395.Domain.ProcedureManagement
{
using System;
public class Procedure
{
public int Id { get; set; }
public Guid Code { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
Bounded contexts:
namespace SO25454395.Infrastructure
{
using System.Data.Entity;
public class RegistrationContext : BaseContext<RegistrationContext>
{
public DbSet<Domain.Registration.Procedure> Prodedures { get; set; }
}
}
...
namespace SO25454395.Infrastructure
{
using System.Data.Entity;
public class ProcedureManagementContext : BaseContext<ProcedureManagementContext>
{
public DbSet<Domain.ProcedureManagement.Procedure> Procedures { get; set; }
}
}
By default, Code First is creating a new database for each context, so we'll disable it using the base class and specify the database to be shared between all contexts.
namespace SO25454395.Infrastructure
{
using System.Data.Entity;
public class BaseContext<TContext> : DbContext where TContext : DbContext
{
static BaseContext()
{
Database.SetInitializer<TContext>(null);
}
protected BaseContext() : base("Database")
{
}
}
}
Finally, we need a context that contains all the classes used to build the complete model. This context is used for initialization.
namespace SO25454395.Infrastructure
{
using SO25454395.Domain.ProcedureManagement;
using System.Data.Entity;
public class DatabaseContext : DbContext
{
public DbSet<Procedure> Procedures { get; set; }
public DatabaseContext() : base("Database")
{
}
}
}
Test:
namespace SO25454395.Tests
{
using SO25454395.Infrastructure;
using System;
using System.Linq;
using Xunit;
public class BoundedContextTests
{
[Fact]
public void Test()
{
using (var databaseContext = new DatabaseContext())
{
databaseContext.Database.Initialize(true);
}
var code = Guid.NewGuid();
var name = "Name";
var price = 123.45m;
using (var procedureManagementContext = new ProcedureManagementContext())
{
var procedure = new Domain.ProcedureManagement.Procedure { Code = code, Name = name, Price = price };
procedureManagementContext.Procedures.Add(procedure);
procedureManagementContext.SaveChanges();
}
using (var registrationContext = new RegistrationContext())
{
var procedure = registrationContext.Prodedures.Single(p => p.Code == code);
Assert.Equal(name, procedure.Name);
// procedure.Price is not available here.
}
}
}
}
source to share