none
How to separate domain classes from EF code first layer RRS feed

  • Question

  • i am trying to get control over DDD with EF code first. i saw when people work with EF code first then domain classes reside there in same classes. just see a small example.

       public class TestDBContext : DbContext
        {
            public TestDBContext()
                : base("name=TestDBContext")
            {
            }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                //modelBuilder.Configurations.Add(new vwCustomerConfiguration());
                Database.SetInitializer<TestDBContext>(null);
            }
    
            public DbSet<Customer> Customer { get; set; }
            public DbSet<Addresses> Addresses { get; set; }
            public DbSet<Contacts> Contacts { get; set; }
            public virtual DbSet<vwCustomer> vwCustomers { get; set; }
            public DbSet<vwMyCustomers> vwMyCustomers { get; set; }
        }

    customer, address, contact and all domain classes are in same project but i want to put all these domain classes in different project.

    just see new project hierarchy which i am thinking to implement. all project name will start with my company then do and project name

    here it is

    1) Impex.Domain

    2) Impex.Storage

    3) Impex.Business

    4) Impex.UI

    so i will have 4 layers and those are domain, Storage, Business and UI. Storage, Business and UI these 3 layer will have reference of Domain layer because these 3 layers Storage, Business and UI may use domain classes.

    UI will pass data to business layer and received data from business layer. business layer again will talk to Storage layer where EF code first will be implemented to interact with DB.

    if i can successfully complete my project following 4 layers then people should consider my project is based on DDD pattern or not ?

    so tell me am i thinking right way. please give all your suggestion and guidance. thanks

    Tuesday, November 1, 2016 9:36 AM

Answers

  • i got few links but not sure does it relevant for mapping EF Virtual Model and domain model ?

    https://www.exceptionnotfound.net/entity-framework-and-wcf-mapping-entities-to-dtos-with-automapper/

    https://rogeralsing.com/2013/12/01/why-mapping-dtos-to-entities-using-automapper-and-entityframework-is-horrible/

    http://www.dataworks.ie/Blog/Item/entity_framework_5_with_automapper_and_repository_pattern

    thanks

    • Marked as answer by Sudip_inn Saturday, November 5, 2016 4:39 PM
    Friday, November 4, 2016 1:30 PM
  • i got few links but not sure does it relevant for mapping EF Virtual Model and domain model ?

    https://www.exceptionnotfound.net/entity-framework-and-wcf-mapping-entities-to-dtos-with-automapper/

    https://rogeralsing.com/2013/12/01/why-mapping-dtos-to-entities-using-automapper-and-entityframework-is-horrible/

    http://www.dataworks.ie/Blog/Item/entity_framework_5_with_automapper_and_repository_pattern

    thanks

    Automapper is a about mapping the properties for one object to another object. It a generic utility program to map the objects for you, because else, you are going to have to write the mapping code.

    It makes no difference what the objects are as long as you can tell Automapper about the objects.

    • Marked as answer by Sudip_inn Saturday, November 5, 2016 4:39 PM
    Friday, November 4, 2016 6:24 PM

