DDD Value Object: How to persist entity objects without tons of SQL joins?

entity framework core value object
ddd collection of entities
ef core value object configuration
power use of value objects in ddd
value object vs entity
value object best practices
value object ddd
aggregate vs value object

Obviously I am wrong in saying that DDD is similar to EAV/CR in usefulness, but the only difference I see so far is physical tables build for each entity with lots of joins rather than three tables and lots of joins.

This must be due to my lack of DDD understanding. How do you physically store these objects to the database without significant joins and complication when importing data? I know you can simply create objects that feed in to your repository, but it's difficult to train tools like Microsoft Sql Server Integration Server to use your custom C# objects and framework. Maybe that should be my question, how do you use your DDD ASP.NET C# framework with Microsoft SQL Server Integration Services and Report Services? LOL.

In an EAV/CR database, we can setup a single Person table with different classes depending on the type of person: Vendor, Customer, Buyer, Representative, Company, Janitor, etc. Three tables, a few joins, attributes are always string with validation before we insert, just like ModelValidation in MVC where the object accepts any value but won't persist until it's valid.

In a standard relational model, we used to create a table for each type of entity, mixing in redundant data types like City.

Using Domain Driven Design, we use objects to represent each type of entity, nested objects for each type of ValueObject, and more nested objects still as necessary. In my understanding, this results in a table for each kind of entity and a table for each kind of information set (value object). With all these tables, I see a lot of joins. We also end up creating a physical table for each new contact type. Obviously there is a better way, so I must be incorrect in how I persist objects to a database.

My Vendor looks like this:

public class Vendor {
    public int vendorID {get; set;}
    public Address vAddress {get; set;}
    public Representative vRep {get;set;}
    public Buyer vBuyer {get; set;}
}

My Buyer:

public class Buyer {
   public int buyerID {get; set;}
   public Address bAddress {get; set;}
   public Email bEmail {get; set;}
   public Phone bPhone {get; set;}
   public Phone bFax (get; set;}
}

Do we really reference things like Vendor.vBuyer.bPhone.pAreaCode? I would think we would reference and store Vendor.BuyerPhoneNumber, and build the objects almost like aliases to these parts: Vendor.Address1, Vendor.Address2, Vendor.BuyerPhoneNumber ... etc.

The real answer is to match your SQL normalization strategy to your objects. If you have lots of duplicate addresses and you need to associate them together, then normalize the data to a separate table, thus creating the need for the value object.

DDD and relational databases – the value object dilemma, Value objects are one of the basic building blocks of object domain how you can persist object and still keep all the benefits of value objects. With Hibernate annotations you can use one-to-many join table without the entity on the is very important for MySQL or SQL Server users where the entire leg� Per DDD patterns, you should encapsulate domain behavior and rules within the entity class itself, so it can control invariants, validations, and rules when accessing any collection. Therefore, it is not a good practice in DDD to allow public access to collections of child entities or value objects.

You could serialize your objects to xml and save it to an xml column in you Sql Server. After all, you are trying to represent a hierarchical data structure, and that's where xml excels.

DDD Value Objects With Entity Framework Core, They are not persisted on their own; they belong to an entity. The creation of a value object is done through the constructor, and once created, or they'll be placed in a separate table, given identity, and pulled with a join. I see a lot of examples given of a DateOfBirth property being its own class. You'll� … but right now, the only way to map many-to-many is to manually create a joining entity. Another way to consider this, is that from a DDD perspective, you won’t be relying on EF to automatically manage the joins for you, which is probably a good thing. Value conversions. This relates to correctly mapping a .NET type to a SQL type.

Domain-driven design proponents often recommend keeping the data model as close to the object model as possible, but it isn't an ironclad rule.

You can still use a EAV/CR database design if you create mappings in your object-relational mapping layer to transform (project) your data into your objects.

Deciding how to design your objects and provide access to child values is really a separate question that you have to address on a case-by-case basis. Vendor.BuyerPhoneNumber or Vendor.vBuyer.bPhone.pAreaCode? The answer always depends, because it's rooted in your specific requirements.

Implementing the infrastructure persistence layer with Entity , When you use relational databases such as SQL Server, Oracle, or PostgreSQL Infrastructure in Entity Framework Core from a DDD perspective in DDD to allow public access to collections of child entities or value objects. to create the queries and joins they need for the presentation layer without the� In the April 2009 issue of MSDN Magazine ("Persistence Patterns") I presented some common patterns that you will encounter when using some sort of Object/Relational Mapping (O/RM) technology to persist business entity objects. I think it's unlikely that you or your team will be writing your own O/RM tooling from scratch, but these patterns are

One of the best ways to store Domain Objects is actually a document database. It works beautifully because the transactional boundary of the document matches perfectly the consistency boundary of the Aggregate Root. You don't have to worry about JOINs, or eager/lazy loading issues. That's not strictly necessary, though, if you apply CQRS (which I've written about below).

The downside is often with querying. If you wish to query directly the persisted data behind your Domain Objects, you can get into knots. However that is a complexity that CQRS aims to solve for you, where you have different parts of your application doing the queries than the parts loading/validating/storing Domain Objects.

You might have a complex "Command" implementation that loads Domain Objects, invokes the behaviour on them (remembering that Domain Objects must have behaviour and encapsulate their data, or else risk becoming "anaemic"), before saving them and optionally even publishing events about what happened.

You might then use those events to update some other "read store", though you don't have to. The point is you have a totally different vertical slice implemented in your application that doesn't have to bother with that complex object model/ORM business, and instead goes straight to the data, loads exactly what it needs and returns it for displaying to the user.

CQRS is not hard, and is not complex. All it does is instruct you to have separate code for:

  1. Handling commands (which mutate state and therefore need the business rules/invariants involved).
  2. Executing queries (which do not mutate state and therefore don't need all the complex business rules/invariants involved, and so can "just" go get the data in an efficient way.

Representing a collection as a Value Object � Enterprise , Or, in other words, can you represent a collection as a Value Object? this question in the discussion to my DDD in Practice Pluralsight course. has a lot of benefits, the most important of which is that Value Objects as entities if we want to persist those items in a relational data store. Join(";", cityList. UPDATE (Nov 5 2018): While you’re here to become a better C# developer, I strongly recommend you to watch my Python tutorial on YouTube. Python is super-hot these days. It’s the number one language employers are looking for and gives you 4x more job opportunities than C#. As part of my new ASP.NET Core course, I’ve been …

Persisting Value Objects, Examples below are based on my Ruby port of the DDD sample app. Serialize the Value Object and Store in Field in Entity's Table Databases such as Postgres and SQL Server provide native format for such approaches. and a NoSQL system as a persistence strategy without thinking through the� I am working at Entity Framework these days. NET Core Web API 3 11 3) Asynchronous Calls, Data-Transfer-Objects & Automapper in . My issue is that when updating the entity along with the value objects, the entity with the parent value object get updated but the child value object didn't. PackageName. e.

Solving the Detached Many-to-Many Problem with the Entity , Join For Free The problem with detached entities is that the data context has no way made to an object graph, without fetching the data from the data store and Here's the Entity Model which is generated from the above SQL schema: Description; //set a new dummy value (with a date/time so we can� You can use it for parent-child relationships in which a child entity can’t exist without its parent entity. That’s the case in the example that I use in this post. An Item entity can’t exist without a PurchaseOrder entity. So, any Item entity that’s not associated to a PurchaseOrder entity, needs to be removed.

Doctrine ORM and DDD aggregates — Matthias Noback, He describes different ways of persisting value objects when using But since there are lots of projects using Doctrine ORM, with on storing aggregates ( entities and value objects) with Doctrine ORM. Designing without the ORM in mind Whenever you still need the value object, you just create it again� Secondly, ORMs save us lots of time by removing the necessity to write tons of boilerplate code. Just imagine creating four separate SQL queries (select, update, insert, delete) for each entity in your domain model. And that’s only for simple CRUD scenarios, the situation gets worse if you try to persist 1-to-many or many-to-many relationships.