You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current ValueObject implementation has three drawbacks:
It uses reflection to get the properties,
Unless you know the code it's hard to modify equality, e.g. if equality should be case-insensitive and
the declaration is somewhat awkward with with the generic parameter that has to be the same as the class.
The ValueObject proposed here or here (Microsoft) seems to solve all those issues and seems to me the most commonly used pattern in newer projects.
Example from Microsoft:
publicabstractclassValueObject{protectedstaticboolEqualOperator(ValueObjectleft,ValueObjectright){if(ReferenceEquals(left,null)^ReferenceEquals(right,null)){returnfalse;}returnReferenceEquals(left,null)||left.Equals(right);}protectedstaticboolNotEqualOperator(ValueObjectleft,ValueObjectright){return!(EqualOperator(left,right));}protectedabstractIEnumerable<object>GetAtomicValues();publicoverrideboolEquals(objectobj){if(obj==null||obj.GetType()!=GetType()){returnfalse;}ValueObjectother=(ValueObject)obj;IEnumerator<object>thisValues=GetAtomicValues().GetEnumerator();IEnumerator<object>otherValues=other.GetAtomicValues().GetEnumerator();while(thisValues.MoveNext()&&otherValues.MoveNext()){if(ReferenceEquals(thisValues.Current,null)^ReferenceEquals(otherValues.Current,null)){returnfalse;}if(thisValues.Current!=null&&!thisValues.Current.Equals(otherValues.Current)){returnfalse;}}return!thisValues.MoveNext()&&!otherValues.MoveNext();}publicoverrideintGetHashCode(){returnGetAtomicValues().Select(x =>x!=null?x.GetHashCode():0).Aggregate((x,y)=>x^y);}// Other utilility methods}
Usage:
publicclassAddress:ValueObject{publicStringStreet{get;privateset;}publicStringCity{get;privateset;}publicStringState{get;privateset;}publicStringCountry{get;privateset;}publicStringZipCode{get;privateset;}privateAddress(){}publicAddress(stringstreet,stringcity,stringstate,stringcountry,stringzipcode){Street=street;City=city;State=state;Country=country;ZipCode=zipcode;}protectedoverrideIEnumerable<object>GetAtomicValues(){// Using a yield return statement to return each element one at a timeyieldreturnStreet;yieldreturnCity;yieldreturnState;yieldreturnCountry;yieldreturnZipCode;}}
I'd suggest using this implementation instead.
The text was updated successfully, but these errors were encountered:
We can not replace the current one since it is a big breaking change, but we may have two base classes (one is existing generic, and other one is this new one). What do you think? I've just created an issue for it: aspnetboilerplate/aspnetboilerplate#4304
The current ValueObject implementation has three drawbacks:
The ValueObject proposed here or here (Microsoft) seems to solve all those issues and seems to me the most commonly used pattern in newer projects.
Example from Microsoft:
Usage:
I'd suggest using this implementation instead.
The text was updated successfully, but these errors were encountered: