Move Domain Classes Configuration To Separate Classes

In  the code first approach of entity framework we have two methods to configure the entities: DataAnnotations and Fluent API.

Generally developers prefer the FluentAPI approach because it provide more flexibility and configuration methods. All configuration codes of domain classes are present in “OnModelCreating” methods. Configuration of every domain class in OnModelCreating can makes it more complex and unmanageable. But, Code First approach provides a method whereby we can place the configuration of each entity into separate files that makes easy to focus on configuration code of a single domain class.

Let us take tthe below example:
  1. namespace ConsApp  
  2. {  
  3.     using System;  
  4.     using System.Data.Entity;  
  5.     using System.ComponentModel.DataAnnotations.Schema;  
  6.     using System.Linq;  
  7.   
  8.     public partial class Data_Model : DbContext  
  9.     {  
  10.         public Data_Model()  
  11.             : base("name=Data_Model")  
  12.         {  
  13.         }  
  14.   
  15.         public virtual DbSet<TblComment_And_Rating> TblComment_And_Rating { getset; }  
  16.         public virtual DbSet<TblCompany_General_Info> TblCompany_General_Info { getset; }  
  17.         public virtual DbSet<TblCompany_Payment_Info> TblCompany_Payment_Info { getset; }  
  18.   
  19.         protected override void OnModelCreating(DbModelBuilder modelBuilder)  
  20.         {  
  21.             modelBuilder.Entity<TblComment_And_Rating>()  
  22.                 .Property(e => e.Comment_Text)  
  23.                 .IsUnicode(false);  
  24.             modelBuilder.Entity<TblComment_And_Rating>().  
  25.                 Property(e => e.Company_Id)  
  26.                 .IsConcurrencyToken(true);  
  27.             modelBuilder.Entity<TblComment_And_Rating>().  
  28.                 Property(e => e.Company_Id)  
  29.                 .HasColumnName("cmp_Id");  
  30.   
  31.             modelBuilder.Entity<TblCompany_General_Info>()  
  32.                 .Property(e => e.Company_Name)  
  33.                 .IsUnicode(false);  
  34.   
  35.             modelBuilder.Entity<TblCompany_General_Info>()  
  36.                 .Property(e => e.Contact_Person)  
  37.                 .IsUnicode(false);  
  38.   
  39.             modelBuilder.Entity<TblCompany_General_Info>()  
  40.                 .Property(e => e.Establish_Year)  
  41.                 .IsFixedLength()  
  42.                 .IsUnicode(false);  
  43.   
  44.             modelBuilder.Entity<TblCompany_General_Info>()  
  45.                 .Property(e => e.Pincode)  
  46.                 .IsFixedLength()  
  47.                 .IsUnicode(false);  
  48.   
  49.             modelBuilder.Entity<TblCompany_General_Info>()  
  50.                 .Property(e => e.Address)  
  51.                 .IsUnicode(false);  
  52.   
  53.             modelBuilder.Entity<TblCompany_General_Info>()  
  54.                 .Property(e => e.Mobile_Number)  
  55.                 .IsUnicode(false);  
  56.   
  57.             modelBuilder.Entity<TblCompany_General_Info>()  
  58.                 .Property(e => e.Landline_Number)  
  59.                 .IsUnicode(false);  
  60.   
  61.             modelBuilder.Entity<TblCompany_General_Info>()  
  62.                 .Property(e => e.Email_Id)  
  63.                 .IsUnicode(false);  
  64.   
  65.             modelBuilder.Entity<TblCompany_General_Info>()  
  66.                 .Property(e => e.Company_Logo)  
  67.                 .IsUnicode(false);  
  68.   
  69.             modelBuilder.Entity<TblCompany_General_Info>()  
  70.                 .Property(e => e.Company_Image)  
  71.                 .IsUnicode(false);  
  72.   
  73.             modelBuilder.Entity<TblCompany_General_Info>()  
  74.                 .Property(e => e.Website)  
  75.                 .IsUnicode(false);  
  76.   
  77.             modelBuilder.Entity<TblCompany_General_Info>()  
  78.                 .Property(e => e.Keyword)  
  79.                 .IsUnicode(false);  
  80.   
  81.             modelBuilder.Entity<TblCompany_General_Info>()  
  82.                 .Property(e => e.Facebook_Id)  
  83.                 .IsUnicode(false);  
  84.   
  85.             modelBuilder.Entity<TblCompany_General_Info>()  
  86.                 .Property(e => e.Linkedin_Id)  
  87.                 .IsUnicode(false);  
  88.   
  89.             modelBuilder.Entity<TblCompany_General_Info>()  
  90.                 .Property(e => e.Twitter_Id)  
  91.                 .IsUnicode(false);  
  92.   
  93.             modelBuilder.Entity<TblCompany_General_Info>()  
  94.                 .Property(e => e.Google_Plus_Id)  
  95.                 .IsUnicode(false);  
  96.   
  97.             modelBuilder.Entity<TblCompany_General_Info>()  
  98.                 .HasMany(e => e.TblComment_And_Rating)  
  99.                 .WithRequired(e => e.TblCompany_General_Info)  
  100.                 .WillCascadeOnDelete(false);  
  101.   
  102.             modelBuilder.Entity<TblCompany_General_Info>()  
  103.                 .HasMany(e => e.TblCompany_Payment_Info)  
  104.                 .WithRequired(e => e.TblCompany_General_Info)  
  105.                 .WillCascadeOnDelete(false);  
  106.         }  
  107.     }  
  108. }  
