Exemplo n.º 1
0
        public static void AddEventServer(IEventServer eventServer)
        {
            lock (serviceLock)
            {
                var exposedTypes = Discovery.GetTypesFromAttribute(typeof(ServiceExposedAttribute));
                foreach (var type in exposedTypes)
                {
                    if (type.IsClass)
                    {
                        if (TypeAnalyzer.GetTypeDetail(type).Interfaces.Any(x => x == typeof(IEvent)))
                        {
                            var interfaceStack = ProviderLayers.GetProviderInterfaceStack();
                            var hasHandler     = Discovery.HasImplementationType(TypeAnalyzer.GetGenericType(typeof(IEventHandler <>), type), interfaceStack, interfaceStack.Length - 1);
                            if (hasHandler)
                            {
                                if (eventClients.Keys.Contains(type))
                                {
                                    throw new InvalidOperationException($"Cannot create loopback. Event Client already registered for type {type.GetNiceName()}");
                                }
                                if (!eventServerTypes.Contains(type))
                                {
                                    eventServerTypes.Add(type);
                                }
                                eventServer.RegisterEventType(type);
                            }
                        }
                    }
                }

                eventServer.SetHandler(HandleRemoteEventDispatchAsync);
                eventServers.Add(eventServer);
                eventServer.Open();
            }
        }
Exemplo n.º 2
0
        public static void AddQueryServer(IQueryServer queryServer)
        {
            lock (serviceLock)
            {
                var exposedTypes = Discovery.GetTypesFromAttribute(typeof(ServiceExposedAttribute));
                foreach (var type in exposedTypes)
                {
                    if (type.IsInterface && TypeAnalyzer.GetTypeDetail(type).Interfaces.Any(x => x == typeof(IBaseProvider)))
                    {
                        var interfaceStack    = ProviderLayers.GetProviderInterfaceStack();
                        var hasImplementation = Discovery.HasImplementationType(type, interfaceStack, interfaceStack.Length - 1);
                        if (hasImplementation)
                        {
                            if (queryClients.Keys.Contains(type))
                            {
                                throw new InvalidOperationException($"Cannot create loopback. Query Client already registered for type {type.GetNiceName()}");
                            }
                            if (!queryServerTypes.Contains(type))
                            {
                                queryServerTypes.Add(type);
                            }
                            queryServer.RegisterInterfaceType(type);
                        }
                    }
                }

                queryServer.SetHandler(HandleRemoteQueryCallAsync);
                queryServers.Add(queryServer);
                queryServer.Open();
            }
        }
Exemplo n.º 3
0
        public void ManyLayers()
        {
            var topLayerProvider = Discovery.GetImplementationTypes(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(), 0);

            Assert.AreEqual(1, topLayerProvider.Count);
            Assert.IsTrue(topLayerProvider.Contains(typeof(TestRuleProvider)));

            var ruleProvider = Discovery.GetImplementationTypes(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                                ProviderLayers.GetProviderInterfaceIndex(typeof(IRuleProvider)));

            Assert.AreEqual(1, ruleProvider.Count);
            Assert.IsTrue(ruleProvider.Contains(typeof(TestRuleProvider)));

            var cacheProvider = Discovery.GetImplementationTypes(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                                 ProviderLayers.GetProviderInterfaceIndex(typeof(ICacheProvider)));

            Assert.AreEqual(1, cacheProvider.Count);
            Assert.IsTrue(cacheProvider.Contains(typeof(TestCacheProvider)));

            var encryptionProvider = Discovery.GetImplementationTypes(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                                      ProviderLayers.GetProviderInterfaceIndex(typeof(IEncryptionProvider)));

            Assert.AreEqual(1, encryptionProvider.Count);
            Assert.IsTrue(encryptionProvider.Contains(typeof(TestEncryptionProvider)));

            var compressionProvider = Discovery.GetImplementationTypes(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                                       ProviderLayers.GetProviderInterfaceIndex(typeof(ICompressionProvider)));

            Assert.AreEqual(1, compressionProvider.Count);
            Assert.IsTrue(compressionProvider.Contains(typeof(TestCompressionProvider)));

            var dualbaseProvider = Discovery.GetImplementationTypes(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                                    ProviderLayers.GetProviderInterfaceIndex(typeof(IDualBaseProvider)));

            Assert.AreEqual(1, dualbaseProvider.Count);
            Assert.IsTrue(dualbaseProvider.Contains(typeof(TestDualBaseProvider)));

            var provider = Discovery.GetImplementationTypes(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                            ProviderLayers.GetProviderInterfaceIndex(typeof(IBaseProvider)));

            Assert.AreEqual(1, provider.Count);
            Assert.IsTrue(provider.Contains(typeof(TestProvider)));
        }
