/// <summary>
        /// Scan Assemblies for Handlers.
        /// </summary>
        /// <param name="aServices"></param>
        /// <param name="aOptions"></param>
        /// <param name="aCallingAssembly">The calling assembly</param>
        private static void EnsureMediator(IServiceCollection aServices, BlazorStateOptions aOptions)
        {
            ServiceDescriptor mediatorServiceDescriptor = aServices.FirstOrDefault(
                aServiceDescriptor => aServiceDescriptor.ServiceType == typeof(IMediator));

            if (mediatorServiceDescriptor == null)
            {
                aServices.AddMediatR(aOptions.Assemblies.ToArray());
            }
        }
示例#2
0
        /// <summary>
        /// Register BlazorState services based on the aConfigure options
        /// </summary>
        /// <param name="aServiceCollection"></param>
        /// <param name="aConfigureBlazorStateOptionsAction"></param>
        /// <returns></returns>
        /// <example></example>
        /// <remarks>The order of registration matters.
        /// If the user wants to change they can configure themselves vs using this extension</remarks>
        public static IServiceCollection AddBlazorState
        (
            this IServiceCollection aServiceCollection,
            Action <BlazorStateOptions> aConfigureBlazorStateOptionsAction = null
        )
        {
            ServiceDescriptor flagServiceDescriptor = aServiceCollection.FirstOrDefault
                                                      (
                aServiceDescriptor => aServiceDescriptor.ServiceType == typeof(BlazorHostingLocation)
                                                      );

            if (flagServiceDescriptor == null)
            {
                var blazorStateOptions = new BlazorStateOptions();
                aConfigureBlazorStateOptionsAction?.Invoke(blazorStateOptions);

                EnsureLogger(aServiceCollection);
                EnsureHttpClient(aServiceCollection);
                EnsureMediator(aServiceCollection, blazorStateOptions);
                EnsureStates(aServiceCollection, blazorStateOptions);

                aServiceCollection.AddScoped <BlazorHostingLocation>();
                aServiceCollection.AddScoped <JsonRequestHandler>();
                aServiceCollection.AddScoped <Subscriptions>();
                aServiceCollection.AddScoped(typeof(IRequestPostProcessor <,>), typeof(RenderSubscriptionsPostProcessor <,>));
                aServiceCollection.AddScoped <IStore, Store>();
                aServiceCollection.AddSingleton(blazorStateOptions);

                if (blazorStateOptions.UseCloneStateBehavior)
                {
                    aServiceCollection.AddScoped(typeof(IPipelineBehavior <,>), typeof(CloneStateBehavior <,>));
                }
                if (blazorStateOptions.UseReduxDevToolsBehavior)
                {
                    aServiceCollection.AddScoped(typeof(IRequestPostProcessor <,>), typeof(ReduxDevToolsPostProcessor <,>));
                    aServiceCollection.AddScoped <ReduxDevToolsInterop>();

                    aServiceCollection.AddTransient <IRequestHandler <CommitRequest, Unit>, CommitHandler>();
                    aServiceCollection.AddTransient <IRequestHandler <JumpToStateRequest, Unit>, JumpToStateHandler>();
                    aServiceCollection.AddTransient <IRequestHandler <StartRequest, Unit>, StartHandler>();
                    aServiceCollection.AddScoped(aServiceProvider => (IReduxDevToolsStore)aServiceProvider.GetService <IStore>());
                }
                if (blazorStateOptions.UseRouting)
                {
                    aServiceCollection.AddScoped <RouteManager>();
                    aServiceCollection.AddScoped <RouteState>();

                    aServiceCollection.AddTransient <IRequestHandler <ChangeRouteAction, Unit>, ChangeRouteHandler>();
                    aServiceCollection.AddTransient <IRequestHandler <InitializeRouteAction, Unit>, InitializeRouteHandler>();
                }
            }
            return(aServiceCollection);
        }
示例#3
0
        public Store
        (
            ILogger <Store> aLogger,
            IServiceProvider aServiceProvider,
            BlazorStateOptions aBlazorStateOptions
        )
        {
            Logger                = aLogger;
            ServiceProvider       = aServiceProvider;
            JsonSerializerOptions = aBlazorStateOptions.JsonSerializerOptions;

            using (Logger.BeginScope(new Dictionary <string, object> {
                [nameof(Guid)] = Guid
            }))
            {
                Logger.LogInformation($"{GetType().Name}: constructor: {nameof(Guid)}:{Guid}");
                States = new Dictionary <string, IState>();
            }
        }
示例#4
0
        private static void EnsureStates(IServiceCollection aServiceCollection, BlazorStateOptions aBlazorStateOptions)
        {
            foreach (Assembly assembly in aBlazorStateOptions.Assemblies)
            {
                IEnumerable <Type> types = assembly.GetTypes().Where
                                           (
                    aType =>
                    !aType.IsAbstract &&
                    !aType.IsInterface &&
                    aType.BaseType != null &&
                    aType.BaseType.IsGenericType &&
                    aType.BaseType.GetGenericTypeDefinition() == typeof(State <>)
                                           );

                foreach (Type type in types)
                {
                    if (!aServiceCollection.Any(aServiceDescriptor => aServiceDescriptor.ServiceType == type))
                    {
                        aServiceCollection.AddTransient(type);
                    }
                }
            }
        }