2
\$\begingroup\$

I tried to refactor my code with entity framework configuration, splitting in multiple private methods, one per entity, each responsible for configuring things related to that entity and only that entity. But I feel like Version 2 can still be improved.

What do you think?

Version 1:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Country>()
        .Property(e => e.Code)
        .IsFixedLength()
        .IsUnicode(false);

    modelBuilder.Entity<Country>()
        .Property(e => e.PhoneCode)
        .IsFixedLength()
        .IsUnicode(false);

    modelBuilder.Entity<Country>()
        .HasMany(e => e.Locations)
        .WithRequired(e => e.Country)
        .WillCascadeOnDelete(false);
}

Version 2:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    Configure(modelBuilder.Entity<Country>());
}

Configure(EntityTypeConfiguration<Country> entity)
{
    entity
        .Property(e => e.Code)
        .IsFixedLength()
        .IsUnicode(false);

    entity  
        .Property(e => e.PhoneCode)
        .IsFixedLength()
        .IsUnicode(false);

    entity
        .HasMany(e => e.Locations)
        .WithRequired(e => e.Country)
        .WillCascadeOnDelete(false);
}
\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

My approach is to keep the XxxxxDbContext class as lightweight as possible:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    new ModelCreator().Execute(modelBuilder);
}

The ModelCreator class looks like this:

internal class ModelCreator
{
    public void Execute(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

        modelBuilder.Configurations.Add(new FirmConfiguration());
        // etc.
    }        
}

Then each table has its own EntityTypeConfiguration<TEntityType>:

internal class FirmConfiguration : EntityTypeConfiguration<Firm>
{
    public FirmConfiguration()
    {
        HasKey(x => new { x.Id });

        Property(t => t.Name).HasMaxLength(30).IsRequired();
        Property(t => t.Address).HasMaxLength(120).IsRequired();
        Property(t => t.PostalCode).HasMaxLength(4).IsRequired();
        Property(t => t.City).HasMaxLength(30).IsRequired();
        Property(t => t.Responsible).HasMaxLength(30).IsRequired();
        Property(t => t.Email).HasMaxLength(40).IsRequired();
        Property(t => t.Phone).HasMaxLength(14);
    }
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ FWIIW, There's a method on the DbModelBuilder that scans an assembly for EntityTypeConfiguration<TEntityType>s: modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());. You can also do something like typeof(FirmConfiguration).Assembly if the configs aren't in same assembly as the context. \$\endgroup\$
    – RobH
    Aug 24, 2016 at 9:42
  • \$\begingroup\$ @RobH Cool, didn't know that. \$\endgroup\$
    – BCdotWEB
    Aug 24, 2016 at 9:48
  • \$\begingroup\$ this is how I'm configuring entities too. I'm coming from nhibernate and I have a number of mapping classes and pocos. this methods allows me to keep my domain objects clean while also mapping to EF. \$\endgroup\$
    – Fran
    Aug 24, 2016 at 13:43
1
\$\begingroup\$

Consider splitting the Configure method into smaller pieces. Each piece should configure only one entity at a time.

You could write them as extensions so that you an chain them:

static DbModelBuilder ConfigureCountry(this DbModelBuilder modelBuilder)
{
    var entity = modelBuilder.Entity<Country>();

    entity
        .Property(e => e.Code)
        .IsFixedLength()
        .IsUnicode(false);
    ..
    return modelBuilder;
}

inside OnModelCreating

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder
        .ConfigureCountry()
        .ConfigureAnotherEntity();
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.