Class ValueObject<TModel>
Class that represent and identifies value objects on domains or services.
note
The value object will always be a model, but it will not always be an entity.
Inheritance
Implements
Namespace: StoneCo.Framework.Model
Assembly: cs.temp.dll.dll
Syntax
public abstract class ValueObject<TModel> : IEquatable<TModel>, IModel where TModel : ValueObject<TModel>
Type Parameters
Name | Description |
---|---|
TModel | The type of the model used in the instance of the value object. |
Methods
Equals(TModel)
Virtual method that indicates whether the current object is equal to another object of the same type.
In this comparison is taken the previously determined properties are used and not the hash code.
Declaration
public virtual bool Equals(TModel other)
Parameters
Type | Name | Description |
---|---|---|
TModel | other | An object to compare with this object. |
Returns
Type | Description |
---|---|
System.Boolean | True if the current object is equal to the other parameter; otherwise, false. If obj is null, the method returns false. |
Examples
Bar class used in this example. Run the RegisterProperty(Expression<Func<TModel, Object>>) sample first.
Sample to compare different codes, result is false.
public class Bar
{
public void Compare()
{
var foo = new Foo("code1", "Stone Co", "Company Stone Co");
var newFoo = new Foo("code2", "Stone Co", "Company Stone Co");
var result = foo.Equals(newFoo);
}
}
Sample to compare different codes, result is true.
public class Bar
{
public void Compare()
{
var foo = new Foo("code1", "Stone Co", "Company Stone Co");
var newFoo = new Foo("code1", "Stone", "Company Stone");
var result = foo.Equals(newFoo);
}
}
Equals(Object)
Override of the method that indicates whether the current object is equal to another object of the same type.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object to compare with this object. |
Returns
Type | Description |
---|---|
System.Boolean | True if the current object is equal to the other parameter; otherwise, false. If obj is null, the method returns false. |
Examples
Foo class used in this example.
Here is a sample of how to register properties.
public class Foo : ValueObject<Foo>
{
public Foo(string code, string name, string description)
: this()
{
Code = code;
Name = name;
Description = description;
}
internal Foo() => RegisterProperty(it => it.Code);
public string Code { get; private set; }
public string Name { get; private set; }
public string Description { get; private set; }
}
Bar class used in this example.
public class Bar
{
public Bar(string code, string name, string description)
{
Code = code;
Name = name;
Description = description;
}
public string Code { get; private set; }
public string Name { get; private set; }
public string Description { get; private set; }
}
FooBar class used in this example.
Here is a sample of how to compare.
public class FooBar
{
public void Compare()
{
var foo = new Foo("code", "Stone Co", "Company Stone Co");
var bar = new Bar("code", "Stone Co", "Company Stone Co");
var result = foo.Equals(bar);
}
}
Result value is false.
GetHashCode()
Override of the method that returns the hash code for this instance.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
System.Int32 | A new hash code. |
RegisterProperty(Expression<Func<TModel, Object>>)
Determines which properties should be used when comparing value objects.
warning
If the properties that are to be used in the comparison are not determined, all properties in the class will be used and this can directly impact the performance.
Declaration
protected void RegisterProperty(Expression<Func<TModel, object>> expression)
Parameters
Type | Name | Description |
---|---|---|
Expression<Func<TModel, System.Object>> | expression | Represents a strongly typed lambda expression as a data structure in the form of an expression tree. This class cannot be inherited. |
Examples
Foo class used in this example.
Here is a sample of how to register properties.
public class Foo : ValueObject<Foo>
{
public Foo(string code, string name, string description)
: this()
{
Code = code;
Name = name;
Description = description;
}
internal Foo() => RegisterProperty(it => it.Code);
public string Code { get; private set; }
public string Name { get; private set; }
public string Description { get; private set; }
}
Operators
Equality(ValueObject<TModel>, ValueObject<TModel>)
Custom the equal operator.
Declaration
public static bool operator ==(ValueObject<TModel> left, ValueObject<TModel> right)
Parameters
Type | Name | Description |
---|---|---|
ValueObject<TModel> | left | Value object from left. |
ValueObject<TModel> | right | Value object from right. |
Returns
Type | Description |
---|---|
System.Boolean | True if left value object equals to right, otherwise, false. |
Inequality(ValueObject<TModel>, ValueObject<TModel>)
Custom the not equal operator.
Declaration
public static bool operator !=(ValueObject<TModel> left, ValueObject<TModel> right)
Parameters
Type | Name | Description |
---|---|---|
ValueObject<TModel> | left | Value object from left. |
ValueObject<TModel> | right | Value object from right. |
Returns
Type | Description |
---|---|
System.Boolean | True if left value object not equals to right, otherwise, false. |