I have a Unit Class that act as a Entity (Aggregate Root) and this class must have reference to Province and City. I have put Province and city in a class called Region (Aggregate Root) and this class is Entity too. every province has its own cities and their names and relations are permanent and can't be changed.
so until now I have two aggregate root, Unit and Region. but in Unit I need to have reference to province and city, but in DDD term I just can reference to root of my aggregate and in this case it is province and I can't have reference to child of province !
how to solve this problem and how to have reference from unit to province and city ?
By the help of plalx I found that my assumption about DDD was wrong. in this model and models like that I shouldn't hidden cities in Aggregate and they should be both Aggregate Root in seperate Aggregate.
Related
Hello I have 2 entities that share some properties in common, studying and teacher, and then I thought about making a generalization
but I was in doubt as to how I can represent this in my domain.
should i have the person table as a normal entity? or as a root aggreggrete?
or wouldn't you need to represent that person entity and just the two students or teacher entities as aggregrate root ?
I have doubts about how to model my domain in a situation of generalization.
But specifically I have doubts as to whether I should put my person class as aggregrate root and everything that is in common between the entities is in the person class
Some misunderstanding about DDD here. An aggregate root should never reference an other aggregate root direcly. The only way for an aggregate root to reference an other is with its ID. I'm not talking about a foreign key here. Keep in mind that each aggregates could be in a separate database. So sharing properties between aggregate roots is strongly discouraged.
The first thing you should do is establish your bounded contexts. Maybe students and teachers are from two differents bounded contexts or maybe they should be spitted in multiples entites. Keep in mind that its ok to duplicate data across bounded contexts.
Lots of examples like order and order lines makes sense, like:
Order is an AR that contains OrderLines
Customer is an AR that contains Orders.
Question is, what is the AR that contains Customer?
I guess it can be something like "shop".
So, shop.AddCustomer(customer)...
but, how to get shop?
If it's an AR (entity) it has an id, so shop.GetById(shopId). If I only have one shop, how does this work with persistence?
Should I have a table (shops) with one line?
Shop is an in-memory object with a collection of Customers?
You got that wrong there. Aggregates do not contain other aggregates! They can only reference them by ID.
An aggregate is a group of entities and value objects that are closely related. The aggregate forms a consistency boundary around them. The Aggregate Root is the root entity in that aggregate that is globally addressable. So in your example with Order and OrderLines, Order could indeed be the AR.
Customer on the other hand, would only reference Orders by ID if it is a separate aggregate.
To retrieve an aggregate, you typically use a Repository. You load an aggregate through the repository by specifying the ID of the aggregate, or some other suitable search parameter.
In DDD, in an aggregate root of Person with a Value Object of Address, mapping that address to a database table is simple: just embed the attributes of the Address object into the record. But what about when the Person has a List, where the count can vary? Do we create a separate table that stores all our Addresses (thereby imposing some quasi-identity on each one), and each row with an FK back to the Person to which it belongs?
There is a good example of object-relational impedance mismatch. What you can do is have a layer super-type where persistence concerns such as an id field lives. Therefore, from your persistence layer's point of view, the VO is an entity, but still modeled as a VO in the domain.
You can read more about the above here.
I wanna model Order and Product concept with the help of DDD but I wonder how to deal with it.
Suppose you have Order class that has OrderItem class as it's child and Order is Aggregate Root, and Order class has list of OrderItems and every OrderItem has a reference to Product Class. Product is Aggregate Root of course.
I mean some thing like this :
public class Order{
...
public list<OrderItem> OrderItems {get;set;}
}
public class OrderItems{
...
public int Qty {get; set;}
public Product {get; set;}
}
public class Product{
...
}
but as far as i know I can't have a reference from child of Order Aggregate to Product Aggregate. how to deal with this ?
Tnx in forward.
At the page 202 of book Architecting application for the enterprise you could see an image. There are various bounded context, two of them are really simialr to what you need. Order is an aggregate root, having Order Detail as its child. Order Detail in turn has a relationship with Product that is the aggegate root of an aggregate in a different bounded context.
So the first thought is that Product belongs to Order aggregate.
However, next you might find out that use-case exist to treat products
outside of orders-for example for the catalog of products. This makes
up for another aggergate rooted in Product.
So... yes, a child of an aggregate can hold a reference to another aggregate root (even in different bounded context).
Is this a right choice? The right answer in a case like that is always: depends.
On what? On business rules, and how you have to handle the aggregate that will be "swallowed" (Product) by another bigger aggregate (Order).
I have read many about how to configure many to many realtionships with EF Code First and fluent configuration. But I couldn't understand why I should create an extra entity for the junction table. Lets assume that I have the following entities:
Department
----------
Id
Name
Material
----------
Id
Name
Now I want to keep records of materials for each department. So I need
DepartmentMaterial
-------------------
DepartmentId
MaterialId
Quantity
What I have read so far I need 3 entities: Department, Material and DepartmentMaterial. So actually I am mapping each table to a corresponding entity which was not I intended to do when I started to learn about DDD. I assumed that EF is going to map that junction table automatically and also queries about material quantities can be done over Department.
Hence, is it possible in EF Code First to configure fluently such a relation without an extra entity?
The junction table can stay out of the conceptual model (class model) if it's only got two foreign keys (DepartmentId, MaterialId). You want to record some data about the association. That's OK, the quantity belongs there. A department has quantity x of material y. Saying that a department has a quantity is meaningless.
This is not against DDD principles. The association class is a first-class citizen in the class model. It could even have its own behavior (like limiting quantities for certain department/material combinations).
This means that your model will not contain a many-to-many association but Department 1-n DepartmentMaterial n-1 Material. And you'll have to query materials of departments through the junction table. This is a very common situation. I hardly ever see pure junction tables in real life. They're collector's items.
Example query:
var query = from d in db.Departments
from dm in d.DepartmentMaterials
select new { Department = d, Materials = dm.Select(x => x.Material)};