Ejemplo n.º 1
0
 public static IStatorBuilder ScanTypes(this IStatorBuilder builder)
 {
     return(builder
            .FindStates()
            .FindActions()
            .FindReducers()
            .FindActionHandlers()
            );
 }
Ejemplo n.º 2
0
        public static IStatorBuilder AddAsyncOperations(this IStatorBuilder builder)
        {
            //Async Operations
            builder.AddTypes(new[] { typeof(StatusUpdated <>) });

            // Async Operation's Errors
            builder.Services.TryAddSingleton <IActionHandler <AsyncError.Occured>, ReducerHandler <AsyncError.State, AsyncError.Occured> >();
            builder.Services.TryAddSingleton <IReducer <AsyncError.Occured, AsyncError.State>, AsyncError.Reducers>();
            builder.Services.TryAddSingleton <IInitialState <AsyncError.State>, AsyncError.InitialState>();
            builder.Services.TryAddSingleton <IState <AsyncError.State>, Internal.State <AsyncError.State> >();
            return(builder);
        }
 public static IStatorBuilder AddReduxDevTools(this IStatorBuilder builder)
 {
     builder.Services.AddSingleton(sp =>
     {
         var states     = new List <Type>();
         var iStateType = typeof(IState <>);
         foreach (var state in builder.States.OrderBy(x => x.Name))
         {
             var x = iStateType.MakeGenericType(state);
             states.Add(x);
         }
         return(new DevToolsStateCollection(states));
     });
     builder.Services.AddSingleton <ReduxDevToolsInteropInitializer>();
     builder.Services.AddSingleton <ReduxDevToolsInterop>();
     builder.Services.AddSingleton <IBeforeReducerHook>(sp => sp.GetService <ReduxDevToolsInterop>());
     builder.Services.AddSingleton <IAfterReducerHook>(sp => sp.GetService <ReduxDevToolsInterop>());
     return(builder);
 }
Ejemplo n.º 4
0
        public static IServiceCollection Apply(this IStatorBuilder builder)
        {
            // Extract types
            builder.ScanTypes();

            // Scan
            builder.Services.Scan(s => s
                                  .AddTypes(builder.All)

                                  // Equivalent to: AddSingleton<IInitialState<TState>, Implementation>();
                                  .AddClasses(classes => classes.AssignableTo(typeof(IInitialState <>)))
                                  .AsImplementedInterfaces()
                                  .WithSingletonLifetime()

                                  // Equivalent to: AddSingleton<IBeforeInterceptorHook, Implementation>();
                                  .AddClasses(classes => classes.AssignableTo(typeof(IBeforeInterceptorHook)))
                                  .AsImplementedInterfaces()
                                  .WithSingletonLifetime()
                                  // Equivalent to: AddSingleton<IAfterInterceptorHook, Implementation>();
                                  .AddClasses(classes => classes.AssignableTo(typeof(IAfterInterceptorHook)))
                                  .AsImplementedInterfaces()
                                  .WithSingletonLifetime()

                                  // Equivalent to: AddSingleton<IBeforeAfterEffectHook, Implementation>();
                                  .AddClasses(classes => classes.AssignableTo(typeof(IBeforeAfterEffectHook)))
                                  .AsImplementedInterfaces()
                                  .WithSingletonLifetime()
                                  // Equivalent to: AddSingleton<IAfterAfterEffectHook, Implementation>();
                                  .AddClasses(classes => classes.AssignableTo(typeof(IAfterAfterEffectHook)))
                                  .AsImplementedInterfaces()
                                  .WithSingletonLifetime()

                                  // Equivalent to: AddSingleton<IBeforeActionHook, Implementation>();
                                  .AddClasses(classes => classes.AssignableTo(typeof(IBeforeActionHook)))
                                  .AsImplementedInterfaces()
                                  .WithSingletonLifetime()
                                  // Equivalent to: AddSingleton<IAfterActionHook, Implementation>();
                                  .AddClasses(classes => classes.AssignableTo(typeof(IAfterActionHook)))
                                  .AsImplementedInterfaces()
                                  .WithSingletonLifetime()

                                  // Equivalent to: AddSingleton<IBeforeReducerHook, Implementation>();
                                  .AddClasses(classes => classes.AssignableTo(typeof(IBeforeReducerHook)))
                                  .AsImplementedInterfaces()
                                  .WithSingletonLifetime()
                                  // Equivalent to: AddSingleton<IAfterReducerHook, Implementation>();
                                  .AddClasses(classes => classes.AssignableTo(typeof(IAfterReducerHook)))
                                  .AsImplementedInterfaces()
                                  .WithSingletonLifetime()

                                  // Equivalent to: AddSingleton<IInterceptor<TState>, Implementation>();
                                  .AddClasses(classes => classes.AssignableTo(typeof(IInterceptor <>)))
                                  .AsImplementedInterfaces()
                                  .WithSingletonLifetime()

                                  // Equivalent to: AddSingleton<IActionAfterEffects<TState>, Implementation>();
                                  .AddClasses(classes => classes.AssignableTo(typeof(IAfterEffects <>)))
                                  .AsImplementedInterfaces()
                                  .WithSingletonLifetime()
                                  );

            // Register States
            foreach (var state in builder.States)
            {
                Console.WriteLine($"state: {state.FullName}");

                // Equivalent to: AddSingleton<IState<TState>, State<TState>>();
                var stateServiceType        = typeof(IState <>).MakeGenericType(state);
                var stateImplementationType = typeof(State <>).MakeGenericType(state);
                builder.Services.AddSingleton(stateServiceType, stateImplementationType);
            }

            // Register Reducers and their respective IActionHandler
            var iReducerType   = typeof(IReducer <,>);
            var reducerHandler = typeof(ReducerHandler <,>);
            var handlerType    = typeof(IActionHandler <>);

            foreach (var reducer in builder.Reducers)
            {
                Console.WriteLine($"reducer: {reducer.FullName}");
                var interfaces = reducer.GetInterfaces()
                                 .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == iReducerType);
                foreach (var @interface in interfaces)
                {
                    // Equivalent to: AddSingleton<IActionHandler<TAction>, ReducerHandler<TState, TAction>>
                    var actionType = @interface.GenericTypeArguments[0];
                    var stateType  = @interface.GenericTypeArguments[1];
                    var iActionHandlerServiceType        = handlerType.MakeGenericType(actionType);
                    var reducerHandlerImplementationType = reducerHandler.MakeGenericType(stateType, actionType);
                    builder.Services.AddSingleton(iActionHandlerServiceType, reducerHandlerImplementationType);

                    // Equivalent to: AddSingleton<IReducer<TState, TAction>, Reducer>();
                    builder.Services.AddSingleton(@interface, reducer);

                    Console.WriteLine($"- AddSingleton<{iActionHandlerServiceType.GetStatorName()}, {reducerHandlerImplementationType.GetStatorName()}>()");
                    Console.WriteLine($"- AddSingleton<{@interface.GetStatorName()}, {reducer.GetStatorName()}>()");
                }
            }
            return(builder.Services);
        }