简体   繁体   中英

Akka.net message immutabilty

I've been working through the Akka.net bootcamp and ran across something that's been puzzling me based on things I've read about messages.

In the messaging documentation it states that messages should be immutable.

In unit 2 of the bootcamp, the Initialize Chart message appears to be mutable on a couple levels.

public class InitializeChart
{
    public InitializeChart(Dictionary<string, Series> initialSeries)
    {
        InitialSeries = initialSeries;
    }

    public Dictionary<string, Series> InitialSeries { get; private set; }
}

First off, the dictionary contents can be modified (add, remove, etc). Secondly the dictionary items, Series , are mutable.

I have 2 questions:

1) Am I in error with my interpretation of how an Akka.net message should be built? (this example is actually correct.)

2) Should Akka.net messages be immutable all the way down? For example, if this message was changed to:

public class InitializeChart
{
    public InitializeChart(Dictionary<string, Series> initialSeries)
    {
        InitialSeries = new ReadOnlyDictionary<string, Series>(initialSeries);
    }

    public IReadOnlyDictionary<string, Series> InitialSeries { get; private set; }
}

Would that satisfy the requirements of an Akka.net message, or should the Series elements also be immutable?

Yes, that message should be using a IReadOnlyDictionary in order to be immutable. You are correct. The element objects themselves should also consist of read-only properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM