Saturday, November 20, 2010

Entity framework code first development setting entity configurations

Entity framework allows creation of classes that can be used to configure the entity properties in a separate class and later add it to the model configuration collection. This approach helps you to encapsulate the configuration information for each type in specific classes. Each configuration class derives from the EntityConfiguration class and the constructor contains all the configuration logic. This is the preferred approach as it encapsulates and makes the code easier to read.

We can now try to rewrite the code used in the previous post using EntityConfiguration classes. Our new configuration classes look like.
public class UserConfiguration : EntityConfiguration<User>
{
    public UserConfiguration()
    {
        MapSingleType(u =>
            new
            {
                u.Id,
                u.FirstName,
                u.LastName,
                u.UserName,
                u.Password,
                u.CreatedDate,
                u.ChangedDate,
                u.Version
            }).ToTable("dbo.Users");

        HasKey<Guid>(u => u.Id);
        Property(u => u.Version).IsConcurrencyToken();

        SetValidations();
    }

    private void SetValidations()
    {
        Property(u => u.FirstName).IsRequired().HasMaxLength(100);
        Property(u => u.LastName).IsRequired().HasMaxLength(100);
        Property(u => u.UserName).IsRequired().HasMaxLength(200);
        Property(u => u.Password).IsRequired();
    }
}

public class OrderConfiguration : EntityConfiguration<Order>
{
    public OrderConfiguration()
    {
        MapSingleType(o =>
            new
            {
                o.Id,
                o.OrderNumber,
                o.OrderDate,
                o.TotalAmount,
                o.UserId,
                o.CreatedDate,
                o.ChangedDate,
                o.Version
            }).ToTable("dbo.Orders");

        HasKey<Guid>(o => o.Id);
        Property(o => o.Version).IsConcurrencyToken();

        SetValidations();
        SetRelationships();
    }

    private void SetValidations()
    {
        Property(o => o.OrderNumber).IsRequired().HasMaxLength(25);
    }

    private void SetRelationships()
    {
        HasRequired(o => o.User).HasConstraint((o, u) => o.UserId == u.Id);
    }
}

public class ProductConfiguration : EntityConfiguration<Product>
{
    public ProductConfiguration()
    {
        MapSingleType(p =>
            new
            {
                p.Id,
                p.Name,
                p.Amount,
                p.CreatedDate,
                p.ChangedDate,
                p.Version
            }).ToTable("dbo.Products");

        HasKey<Guid>(p => p.Id);
        Property(p => p.Version).IsConcurrencyToken();

        SetValidations();
    }

    private void SetValidations()
    {
        Property(p => p.Name).IsRequired().HasMaxLength(100);
        Property(p => p.Amount).HasPrecision(9, 2);
    }
}

The configuration objects are later passed to the ModelBuilder on the OnModelCreating event
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
        {
            SetModelConfigurations(modelBuilder);         
        }

        private void SetModelConfigurations(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new UserConfiguration());
            modelBuilder.Configurations.Add(new OrderConfiguration());
            modelBuilder.Configurations.Add(new ProductConfiguration());
        }

1 comment:

Jijo V.G said...

Need to work on this, I never looked in to....