Пример #1
0
 public ActionIntercept(ModuleBuilder moduleBuilder, ThingOption option)
 {
     _option        = option;
     _moduleBuilder = moduleBuilder;
     Actions        = option.IgnoreCase ? new Dictionary <string, ActionContext>(StringComparer.InvariantCultureIgnoreCase)
         : new Dictionary <string, ActionContext>();
 }
Пример #2
0
 public ActionInterceptFactory(ThingOption option)
 {
     var assemblyName = new AssemblyName("ActionAssembly");
     var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
     var moduleBuilder = assemblyBuilder.DefineDynamicModule("ActionModule");
     _intercept = new ActionIntercept(moduleBuilder, option);
 }
        internal static JsonSerializerSettings ToJsonSerializerSettings(this ThingOption option)
        {
            if (s_serializer == null)
            {
                lock (s_locker)
                {
                    if (s_serializer == null)
                    {
                        s_serializer = new JsonSerializerSettings
                        {
                            Formatting       = Formatting.None,
                            ContractResolver = option.PropertyNamingPolicy == JsonNamingPolicy.CamelCase
                                ? new CamelCasePropertyNamesContractResolver()
                                : new DefaultContractResolver()
                        };

                        foreach (var converter in s_converters)
                        {
                            s_serializer.Converters.Add(converter);
                        }

                        s_converters.Clear();
                    }
                }
            }

            return(s_serializer !);
        }
Пример #4
0
        /// <inheritdoc />
        public ThingContext Create(Thing thing, ThingOption option)
        {
            var thingType = thing.GetType();

            _response
            .SetThing(thing)
            .SetThingOption(option);

            _event
            .SetThing(thing)
            .SetThingOption(option)
            .SetThingType(thingType);

            _property
            .SetThing(thing)
            .SetThingOption(option);

            _action
            .SetThing(thing)
            .SetThingOption(option)
            .SetThingType(thingType);

            VisitEvent(thingType);
            VisitProperty(thingType);
            VisitAction(thingType);

            return(new ThingContext(
                       _response.Build(),
                       _event.Build(),
                       _action.Build(),
                       _property.Build()));
        }
Пример #5
0
 public ThingObserver(IJsonConvert convert,
                      ThingOption option,
                      ILogger <ThingObserver> logger)
 {
     _convert = convert ?? throw new ArgumentNullException(nameof(convert));
     _option  = option ?? throw new ArgumentNullException(nameof(option));
     _logger  = logger ?? throw new ArgumentNullException(nameof(logger));
 }
        public EventInterceptFactory(Thing thing, ThingOption options)
        {
            var thingType = thing.GetType();
            var builder   = Factory.CreateTypeBuilder($"{thingType.Name}EventBinder", thingType.Name,
                                                      null, TypeAttributes.AutoClass | TypeAttributes.Class | TypeAttributes.Public);

            _intercept = new EventIntercept(builder, options);
        }
Пример #7
0
        public EventIntercept(TypeBuilder builder, ThingOption options)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));
            _builder = builder ?? throw new ArgumentNullException(nameof(builder));

            Events = options.IgnoreCase ? new Dictionary <string, EventCollection>(StringComparer.OrdinalIgnoreCase)
                : new Dictionary <string, EventCollection>();
        }
Пример #8
0
 /// <inheritdoc />
 public IActionBuilder SetThingOption(ThingOption option)
 {
     _option                = option;
     _actions               = new Dictionary <string, ActionCollection>(option.IgnoreCase ? StringComparer.OrdinalIgnoreCase : null);
     _jsonConvertibles      = new Dictionary <string, IJsonConvertible>(option.IgnoreCase ? StringComparer.OrdinalIgnoreCase : null);
     _jsonSchemaValidations = new Dictionary <string, IJsonSchemaValidation>(option.IgnoreCase ? StringComparer.OrdinalIgnoreCase : null);
     _convertibles          = new Dictionary <string, IConvertible?>(option.IgnoreCase ? StringComparer.OrdinalIgnoreCase : null);
     return(this);
 }
Пример #9
0
        public MDnsRegisterTest()
        {
            _fixture = new Fixture();
            _server  = Substitute.For <IServer>();
            _option  = new ThingOption();
            _logger  = Substitute.For <ILogger <MDnsRegisterHostedService> >();

            _things = new List <Thing>();
        }
Пример #10
0
        public void CreateWithActions()
        {
            var thing  = new ActionThing();
            var option = new ThingOption();

            var context = _factory.Create(thing, option);

            context.Should().NotBeNull();

            _property
            .Received(1)
            .Build();

            _action
            .Received(1)
            .Build();

            _response
            .Received(1)
            .Build();

            _event
            .Received(1)
            .Build();

            _property
            .DidNotReceive()
            .Add(Arg.Any <PropertyInfo>(), Arg.Any <JsonSchema>());

            _response
            .DidNotReceive()
            .Add(Arg.Any <PropertyInfo>(), Arg.Any <ThingPropertyAttribute>(), Arg.Any <JsonSchema>());

            _event
            .DidNotReceive()
            .Add(Arg.Any <EventInfo>(), Arg.Any <ThingEventAttribute>());

            _response
            .DidNotReceive()
            .Add(Arg.Any <EventInfo>(), Arg.Any <ThingEventAttribute>());

            _action
            .Received(2)
            .Add(Arg.Any <MethodInfo>(), Arg.Any <ThingActionAttribute>());

            _action
            .Received(2)
            .Add(Arg.Any <ParameterInfo>(), Arg.Any <JsonSchema>());

            _response
            .Received(2)
            .Add(Arg.Any <MethodInfo>(), Arg.Any <ThingActionAttribute>());

            _response
            .Received(2)
            .Add(Arg.Any <ParameterInfo>(), Arg.Any <ThingParameterAttribute>(), Arg.Any <JsonSchema>());
        }
 public ThingResponseBuilderTest()
 {
     _builder = new ThingResponseBuilder();
     _fixture = new Fixture();
     _option  = new ThingOption
     {
         WriteIndented        = true,
         PropertyNamingPolicy = JsonNamingPolicy.CamelCase
     };
 }
Пример #12
0
        /// <inheritdoc />
        public IThingResponseBuilder SetThingOption(ThingOption option)
        {
            _option = option;

            if (_thing != null)
            {
                _thingName = _option.PropertyNamingPolicy.ConvertName(_thing.Name);
            }

            return(this);
        }
Пример #13
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);
        }
Пример #14
0
        /// <summary>
        /// Initialize a new instance of <see cref="MDnsRegisterHostedService"/>.
        /// </summary>
        /// <param name="server">The <see cref="IServer"/> with feature.</param>
        /// <param name="options">The <see cref="ThingOption"/>.</param>
        /// <param name="things">All things for in the application. It's use for determinate the server name if isn't set.</param>
        /// <param name="logger">The <see cref="ILogger{TCategoryName}"/></param>
        public MDnsRegisterHostedService(IServer server,
                                         ThingOption options,
                                         IEnumerable <Thing> things,
                                         ILogger <MDnsRegisterHostedService> logger)
        {
            _discovery = new ServiceDiscovery();
            _profiles  = new List <ServiceProfile>();
            _server    = server ?? throw new ArgumentNullException(nameof(server));
            _option    = options;
            _logger    = logger;
            _name      = options.ServerName;

            if (string.IsNullOrEmpty(_name))
            {
                _name = things.First().Name;
            }
        }
        /// <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);
        }
 /// <summary>
 /// Add <see cref="JsonConverter"/>
 /// </summary>
 /// <param name="option">The <see cref="ThingOption"/>.</param>
 /// <param name="convert">The <see cref="JsonConverter"/> to be added.</param>
 /// <returns>The same instance passed in option.</returns>
 public static ThingOption AddJsonConvert(this ThingOption option, JsonConverter convert)
 {
     s_converters.AddLast(convert);
     return(option);
 }
Пример #17
0
 public PropertiesIntercept(ThingOption option)
 {
     _option    = option ?? throw new ArgumentNullException(nameof(option));
     Properties = option.IgnoreCase ? new Dictionary <string, Property>(StringComparer.InvariantCultureIgnoreCase)
         : new Dictionary <string, Property>();
 }
Пример #18
0
 private static float <Generate> m__0(ThingOption x)
 {
     return(x.weight);
 }
 public PropertiesInterceptFactory(Thing thing, ThingOption option)
 {
     _thing     = thing ?? throw new ArgumentNullException(nameof(thing));
     _intercept = new PropertiesIntercept(option);
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="option"></param>
 public NewtonsoftJsonConvert(ThingOption option)
 {
     _settings = option?.ToJsonSerializerSettings() ?? throw new ArgumentNullException(nameof(option));
 }
Пример #21
0
 /// <summary>
 /// Initialize a new instance of <see cref="SystemTextJson"/>.
 /// </summary>
 /// <param name="options">The <see cref="ThingOption"/>.</param>
 public SystemTextJson(ThingOption options)
 {
     _options = options ?? throw new ArgumentNullException(nameof(options));
 }
 /// <inheritdoc />
 public IPropertyBuilder SetThingOption(ThingOption option)
 {
     _option     = option;
     _properties = new Dictionary <string, IThingProperty>(option.IgnoreCase ? StringComparer.OrdinalIgnoreCase : null);
     return(this);
 }
Пример #23
0
 public EventInterceptTest()
 {
     _fixture = new Fixture();
     _options = new ThingOption();
 }
Пример #24
0
 /// <inheritdoc />
 public IEventBuilder SetThingOption(ThingOption option)
 {
     _option = option;
     _events = new Dictionary <string, EventCollection>(option.IgnoreCase ? StringComparer.OrdinalIgnoreCase : null);
     return(this);
 }