torhovland / blazor-redux

Connecting a Redux state store with Blazor.
Other
482 stars 48 forks source link

MediatR integration #1

Open StevenTCramer opened 6 years ago

StevenTCramer commented 6 years ago

I have been using Redux in React for a while now. In my C# code I consider a switch statement a code smell. I know they use them Redux but that is JavaScript.

I also use the MeditaR pattern from Jimmy Bogard on all my .net core web apps. I did a quick google search for MediatR Redux and found:

https://github.com/sstorie/MediatRedux/tree/master/src/MediatRedux

His concerns are the same as mine. also this would give a pipeline for extention simply using MediatRs abilities. Would like your thoughts.

Thanks for the great work.

panesofglass commented 6 years ago

I had a similar idea, but I think this is an area where you want your dependencies to be few and tiny. Adding Mediatr, Rx.Main, and AutoMapper quickly begin to grow the size of the app you have to send and run in the browser. Worth trying, but I fear the result is a bloated application dependency that slows everything down.

torhovland commented 6 years ago

@StevenTCramer I haven't used MediatR, so I can't really judge whether this is a good idea or not. But if it is, the MediatRedux docs aren't communicating it very convincingly. They argue that instead of switch-like code such as this:

        public static int Execute(int state, IAction action)
        {
            if(action is IncrementAction)
            {
                return state + 1;
            }

            if(action is DecrementAction)
            {
                return state - 1;
            }

            return state;
        }

we should be doing this:

public class IncrementCounter : ReduxAction<State> {}

public class IncrementCounterHandler : ReduxActionHandler<IncrementCounter, State>
{
    protected override void HandleAction(State state, IncrementCounter action)
    {
        state.Counter++;
    }
}

public class DecrementCounter : ReduxAction<State> {}

public class DecrementCounterHandler : ReduxActionHandler<DecrementCounter, State>
{
    protected override void HandleAction(State state, DecrementCounter action)
    {
        state.Counter--;
    }
}

Anyway, the switch-like reducers don't look too bad in a language like Elm, and with F# it is the same. Even with the pattern matching in C#, it is much nicer than the is/as casting we had before.

I need to look closer into MediatR, though.

torhovland commented 6 years ago

@StevenTCramer It looks like it should be possible to let the store send MediatR messages using a Redux middleware. In that case we could even keep the MediatR integration in a separate NuGet package if we wanted to. This isn't supported at the moment, but I'd be interested in having a look later.

psampaio commented 6 years ago

I think this would allow you to keep the main Redux concepts while taking advantage of the C# language benefits. And keeping a separate package makes perfect sense.

The main issue I see is with the composition of Reducers to build the new state. How do you compose the state on all the ReduxActionHandler<>?

panesofglass commented 6 years ago

@torhovland you may be able to implement this without Rx.Main if you implement your own IObservable+IObserver or ISubject.