Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut in Fluent API to set multiple properties as required

  1. Here is what I am currently doing:

    modelBuilder.Entity<Product>().Property(e => e.Name).IsRequired();                   
    modelBuilder.Entity<Product>().Property(e => e.UPC).IsRequired();      
    modelBuilder.Entity<Product>().Property(e => e.Price).IsRequired();      
    modelBuilder.Entity<Product>().Property(e => e.Description).IsRequired();
    
  2. Here is what I would like to be doing:

    modelBuilder.Entity<Product>()
        .Property(e => e.Name).IsRequired()
        .Property(e => e.UPC).IsRequired()
        .Property(e => e.Price).IsRequired()
        .Property(e => e.Description).IsRequired()
    

The latter, though, doesn't work. Is there another way not to have to repeat the modelBuilder.Entity<Product>() each time?

  1. Here is the current most pithy option:

    var e = modelBuilder.Entity<Product>();
    e.Property(e => e.Name).IsRequired();                   
    e.Property(e => e.UPC).IsRequired();      
    e.Property(e => e.Price).IsRequired();      
    e.Property(e => e.Description).IsRequired();
    
like image 322
Shaun Luttin Avatar asked Aug 25 '13 01:08

Shaun Luttin


1 Answers

This is compatible with all of the existing DbModelBuilder extension methods since it's just adding a fluent layer on top, but that it does come with some syntactical overhead. Not exactly what you asked for, but doesn't involve mucking around with the supporting code. Haven't fully tested this yet, but it should work if you're comfortable with the syntax:

// First option - like this better because it has less cruft than multiple Has invocations

var modelBuilder = new DbModelBuilder();
var modelConfiguration = new ModelConfigurator(modelBuilder);

modelConfiguration.Entity<Product>().Has(e => {
                                         e.Property(en => en.Name).IsRequired();
                                         e.Property(en => en.UPC).IsRequired();
                                         e.Property(en => en.Price).IsRequired();
                                         e.Property(en => en.Description).IsRequired();}
                                        );           

OR

var modelBuilder = new DbModelBuilder();
var modelConfiguration = new ModelConfigurator(modelBuilder);
modelConfiguration.Entity<Product>().Has(e => e.Property(en => en.Name).IsRequired())
                                    .Has(e => e.Property(en => en.UPC).IsRequired())
                                    .Has(e => e.Property(en => en.Price).IsRequired())
                                    .Has(e => e.Property(en => en.Description).IsRequired());

// continue configuring properties, and creating methods on ModelConfigurator as needed

Supporting code:

  public class Product{
        public string Name {get;set;}
        public double Price {get;set;}
        public string UPC {get;set;}
        public string Description {get;set;}

    }

    public class ModelConfigurator{

        public DbModelBuilder ModelBuilder{get;set;}

        public ModelConfigurator(DbModelBuilder modelBuilder){
            ModelBuilder = modelBuilder;
        }

        public EntityConfigurator<TEntity> Entity<TEntity>() where TEntity : class {
            var entity = ModelBuilder.Entity<TEntity>();
            return new EntityConfigurator<TEntity>(entity);
        }
    }

    public class EntityConfigurator<TEntity> where TEntity : class{

        public EntityTypeConfiguration<TEntity> EntityTypeConfiguration {get;set;}

        public EntityConfigurator(EntityTypeConfiguration<TEntity> entityTypeConfiguration){
            EntityTypeConfiguration = entityTypeConfiguration;
        }

        public EntityConfigurator<TEntity> Has(Action<EntityTypeConfiguration<TEntity>> a){
            a(this.EntityTypeConfiguration);
            return this;
        }
    }
like image 80
DavidN Avatar answered Sep 29 '22 04:09

DavidN