Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Address within the Order entity should be a Value-Object instead of an Entity - Id for EF can be implemented as a shadow state primary key #4

@CESARDELATORRE

Description

@CESARDELATORRE

The "Address" within the "Order" entity should be a Value-Object instead of an entity, because an Address has no identity within that context, therefore it shouldn't be an Entity but a Value-Object (or even a ComplexType in EF).
https://github.com/dotnet/eShopOnContainers/blob/master/src/Services/Ordering/Ordering.Domain/Address.cs
The issue is that in EF Core still doesn't support ComplexTypes like the traditional EF does.

Implement a base ValueObject type that is hiding the Id with a "Shadow Primary Key"
In EF Core we can "hide" the ID of the entity by using a Shadow state property that us part of the database model but does not have a corresponding property in the CLR Entity class.
See:
http://www.talkingdotnet.com/use-shadow-properties-entity-framework-core/
https://docs.microsoft.com/en-us/ef/core/modeling/shadow-properties

Value-Oject persisted in the same Order's table "a la" ComplexType.
If we want the VO group of fields to be persisted within the same Order table, we'll need to modify the relationship and make it 1:1 explicitly so by default is not 1:many and persisted in another table..

A Value-Oject should be IMMUTABLE throughout its life.
Solution: Put Setters as Private..
One problem in implementing true Immutable Value-Objects in .NET is serialization to be done by Entity Framework. These mechanisms require a parameterless constructor, so the true Immutability of the object is violated.
Here is a post on a possible approach to solve this problem:
https://christianarg.wordpress.com/2010/09/10/c-net-immutable-properties-for-entity-framework-serializable-classes-etc/

VO Comparison operator must be done by comparing fields/properties and/or using hashes..

Other interesting posts about Value-Objects and .NET:
https://www.codeproject.com/Articles/1046193/Value-Object-Design-Pattern-in-Csharp
http://elegantcode.com/2009/06/07/generic-value-object/
http://grabbagoft.blogspot.com/2007/06/generic-value-object-equality.html

Please, reply to this post if you have a good solution in regards this issue and EF Core.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions