Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6971717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:52:33+00:00 2026-05-27T16:52:33+00:00

I have several concerns when trying to do DDD development with EF 4.2 (or

  • 0

I have several concerns when trying to do DDD development with EF 4.2 (or EF 4.1) code first. I’ve done some extensive research but haven’t come up with concrete answers for my specific concerns. Here are my concerns:

  1. The domain cannot know about the persistence layer, or in other words the domain is completely separate from EF. However, to persist data to the database each entity must be attached to or added to the EF context. I know you are supposed to use factories to create instances of the aggregate roots so the factory could potentially register the created entity with the EF context. This appears to violate DDD rules since the factory is part of the domain and not part of the persistence layer. How should I go about creating and registering entities so that they correctly persist to the database when needed to?

  2. Should an aggregate entity be the one to create it’s child entities? What I mean is, if I have an Organization and that Organization has a collection of Employee entities, should Organization have a method such as CreateEmployee or AddEmployee? If not where does creating an Employee entity come in keeping in mind that the Organization aggregate root ‘owns’ every Employee entity.

  3. When working with EF code first, the IDs (in the form of identity columns in the database) of each entity are automatically handled and should generally never be changed by user code. Since DDD states that the domain is separate from persistence ignorance it seems like exposing the IDs is an odd thing to do in the domain because this implies that the domain should handle assigning unique IDs to newly created entities. Should I be concerned about exposing the ID properties of entities?

I realize these are kind of open ended design questions, but I am trying to do my best to stick to DDD design patterns while using EF as my persistence layer.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:52:33+00:00Added an answer on May 27, 2026 at 4:52 pm

    On 1: I’m not all that familiar with EF but using the code-first/convention based mapping approach, I’d assume it’s not too hard to map POCOs with getters and setters (even keeping that “DbContext with DbSet properties” class in another project shouldn’t be that hard). I would not consider the POCOs to be the Aggregate Root. Rather they represent “the state inside an aggregate you want to persist”. An example below:

    // This is what gets persisted
    public class TrainStationState {
      public Guid Id { get; set; }
      public string FullName { get; set; }
      public double Latitude { get; set; }
      public double Longitude { get; set; }
    
      // ... more state here
    }
    
    // This is what you work with
    public class TrainStation : IExpose<TrainStationState> { 
      TrainStationState _state;
    
      public TrainStation(TrainStationState state) {
        _state = state;
        //You can also copy into member variables
        //the state that's required to make this
        //object work (think memento pattern).
        //Alternatively you could have a parameter-less
        //constructor and an explicit method
        //to restore/install state.
      }
    
      TrainStationState IExpose.GetState() {
        return _state;
        //Again, nothing stopping you from
        //assembling this "state object"
        //manually.
      }
    
      public void IncludeInRoute(TrainRoute route) {
        route.AddStation(_state.Id, _state.Latitude, _state.Longitude);
      }
    }
    

    Now, with regard to aggregate life-cycle, there are two main scenario’s:

    1. Creating a new aggregate: You could use a factory, factory method, builder, constructor, … whatever fits your needs. When you need to persist the aggregate, query for its state and persist it (typically this code doesn’t reside inside your domain and is pretty generic).
    2. Retrieving an existing aggregate: You could use a repository, a dao, … whatever fits your needs. It’s important to understand that what you are retrieving from persistent storage is a state POCO, which you need to inject into a pristine aggregate (or use it to populate it’s private members). This all happens behind the repository/DAO facade. Don’t muddle your call-sites with this generic behavior.

    On 2: Several things come to mind. Here’s a list:

    1. Aggregate Roots are consistency boundaries. What consistency requirements do you see between an Organization and an Employee?
    2. Organization COULD act as a factory of Employee, without mutating the state of Organization.
    3. “Ownership” is not what aggregates are about.
    4. Aggregate Roots generally have methods that create entities within the aggregate. This makes sense because the roots are responsible for enforcing consistency within the aggregate.

    On 3: Assign identifiers from the outside, get over it, move on. That does not imply exposing them, though (only in the state POCO).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good people of SO, Today I have some serious concerns on my business layer
In my last question I posted some sample code on how I was trying
I am trying to start a process and capture the output, have come a
I have data split across several mysql tables to prevent data duplication but joining
I have a situation where I'm trying to consolidate several Javascript files into one,
I have several ASP:TextBox controls on a form (about 20). When the form loads,
I have several RequiredFieldValidators in an ASP.NET 1.1 web application that are firing on
I have several tables whose only unique data is a uniqueidentifier (a Guid) column.
We have several jobs that run concurrently that have to use the same config
I have several old 3.5in floppy disks that I would like to backup. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.