mrpmorris / Fluxor

Fluxor is a zero boilerplate Flux/Redux library for Microsoft .NET and Blazor.
MIT License
1.24k stars 141 forks source link

Question: does this library support using C# records for State? #276

Closed josephr5000 closed 2 years ago

josephr5000 commented 2 years ago

Can I do this:

[FeatureState] public record MyState(int balanceA= 0, int balanceB);

such that I could write a reducer:

[ReducerMethod] public static MyState ReduceBalanceChangeA(MyState state, BalanceChangeAAction action) =>
    state with { balanceA = state.balanceA + action.balanceChange };
mrpmorris commented 2 years ago

You either need to write it in its expanded form (like a class) so you can either add a parameterless constructor or a static method to create a default instance, or you could remove [FeatureState] and add a class

public class MyStateFeature : Feature { override the abstract method to create a default state + specify its name }

uhfath commented 2 years ago

Could you please elaborate a bit why we shouldn't use records? Is it because of #274 ?

uhfath commented 2 years ago

Sorry, my bad. I missed the part about "expanded form". So I guess it's safe to use a record like this?

[FeatureState]
public record AuthState : LoaderStateBase
{
    public bool IsAuthorized { get; init; }
    public string UserName { get; init; } = "none";
}

Considering that these defaults are suitable.

mrpmorris commented 2 years ago

Yes, you can also do this

[FeatureState]
public record AuthState(bool IsAuthorized, string UserName)
{
  private AuthState() => new AuthState(false, "None");
}