mrpmorris / Fluxor

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

Question about classes verses records for simpler syntax #332

Closed david-randoll closed 2 years ago

david-randoll commented 2 years ago

Hello,

I like to have everything in one file for discoverability purposes. It seems using records makes the syntax a lot simpler. They both works but any reasons against just going with option 2?

Option 1:

public class Effects
{
  private readonly IWeatherForecastService WeatherForecastService;

  public Effects(IWeatherForecastService weatherForecastService)
  {
    WeatherForecastService = weatherForecastService;
  }

  [EffectMethod]
  public async Task HandleFetchDataAction(FetchDataAction action, IDispatcher dispatcher)
  {
    var forecasts = await WeatherForecastService.GetForecastAsync(DateTime.Now);
    dispatcher.Dispatch(new FetchDataResultAction(forecasts));
  }
}

Option 2:

public record Effects(IWeatherForecastService WeatherForecastService)
{
    [EffectMethod]
    public async Task HandlerAsync(FetchDataAction action, IDispatcher dispatcher)
    {
        var forecasts = await WeatherForecastService.GetForecastAsync(DateTime.Now);
        dispatcher.Dispatch(new FetchDataResultAction(forecasts));
    }
}

Similarly, both of the below work but option 2 is a lot simpler. Any reasons against putting the reducer directly within the action?

Option 1:

public class IncrementCounterAction
{
}
public class Reducers
{
    [ReducerMethod]
    public static CounterState Reducer(CounterState state, IncrementCounterAction action)
    {
        return new CounterState(clickCount: state.ClickCount + 1);
    }
}

Option 2:

public class IncrementCounterAction
{
    [ReducerMethod]
    public static CounterState Reducer(CounterState state, IncrementCounterAction action)
    {
        return new CounterState(clickCount: state.ClickCount + 1);
    }
}
mrpmorris commented 2 years ago

If you have multiple features then you could end up having them reference each other's actions.

I'd say to put the actions in the main store folder, and then your effects and reducers anywhere you want to, put them in your state record of you wish.

But they all work, so it's up to you.