Value objects in DDD?
jayamoorthi parasuraman
FSD|Azure|Cosmos, postgresql, docker,|AKS|DevOps|Dot.Net Core/.Net5/6/7/8 ||xunit,nunit, integration, graphQL,gRpc | EFCore|API |WCF| Angular/React |Microservices,DDD/TDD| Dapper | Sonar Mob: +919715783720/+6580537622
Value Objects is one of the component of DDD.
Identity is fundamental for entities. However, there are many objects and data items in a system that do not require an identity and identity tracking, such as value objects.
Let say Money value or DateTime or Value of other reference entiity as value object from aggregate root.
A value object can reference other entities. For example, in an application that generates a route that describes how to get from one point to another, that route would be a value object.
Important characteristics of value objects
There are two main characteristics for value objects:
Important to rememeber below the points
领英推荐
How do we create string type into Value object?
Let us consider EmailAddress need to do validate emailId logic from string into value object.
public sealed class EmailAddress
{
private readonly string _emailAddressValidation = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
+ @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
+ @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
private readonly string emailAddress;
public EmailAddress(string emailAddress)
{
this.emailAddress = emailAddress;
}
public static EmailAddress CreateEmailAddress(string emailAddress)
{
if (!new EmailAddressAttribute().IsValid(emailAddress))
throw new InvalidDataException(emailAddress);
//OR
if(!EmailIsValid(emailAddress))
throw new InvalidDataException(emailAddress);
return new EmailAddress(emailAddress);
}
public string GetEmailAddress()
{
return emailAddress;
}
public override string ToString()
{
return GetEmailAddress();
}
public class EmailAddressValidator : IEquatable<EmailAddress>, IEqualityComparer<EmailAddress>, IComparable
Conclusion:
There is no identity and Immutable for this value object, might have value to reference of another entity.