Exemplo n.º 4
0
        private static Task HandleEventAsync(IEvent @event, Type eventType, bool throwError)
        {
            Type interfaceType = TypeAnalyzer.GetGenericType(iEventHandlerType, eventType);

            var providerType = Discovery.GetImplementationType(interfaceType, ProviderLayers.GetProviderInterfaceStack(), 0, throwError);

            if (providerType == null)
            {
                return(Task.CompletedTask);
            }
            var method = TypeAnalyzer.GetMethodDetail(providerType, nameof(IEventHandler <IEvent> .Handle), new Type[] { eventType });

            var provider = Instantiator.GetSingleInstance(providerType);

            var task = (Task)method.Caller(provider, new object[] { @event });

            LogMessage(eventType, @event);

            return(task);
        }
Exemplo n.º 5
0
        private static Task HandleCommandAsync(ICommand command, Type commandType, bool throwError)
        {
            var interfaceType = TypeAnalyzer.GetGenericType(iCommandHandlerType, commandType);

            var providerType = Discovery.GetImplementationType(interfaceType, ProviderLayers.GetProviderInterfaceStack(), 0, throwError);

            if (providerType == null)
            {
                return(Task.CompletedTask);
            }
            var method = TypeAnalyzer.GetMethodDetail(providerType, nameof(ICommandHandler <ICommand> .Handle), new Type[] { commandType });

            var provider = Instantiator.GetSingleInstance(providerType);

            var task = (Task)method.Caller(provider, new object[] { command });

            LogMessage(commandType, command);

            return(task);
        }
Exemplo n.º 6
0
        public void HasLayers()
        {
            var topLayerProvider = Discovery.HasImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(), 0);

            Assert.IsTrue(topLayerProvider);

            var ruleProvider = Discovery.HasImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                               ProviderLayers.GetProviderInterfaceIndex(typeof(IRuleProvider)));

            Assert.IsTrue(ruleProvider);

            var cacheProvider = Discovery.HasImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                                ProviderLayers.GetProviderInterfaceIndex(typeof(ICacheProvider)));

            Assert.IsTrue(cacheProvider);

            var encryptionProvider = Discovery.HasImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                                     ProviderLayers.GetProviderInterfaceIndex(typeof(IEncryptionProvider)));

            Assert.IsTrue(encryptionProvider);

            var compressionProvider = Discovery.HasImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                                      ProviderLayers.GetProviderInterfaceIndex(typeof(ICompressionProvider)));

            Assert.IsTrue(compressionProvider);

            var dualbaseProvider = Discovery.HasImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                                   ProviderLayers.GetProviderInterfaceIndex(typeof(IDualBaseProvider)));

            Assert.IsTrue(dualbaseProvider);

            var provider = Discovery.HasImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                           ProviderLayers.GetProviderInterfaceIndex(typeof(IBaseProvider)));

            Assert.IsTrue(provider);
        }
Exemplo n.º 7
0
        public void Layers()
        {
            var topLayerProvider = Discovery.GetImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(), 0);

            Assert.AreEqual(typeof(TestRuleProvider), topLayerProvider);

            var ruleProvider = Discovery.GetImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                               ProviderLayers.GetProviderInterfaceIndex(typeof(IRuleProvider)));

            Assert.AreEqual(typeof(TestRuleProvider), ruleProvider);

            var cacheProvider = Discovery.GetImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                                ProviderLayers.GetProviderInterfaceIndex(typeof(ICacheProvider)));

            Assert.AreEqual(typeof(TestCacheProvider), cacheProvider);

            var encryptionProvider = Discovery.GetImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                                     ProviderLayers.GetProviderInterfaceIndex(typeof(IEncryptionProvider)));

            Assert.AreEqual(typeof(TestEncryptionProvider), encryptionProvider);

            var compressionProvider = Discovery.GetImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                                      ProviderLayers.GetProviderInterfaceIndex(typeof(ICompressionProvider)));

            Assert.AreEqual(typeof(TestCompressionProvider), compressionProvider);

            var dualbaseProvider = Discovery.GetImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                                   ProviderLayers.GetProviderInterfaceIndex(typeof(IDualBaseProvider)));

            Assert.AreEqual(typeof(TestDualBaseProvider), dualbaseProvider);

            var provider = Discovery.GetImplementationType(typeof(ITestProvider), ProviderLayers.GetProviderInterfaceStack(),
                                                           ProviderLayers.GetProviderInterfaceIndex(typeof(IBaseProvider)));

            Assert.AreEqual(typeof(TestProvider), provider);
        }