All replies

  • Hi Mou_inn,

    >>if i can successfully complete my project following 4 layers then people should consider my project is based on DDD pattern or not ?

    From a DDD perspective, there's no such thing as "an Entity designed with EF in mind". A DDD Entity should be persistence ignorant and bear no trace of any ORM. The domain layer has no interest in how, where, whether or when its objects are stored. So I think you could put all domain classes in different project.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 2, 2016 9:59 AM
    Moderator
  • i was looking for instruction and sample code to achieve what i asked.
    Wednesday, November 2, 2016 1:27 PM
  • i was looking for instruction and sample code to achieve what i asked.

    Domain Model objects and EF Virtual Model objects are two different objects with two different purposes.

    At best, EF Virtual Model objects should be mapped over to Domain Model objects and Domain Model objects should be mapped back to the EF Virtual Model objects for data persistence using a solution like Auto Mapper to do the mapping, or you can write the mapping code yourself.

    EF should be unaware of the Domain Model objects and why you have some

    public virtual DbSet<vwCustomer> vwCustomers { get; set; }  

    Public DbSet<vwMyCustomers> vwMyCustomers { get; set; }

    is wrong that should not be there.


      
    Wednesday, November 2, 2016 4:29 PM
  • you said :- At best, EF Virtual Model objects should be mapped over to Domain Model objects and Domain Model objects should be mapped back to the EF Virtual Model objects for data persistence using a solution like Auto Mapper to do the mapping, or you can write the mapping code yourself.

    would please small but complete code which show how to map EF Virtual Model with domain model using auto mapper. thanks

    Thursday, November 3, 2016 10:55 AM
  • you said :- At best, EF Virtual Model objects should be mapped over to Domain Model objects and Domain Model objects should be mapped back to the EF Virtual Model objects for data persistence using a solution like Auto Mapper to do the mapping, or you can write the mapping code yourself.

    would please small but complete code which show how to map EF Virtual Model with domain model using auto mapper. thanks

    I don't have time for this I work for a living. You have to find an article on how to use AutoMapper. You need to have the Domain Objects in their on project, call it Entities, that can be referenced by all projects needing to use the Domain objects. On  methods that read data from EF you need to map the properties from EF object to Domain object and send the Domain object where you need to send it. On persisting data back to the DB, then you send the Domain object the methods that are going to persist the EF object to the DB you map the Domain object over to the EF object and you persist the EF object to the DB.

    You should be able to figure this out by yourself.

     
    Thursday, November 3, 2016 2:56 PM
  • u said :- EF Virtual Model objects should be mapped over to Domain Model objects and Domain Model objects should be mapped back to the EF Virtual Model objects for data persistence using a solution like Auto Mapper to do the mapping, or you can write the mapping code yourself.

    ok i can understand that u r busy. if possible at least give me few article on this which show how we can use auto mapper to map relation between domain class and EF Virtual Model objects.

    if this is not even possible then tell me which keyword i use in google search to get relevant links. thanks

    Friday, November 4, 2016 8:27 AM
  • u said :- EF Virtual Model objects should be mapped over to Domain Model objects and Domain Model objects should be mapped back to the EF Virtual Model objects for data persistence using a solution like Auto Mapper to do the mapping, or you can write the mapping code yourself.

    ok i can understand that u r busy. if possible at least give me few article on this which show how we can use auto mapper to map relation between domain class and EF Virtual Model objects.

    if this is not even possible then tell me which keyword i use in google search to get relevant links. thanks

    https://www.google.com/#q=automapper

    I really don't understand why you can't do this yourself in  using Bing or Google, your friends, to go find solutions.

    Friday, November 4, 2016 11:17 AM
  • i got few links but not sure does it relevant for mapping EF Virtual Model and domain model ?

    https://www.exceptionnotfound.net/entity-framework-and-wcf-mapping-entities-to-dtos-with-automapper/

    https://rogeralsing.com/2013/12/01/why-mapping-dtos-to-entities-using-automapper-and-entityframework-is-horrible/

    http://www.dataworks.ie/Blog/Item/entity_framework_5_with_automapper_and_repository_pattern

    thanks

    • Marked as answer by Sudip_inn Saturday, November 5, 2016 4:39 PM
    Friday, November 4, 2016 1:30 PM
  • i got few links but not sure does it relevant for mapping EF Virtual Model and domain model ?

    https://www.exceptionnotfound.net/entity-framework-and-wcf-mapping-entities-to-dtos-with-automapper/

    https://rogeralsing.com/2013/12/01/why-mapping-dtos-to-entities-using-automapper-and-entityframework-is-horrible/

    http://www.dataworks.ie/Blog/Item/entity_framework_5_with_automapper_and_repository_pattern

    thanks

    Automapper is a about mapping the properties for one object to another object. It a generic utility program to map the objects for you, because else, you are going to have to write the mapping code.

    It makes no difference what the objects are as long as you can tell Automapper about the objects.

    • Marked as answer by Sudip_inn Saturday, November 5, 2016 4:39 PM
    Friday, November 4, 2016 6:24 PM