Elm Architecture (MVU) in C# and Deep Copy

4.5k Views Asked by At

I'm looking into the possibility of implementing an MVU pattern in a Xamarin app using C#. This mostly seems to hinge on the immutability of the model being provided to the view and the Update function that will keep producing a new model whenever the model needs to be changed.

Is it possible in C# to properly achieve this without implementing some sort of deep copy if the model might contain reference types?

I'm aware that frameworks like Fabulous exist for doing this in F# but I'm just trying to understand if C#'s lack of support for immutability means it isn't possibly to implement a pure form of MVU without resorting to implementing some sort of deep copy operation?

3

There are 3 best solutions below

0
On

Comet is likely the library you're looking for. It will also be the foundation of the MVU pattern MAUI will support, and that @sevenate was referring to.

0
On

Seems like Microsoft will answer to this questions with its upcoming Multi-platform App UI (MAUI) framework soon:

readonly State<int> count = 0;

[Body]
View body() => new StackLayout
{
    new Label("Welcome to .NET MAUI!"),
    new Button(
        () => $"You clicked {count} times.",
        () => count.Value ++)
    )
};

More info:

0
On

There's a library for doing full MVU in C# with Xamarin.Forms: https://github.com/shirshov/laconic (I'm the author)