Пример #1
0
        public IStoreConfiguration <TState> UseReducer <TReducerType, TSectionType>(Expression <StateSubSectionSelectionDelegate <TState, TSectionType> > sectionSelector)
            where TReducerType : IReducer <TSectionType>
        {
            Type reducerType = typeof(ReducerSection <TState, TReducerType, TSectionType>);

            IParameter[] parameters = new IParameter[]
            {
                ConstantParameter.Create <ITypeRequest>("subReducer", new TypeRequest(typeof(TReducerType))),
                ConstantParameter.Create <Expression <StateSubSectionSelectionDelegate <TReducerType, TSectionType> > >(nameof(sectionSelector), sectionSelector)
            };

            TypeRequest typeRequest = new TypeRequest(reducerType, parameters);

            m_reducers.Add(typeRequest);
            return(this);
        }
Пример #2
0
        public TStoreType CreateStore <TStoreType>() where TStoreType : IStore <TState>
        {
            if (m_activatorTypeRequest == null)
            {
                throw new MissingActivatorException();
            }

            List <IReducer <TState> > reducers    = new List <IReducer <TState> >();
            List <IMiddleware>        middlewares = new List <IMiddleware>();
            List <IParameter>         parameters  = new List <IParameter>();

            // Create activator
            IActivator activator = DefaultActivator.Get <IActivator>(m_activatorTypeRequest);

            parameters.Add(ConstantParameter.Create <IActivator>(nameof(activator), activator));

            // Logging
            ILog logger = activator.Get <ILog>(m_loggerTypeRequest);

            parameters.Add(new ConstantParameter("log", logger, typeof(ILog)));

            foreach (ITypeRequest singletonRequest in m_singletonTypeRequests)
            {
                object instance = activator.Get(singletonRequest, parameters);

                IParameter parameter = new ConstantParameter("", instance, singletonRequest.Type);

                parameters.Add(parameter);
            }

            foreach (ITypeRequest reducerTypeRequest in m_reducers)
            {
                IReducer <TState> reducer = activator.Get <IReducer <TState> >(reducerTypeRequest, parameters);
                reducers.Add(reducer);
            }

            ReducerMiddleware <TState> rootDispatcher = new ReducerMiddleware <TState>(reducers);

            ActionDispatchDelegate next = rootDispatcher.InvokeAsync;

            foreach (ITypeRequest middlewareTypeRequest in m_middleware)
            {
                IParameter          nextParameter      = ConstantParameter.Create <ActionDispatchDelegate>("next", next);
                IAbstractMiddleware abstractMiddleware = activator.Get <IAbstractMiddleware>(middlewareTypeRequest, parameters.Clone(nextParameter));
                switch (abstractMiddleware)
                {
                case IMiddleware middleware:
                    next = (p) => middleware.InvokeAsync(p);
                    break;

                case IMiddleware <TState> typedMiddleware:
                    next = (p) => typedMiddleware.InvokeAsync((ActionContext <TState>)p);
                    break;

                default:
                    throw new InvalidMiddlewareException(abstractMiddleware.GetType());
                }
            }

            IList <IParameter> storeParameters = parameters.Clone(
                ConstantParameter.Create <TState>("state", m_initialState),
                ConstantParameter.Create <ActionDispatchDelegate>("dispatch", next)
                );

            TypeRequest storeRequest = new TypeRequest(typeof(TStoreType));

            TStoreType store = DefaultActivator.Get <TStoreType>(storeRequest, storeParameters);

            return(store);
        }