Question

I'm checking out Sharp Architecture's code. So far it's cool, but I'm having problems getting my head around how to implement DDD value objects in the framework (doesn't seem to be anything mentioning this in the code). I'm assuming the base Entity class and Repository base are to be used for entities only. Any ideas on how to implement value objects in the framework?

Was it helpful?

Solution

In Sharp Arch there is a class ValueObject in namespace SharpArch.Domain.DomainModel. This object inherits from BaseObject and overrides the == and != operators and the Equals() and GetHashCode() methods. The method overrides just calls the BaseObject versions of those two methods which in turn uses GetTypeSpecificSignatureProperties() method to get the properties to use in the equality comparison.

Bottom line is that Entity's equality is determined by

  1. Reference equality
  2. Same type?
  3. Id's are the same
  4. Comparison of all properties decorated with the [DomainSignature] attribute

For ValueObjects, the BaseObject's Equals method is used

  1. Reference equality
  2. Same type?
  3. Compare all public properties

This is a little bit simplified, I suggest you get the latest code from github and read through the code in the mentioned 3 classes yourself.

Edit: Regarding persistence, this SO question might help. Other than that, refer to the official NH and Fluent NH documentation

OTHER TIPS

Value objects are simple objects that don't require a base class. (The only reason entities have base classes is to provide equality based on the identity). Implementing a value object just means creating a class to represent a value from your domain. A lot of times value objects should be immutable and provide equality comparison methods to determine equality to other value objects of the same type. Take a look here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top