Exemple #1
0
        public static IThingCollectionBuilder AddThings(this IServiceCollection service,
                                                        Action <ThingOption>?options = null)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            var thingOption = new ThingOption();

            options?.Invoke(thingOption);

            service.AddSingleton(thingOption);

            service.TryAddSingleton(provider =>
            {
                var opt = provider.GetRequiredService <ThingOption>();
                return(new JsonSerializerOptions
                {
                    PropertyNamingPolicy = opt.PropertyNamingPolicy,
                    DictionaryKeyPolicy = opt.PropertyNamingPolicy,
                    PropertyNameCaseInsensitive = opt.IgnoreCase,
                    IgnoreNullValues = true
                });
            });

            service.AddSingleton <IWebSocketAction, RequestAction>();
            service.AddSingleton <IWebSocketAction, AddEventSubscription>();
            service.AddSingleton <IWebSocketAction, SetThingProperty>();

            service.AddScoped <ThingObserverResolver>();
            service.AddScoped(provider => provider.GetRequiredService <ThingObserverResolver>().Observer);

            service.AddSingleton(provider =>
            {
                var opt     = provider.GetRequiredService <ThingOption>();
                var actions = provider.GetRequiredService <IEnumerable <IWebSocketAction> >();

                return(actions.ToDictionary(
                           x => x.Action,
                           x => x,
                           opt.IgnoreCase ? StringComparer.InvariantCultureIgnoreCase : null));
            });

            var builder = new ThingCollectionBuilder(service);

            return(builder);
        }
        /// <summary>
        /// Add thing/.
        /// </summary>
        /// <param name="service"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">The <paramref name="service"/> if null this will throw <see cref="ArgumentNullException"/>.</exception>
        public static IThingCollectionBuilder AddThings(this IServiceCollection service, Action <ThingOption>?options = null)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            var thingOption = new ThingOption();

            options?.Invoke(thingOption);

            service.AddSingleton(thingOption);

            service.AddSingleton <IWebSocketAction, RequestAction>();
            service.AddSingleton <IWebSocketAction, AddEventSubscription>();
            service.AddSingleton <IWebSocketAction, SetThingPropertyWebSocketAction>();

            service.TryAddTransient <IThingContextFactory, ThingContextFactory>();
            service.TryAddTransient <IThingResponseBuilder, ThingResponseBuilder>();
            service.TryAddTransient <IEventBuilder, EventBuilder>();
            service.TryAddTransient <IActionBuilder, ActionBuilder>();
            service.TryAddTransient <IPropertyBuilder, PropertyBuilder>();

            service.TryAddSingleton <IPropertyFactory, PropertyFactory>();

            service.TryAddSingleton <SystemTextJson>();
            service.TryAddSingleton <IJsonConvert>(provider => provider.GetRequiredService <SystemTextJson>());
            service.TryAddSingleton <ThingObserver>();

            service.TryAddSingleton <IJsonSchemaValidationFactory, SystemTexJsonSchemaValidationFactory>();
            service.TryAddSingleton <IJsonConvertibleFactory, SystemTexJsonConvertibleFactory>();
            service.TryAddTransient <IConvertibleFactory, ConvertibleFactory>();
            service.AddHostedService <MDnsRegisterHostedService>();

            var builder = new ThingCollectionBuilder(service);

            return(builder);
        }