Skip to main content

Value Object : Value Object should have parametrized constructor

Medium
DDD

If you don't have parametrized constructor or equivalent, you have setter. You shouldn't have a setter.

Value Objects are classes annotated with ValueObject.

Examples

Example 1:

Positive

Correct implementation following the practice.

namespace Practices.DDD.ValueObject.ParametrizedConstructor
{
[ValueObject]
internal class VODefaultValue
{
public VODefaultValue(int myValue, int secondValue = 0)
{
MyValue = myValue;
}

public int MyValue { get; }
public int SecondValue { get; }
}




[ValueObject]
public class ValueObjectWithoutParametrizedConstructor
{
public ValueObjectWithoutParametrizedConstructor()
{

}

public int SomeProperty { get; set; }
}
}

Negative

Incorrect implementation that violates the practice.

namespace Practices.DDD.ValueObject.ParametrizedConstructor
{
[ValueObject]
internal class VODefaultValue
{
public VODefaultValue(int myValue, int secondValue = 0)
{
MyValue = myValue;
}

public int MyValue { get; }
public int SecondValue { get; }
}




[ValueObject]
public class ValueObjectWithoutParametrizedConstructor
{
public ValueObjectWithoutParametrizedConstructor()
{

}

public int SomeProperty { get; set; }
}
}