Created
February 10, 2020 15:36
-
-
Save aspnetde/9f0283bc895e8f2d963399ab63a314af to your computer and use it in GitHub Desktop.
How could https://thomasbandt.com/model-view-update look in C#?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MvuCsharp | |
{ | |
public class Cmd | |
{ | |
public static readonly Cmd None = new Cmd(); | |
public static Cmd OfMsg(IMessage msg) => new Cmd(); // TODO | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace MvuCsharp | |
{ | |
public class Model | |
{ | |
public int Count { get; } | |
public Model(int count) | |
{ | |
Count = count; | |
} | |
} | |
public interface IMessage | |
{ | |
} | |
public class Increment : IMessage | |
{ | |
public int Value { get; } | |
public Increment(int value) | |
{ | |
Value = value; | |
} | |
} | |
public class IncrementRandom : IMessage | |
{ | |
} | |
public class CmdIncrementRandom : IMessage | |
{ | |
} | |
public static class DemoProgram | |
{ | |
private static readonly Random _random = new Random(); | |
private static Increment IncrementRandom() => new Increment(_random.Next(10, 100)); | |
public static ValueTuple<Model, Cmd> Init() => (new Model(0), Cmd.None); | |
public static ValueTuple<Model, Cmd> Update(IMessage msg, Model model) | |
{ | |
return msg switch | |
{ | |
Increment increment => (new Model(increment.Value), Cmd.None), | |
IncrementRandom _ => (model, Cmd.OfMsg(new CmdIncrementRandom())), | |
CmdIncrementRandom _ => (model, Cmd.OfMsg(IncrementRandom())), | |
_ => throw new NotImplementedException() | |
}; | |
} | |
public static void View() | |
{ | |
// TODO | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment