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);
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 24, 2016 at 7:34
\$\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);
 }
}
answered Aug 24, 2016 at 9:04
\$\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\$ Commented Aug 24, 2016 at 9:42
  • \$\begingroup\$ @RobH Cool, didn't know that. \$\endgroup\$ Commented 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\$ Commented 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();
}
answered Aug 24, 2016 at 7:45
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.