Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling entity updates from a mapped object

I have my code first, SQL data models (using EF Core 1.1) that are used to model my schema/tables. However I also have domain objects which are partial or full mapped versions of these SQL data models, in essence they sort of have the same shape as the SQL data models.

Now I would like to know what is the best way to handle cascading updates when you have complex objects being altered outside of the context of its tracked context. When you consider that all my domain operations do not take place on the tracked entity, they take place on the domain object.

In Short, This is what I am trying to achieve.

1) Read entity from database.

2) Map entity to domain object.

3) Apply updates to domain object.

4) Map domain object back to entity.

5) Apply database update on mapped entity which results in the entity and its associated relative entities to be updated.

By the way the entities and domain object have the typical many to one relationships that one might run into. What is the best way to go about doing this?

like image 214
Lutando Avatar asked Apr 05 '17 16:04

Lutando


1 Answers

What is the best way to go about doing this?

I think the best way to go about this is to avoid the problem in the first place by using an framework that is flexible enough to allow mapping the domain objects directly to the database without too many compromise in order to avoid having to model an explicit persistence model in code.

in essence they sort of have the same shape as the SQL data models

If you think about it means you would have the same impedance mismatch between your domain model (object model) and relational DB model than between your domain model and the explicit persistence model.

Still, there is an elegant way to perform the mapping which Vaughn Vernon describes in Modeling Aggregates with DDD and Entity Framework. Basically, it boils down to store state in explicit state objects which are bags of getters/setters that are encapsulated and maintained by real domain objects. These state objects are then mapped with EF.

E.g. taken from the above linked article

public class Product {
  public Product(
      TenantId tenantId,
      ProductId productId,
      ProductOwnerId productOwnerId,
      string name,
      string description) {
    State = new ProductState();
    State.ProductKey = tenantId.Id + ":" + productId.Id;
    State.ProductOwnerId = productOwnerId;
    State.Name = name;
    State.Description = description;
    State.BacklogItems = new List<ProductBacklogItemState>();
  }

  internal Product(ProductState state) {
    State = state;
  }
  ...
}
like image 112
plalx Avatar answered Oct 12 '22 10:10

plalx