I am relatively new to Entity Framework, and I might be misunderstanding some concepts, so I need some guidance. I am building a modular monolith using Domain-Driven Design (DDD) and trying to apply the concept of Bounded Contexts. I followed the idea presented in this article: https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/january/data-points-shrink-ef-models-with-ddd-bounded-contexts, but I haven’t had success so far.
For some reason, the configuration is not being interpreted as it should. An example of this is that EF is treating my value objects as entities after trying this approach
The approach I am trying to follow is to use a central building block in my architecture, where I have a base DbContext that contains all the classes representing the tables of my application. Additionally, I am using the same database for all bounded contexts. Below is the implementation of the base DbContext:
public class BaseDbContext<TContext> : DbContext
where TContext : DbContext
{
protected BaseDbContext(DbContextOptions<TContext> options) : base(options)
{
Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(SchemaManagementAssemblyReference.Assembly);
}
}
In the assembly of this DbContext, I also have objects that represent the full definition of my tables and their configurations. For example:
internal sealed class AddressSchema : BaseEntity
{
public override int Id { get; protected set; }
public override Guid Uuid { get; protected init; }
public int? ParticipantId { get; private set; }
public int? AdherenceId { get; private set; }
public int? BeneficiaryId { get; private set; }
public Street? Street { get; private set; }
public Neighborhood? Neighborhood { get; private set; }
public City? City { get; private set; }
public State? State { get; private set; }
public Country? Country { get; private set; }
public ZipCode? ZipCode { get; private set; }
public Number? Number { get; private set; }
public AddressLine? AddressLine { get; private set; }
public bool IsForeign { get; private set; }
public override DateTime CreatedAt { get; protected set; }
public override DateTime? UpdatedAt { get; protected set; }
public override bool IsDeleted { get; protected set; }
}
I also have some ValueObjects that are configured as columns in the database
public sealed class AddressLine : ValueObject
{
private readonly string _value;
//...
private AddressLine(string value)
{
//...
}
public static implicit operator string? (AddressLine? addressLine) => addressLine?._value;
public static implicit operator AddressLine(string value) => new(value);
//...
}
And the mappings configuration:
public abstract class BaseEntityTypeConfiguration<T> : IEntityTypeConfiguration<T>
where T : BaseEntity
{
public virtual void Configure(EntityTypeBuilder<T> builder)
{
builder.Property(entity => entity.Uuid)
.ValueGeneratedOnAdd()
.HasDefaultValueSql(SchemaConstants.DefaultValues.MssqlNewGuidValue);
builder.HasIndex(entity => entity.Uuid)
.IsUnique();
builder.Property(entity => entity.CreatedAt)
.HasDefaultValueSql(SchemaConstants.DefaultValues.MssqlCurrentUtcDateValue)
.ValueGeneratedOnAdd();
builder.Property(entity => entity.UpdatedAt);
builder.Property(entity => entity.IsDeleted)
.HasDefaultValue(false)
.ValueGeneratedOnAdd();
builder.HasQueryFilter(entity => !entity.IsDeleted)
.HasIndex(entity => entity.IsDeleted)
.HasFilter(FilterConstants.IsNotDeletedFilter);
builder.HasIndex(entity => entity.IsDeleted);
}
}
internal sealed class AddressSchemaMap : BaseEntityTypeConfiguration<AddressSchema>
{
public override void Configure(EntityTypeBuilder<AddressSchema> builder)
{
base.Configure(builder);
builder.Property(address => address.Street)
.HasMaxLength(Street.MaxLength)
.HasConversion(ValueObjectConverter<Street, string>.Create()!);
builder.Property(address => address.City)
.HasMaxLength(City.MaxLength)
.HasConversion(ValueObjectConverter<City, string>.Create()!);
builder.Property(address => address.Neighborhood)
.HasMaxLength(Neighborhood.MaxLength)
.HasConversion(ValueObjectConverter<Neighborhood, string>.Create()!);
builder.Property(address => address.ZipCode)
.HasMaxLength(ZipCode.MaxLength)
.HasConversion(ValueObjectConverter<ZipCode, string>.Create()!);
builder.Property(address => address.Country)
.HasMaxLength(Country.MaxLength)
.HasConversion(ValueObjectConverter<Country, string>.Create()!);
builder.Property(address => address.State)
.HasMaxLength(State.MaxLength)
.HasConversion(ValueObjectConverter<State, string>.Create()!);
builder.Property(address => address.AddressLine)
.HasMaxLength(AddressLine.MaxLength)
.HasConversion(ValueObjectConverter<AddressLine, string>.Create()!);
builder.Property(address => address.Number)
.HasMaxLength(Number.MaxLength)
.HasConversion(ValueObjectConverter<Number, string>.Create()!);
builder.Property(address => address.IsForeign)
.IsRequired();
builder.HasOne<ParticipantSchema>()
.WithOne(participant => participant.Address)
.HasForeignKey<AddressSchema>(address => address.ParticipantId)
.OnDelete(DeleteBehavior.NoAction);
builder.HasOne<AdherenceSchema>()
.WithOne(adherence => adherence.Address)
.HasForeignKey<AddressSchema>(address => address.AdherenceId)
.OnDelete(DeleteBehavior.NoAction);
builder.HasOne<BeneficiarySchema>()
.WithOne(beneficiary => beneficiary.Address)
.HasForeignKey<AddressSchema>(address => address.BeneficiaryId)
.OnDelete(DeleteBehavior.NoAction);
builder.ToTable("Addresses");
}
}
In my bounded contexts, I derive from this base DbContext as follows:
internal sealed class LoanDbContext : BaseDbContext<LoanDbContext>
{
public LoanDbContext(DbContextOptions<LoanDbContext> options) : base(options)
{
}
public DbSet<Address> Addresses{ get; private set; }
//....
}
In my bounded contexts, I use the domain entities in my DbSets, but I am not sure if this might be causing an issue.
When I attempt to create a new migration, I get the following error:
Unable to create a 'DbContext' of type ''. The exception 'No suitable constructor was found for entity type 'AddressLine'. The following constructors had parameters that could not be bound to properties of the entity type:
Cannot bind 'value' in 'AddressLine(string value)'
Note that only mapped properties can be bound to constructor parameters. Navigations to related entities, including references to owned types, cannot be bound.'