We can see dbContext class contain three Dbset for “TblComment_And_Rating”, “TblCompany_General_Info” and “TblCompany_Payment_Info” model classes. The “OnModelCreating” method contain the configuration codes for “TblComment_And_Rating” and “TblCompany_General_Info” domain class(Entity). Now we create two separate configuration class and move the configuration code to these classes.

Now we create the “General_Info_Config” and “Comment_Rating_Config” configuration classes that derives from EntityTypeConfiguration<TEntity>.

General_Info_Config Class
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data.Entity.ModelConfiguration;  
  4. using System.Linq;  
  5.   
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8.   
  9. namespace ConsApp  
  10. {  
  11.   
  12.     class General_Info_Config: EntityTypeConfiguration<TblCompany_General_Info>  
  13.     {  
  14.        public General_Info_Config()  
  15.         {  
  16.   
  17.             Property(e => e.Company_Name).  
  18.                IsUnicode(false);  
  19.   
  20.               
  21.                     Property(e => e.Contact_Person)  
  22.                     .IsUnicode(false);  
  23.   
  24.               
  25.                     Property(e => e.Establish_Year)  
  26.                     .IsFixedLength()  
  27.                     .IsUnicode(false);  
  28.   
  29.               
  30.                    Property(e => e.Pincode)  
  31.                     .IsFixedLength()  
  32.                     .IsUnicode(false);  
  33.   
  34.               
  35.                     Property(e => e.Address)  
  36.                     .IsUnicode(false);  
  37.   
  38.               
  39.                     Property(e => e.Mobile_Number)  
  40.                     .IsUnicode(false);  
  41.   
  42.               
  43.                     Property(e => e.Landline_Number)  
  44.                     .IsUnicode(false);  
  45.   
  46.               
  47.                     Property(e => e.Email_Id)  
  48.                     .IsUnicode(false);  
  49.   
  50.               
  51.                     Property(e => e.Company_Logo)  
  52.                     .IsUnicode(false);  
  53.   
  54.               
  55.                     Property(e => e.Company_Image)  
  56.                     .IsUnicode(false);  
  57.   
  58.               
  59.                     Property(e => e.Website)  
  60.                     .IsUnicode(false);  
  61.   
  62.               
  63.                     Property(e => e.Keyword)  
  64.                     .IsUnicode(false);  
  65.   
  66.               
  67.                     Property(e => e.Facebook_Id)  
  68.                     .IsUnicode(false);  
  69.   
  70.               
  71.                     Property(e => e.Linkedin_Id)  
  72.                     .IsUnicode(false);  
  73.   
  74.   
  75.             Property(e => e.Twitter_Id)  
  76.                     .IsUnicode(false);  
  77.   
  78.   
  79.             Property(e => e.Google_Plus_Id)  
  80.                     .IsUnicode(false);  
  81.   
  82.   
  83.             HasMany(e => e.TblComment_And_Rating)  
  84.                     .WithRequired(e => e.TblCompany_General_Info)  
  85.                     .WillCascadeOnDelete(false);  
  86.   
  87.   
  88.             HasMany(e => e.TblCompany_Payment_Info)  
  89.                     .WithRequired(e => e.TblCompany_General_Info)  
  90.                     .WillCascadeOnDelete(false);  
  91.         }  
  92.     }  
  93. }  
We can see that all configuration code of “TblCompany_General_Info” entity is placed into constructor of “General_Info_Config” class that is derived from “EntityTypeConfiguration”. Using similar way now we create the “Comment_Rating_Config” class and move the configuration code of “TblComment_And_Rating” domain class from “OnModelCreating” method to this class.

Comment_Rating_Config Class
  1. using System.Data.Entity.ModelConfiguration;  
  2.   
  3. namespace ConsApp  
  4. {  
  5.     class Comment_Rating_Config : EntityTypeConfiguration<TblComment_And_Rating>  
  6.     {  
  7.         public Comment_Rating_Config()  
  8.         {  
  9.             
  10.                 Property(e => e.Comment_Text)  
  11.                 .IsUnicode(false);  
  12.              
  13.                 Property(e => e.Company_Id)  
  14.                 .IsConcurrencyToken(true);  
  15.              
  16.                 Property(e => e.Company_Id)  
  17.                 .HasColumnName("cmp_Id");  
  18.         }  
  19.     }  
  20. }  
In the above code we created Comment_Rating_Config class and placed the configuration code of TblComment_And_Rating domain class. After separating the configuration code now need to specify entity type in a generic place holder for which you include configurations. Now we add the reference of configuration class in “OnModelCreating” method .

method

In the above code we added the references of configurations classes into OnModelCreating method. It is a good approach to separate the configuration of domain classes and create a separate configuration class for each domain classes. Using this approach we can reduce the complexity of code and it reduce the efforts to find out the all configuration methods related to a specific domain classes. So if you are developing a large application that contain many domain classes and you are using “Fluent API” approach for configuration the domain classes, in that case you should be separate the configuration codes related to domain classes. 


Similar Articles