Value Object and View Model

I am working on a solution using DDD for architecture. I have a property in my ViewModel that points to a ValueObject, the view model also implements the INotifyPropertyChanged interface. The ValueObject value will change when the user enters data on the front side. The problem I am facing is an object of value, which is supposed to be immutable. How can I get around this problem? Thank you in advance.

+3
source share
1 answer

If you can edit something, then there must be a modified container for an immutable value. Therefore, your view model should act directly in a mutable container, and not on an immutable value.

An integer is an example of such an immutable value object: the type Int32has no members that allow you to change the state of the object. You can replace an integer rather than change it. Thus, the view model for the whole will look like this:

public MutableIntegerViewModel
{
    private readonly mutableInteger;

    public MutableIntegerViewModel(MutableInteger mutableInteger)
    {
        this.mutableInteger = mutableInteger;
    }

    public string DisplayText
    {
        get
        {
            return this.mutableInteger.Value.ToString(
                CultureInfo.CurrentCulture);
        }
        set
        {
           this.mutableInteger.Value = 
               Int32.Parse(value, CultureInfo.CurrentCulture);
        }
    }
}

Where MutableIntegeris it only:

public class MutableInteger
{
   public int Value { get; set; }
}

I forgot about error handling and change notification here, but hopefully you get this idea.

, Customer FirstName a LastName. , .

+2

Source: https://habr.com/ru/post/1737982/


All Articles