Jeff
Jeff

Reputation: 4423

C# - Enforce Child Object Immutability in Struct

I am trying to create an immutable struct. The problem is, it contains mutable objects inside. Is it possible to make those immutable (without modifying their class)? For example (https://dotnetfiddle.net/KTiTzB):

public class IntHolder
{
    public int X
    {
        get;
        set;
    }
}

public struct ImmutableStruct
{
    public int ImmutableInt
    {
        get;
        private set;
    }

    public IntHolder MyIntHolder
    {
        get;
        private set;
    }

    public ImmutableStruct(int immutableInt, IntHolder myIntHolder): this ()
    {
        ImmutableInt = immutableInt;
        MyIntHolder = myIntHolder;
    }
}

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Immutability");
        IntHolder intHolder = new IntHolder();
        intHolder.X = 40;
        Console.WriteLine(intHolder.X == 40); // TRUE
        ImmutableStruct immStruct = new ImmutableStruct(10, intHolder);
        Console.WriteLine(immStruct.ImmutableInt == 10); // TRUE
        // immStruct.ImmutableInt = 4; // THIS DOESN'T WORK, AS EXPECTED. Hurray!
        // immStruct.MyIntHolder = new IntHolder(3); // ALSO DOESN'T WORK, GOOD!
        immStruct.MyIntHolder.X = 4; // how can I prevent this from working?
    }
}

Is there a way to make the IntHolder member immutable while leaving the IntHolder class unmodified?

Upvotes: 2

Views: 163

Answers (1)

Dongdong
Dongdong

Reputation: 2498

It's actually a big question: how to handle nested objects in immutable data?

  1. https://softwareengineering.stackexchange.com/questions/279580/how-to-make-complex-objects-immutable

  2. https://medium.freecodecamp.org/handling-state-in-react-four-immutable-approaches-to-consider-d1f5c00249d5

  3. https://redux.js.org/recipes/structuringreducers/immutableupdatepatterns

In my opinion, It's worthy to create immutable children models to make sure the whole object immutable.

See comments in code below:

public class IntHolder
{
    public int X
    {
        get;
        set;
    }

    public ImmutableIntHolder ToImmutable()//convert itself to ImmutableIntHolder 
    {
        return new ImmutableIntHolder(X);
    }
}
public class ImmutableIntHolder
{
    public ImmutableIntHolder(int x)
    {
        X = x;
    }
    public int X
    {
        get;
        private set;
    }

    public IntHolder ToIntHolder() //convert it back to mutable IntHolder 
    {
        return new IntHolder()
        {
            X = this.X
        };
    }
}
public struct ImmutableStruct
{
    public int ImmutableInt
    {
        get;
        private set;
    }

    public ImmutableIntHolder IntHolder //use ImmutableIntHolder instead
    {
        get;
        private set;
    }

    public ImmutableStruct(int immutableInt, IntHolder myIntHolder) : this()
    {
        ImmutableInt = immutableInt;
        IntHolder = myIntHolder.ToImmutable(); // convert to immutable
    }
}

another option is:

public class IntHolder
{
    public int X
    {
        get;
        set;
    }
}


public class ImmutableStruct //changed to class
{
    public int ImmutableInt
    {
        get;
        private set;
    }

    public ImmutableIntHolder IntHolder
    {
        get;
        private set;
    }

    public ImmutableStruct(int immutableInt, IntHolder myIntHolder) //: this()
    {
        ImmutableInt = immutableInt;
        IntHolder = new ImmutableIntHolder(myIntHolder); // convert here.
    }

    public class ImmutableIntHolder
    {
        public ImmutableIntHolder(IntHolder intHolder)
        {
            //map all properties
            X = intHolder.X;
        }
        public int X
        {
            get;
            private set;
        }
    }
}

Upvotes: 1

Related Questions