Beispiel #1
0
        private MapperConfiguration ResolveMapperConfiguration(Type converterType)
        {
            NullGuard.NotNull(converterType, nameof(converterType));

            lock (SyncRoot)
            {
                if (MapperConfigurations.TryGetValue(converterType, out MapperConfiguration mapperConfiguration))
                {
                    return(mapperConfiguration);
                }

                mapperConfiguration = new MapperConfiguration(Initialize);
                MapperConfigurations.Add(converterType, mapperConfiguration);
                return(mapperConfiguration);
            }
        }
        protected Exception Handle(AggregateException aggregateException, Func <Exception, IIntranetExceptionBuilder> intranetExceptionBuilder)
        {
            NullGuard.NotNull(aggregateException, nameof(aggregateException))
            .NotNull(intranetExceptionBuilder, nameof(intranetExceptionBuilder));

            Exception innerException = null;

            aggregateException.Handle(exception =>
            {
                innerException = exception;
                return(true);
            });

            IntranetExceptionBase intranetException = innerException as IntranetExceptionBase;

            return(intranetException ?? intranetExceptionBuilder(innerException).Build());
        }
        private static IEnumerable <TypeInfo> GetImplementingClassTypeInfos(Assembly assembly, TypeInfo handlerInterfaceTypeInfo)
        {
            NullGuard.NotNull(assembly, nameof(assembly))
            .NotNull(handlerInterfaceTypeInfo, nameof(handlerInterfaceTypeInfo));

            return(assembly.ExportedTypes
                   .Select(exportedType => exportedType.GetTypeInfo())
                   .Where(typeInfo =>
            {
                if (typeInfo.IsClass == false || typeInfo.IsAbstract)
                {
                    return false;
                }

                return GetInterfaceTypeInfos(typeInfo).Contains(handlerInterfaceTypeInfo);
            })
                   .ToArray());
        }
        public async Task PublishAsync <TEvent>(TEvent e) where TEvent : class, IEvent
        {
            NullGuard.NotNull(e, nameof(e));

            IList <Task> handleTaskCollection = new List <Task>();

            foreach (KeyValuePair <IEventHandler, int> item in this)
            {
                IEventHandler <TEvent> eventHandler = item.Key as IEventHandler <TEvent>;
                if (eventHandler == null)
                {
                    continue;
                }

                handleTaskCollection.Add(eventHandler.HandleAsync(e));
            }

            await Task.WhenAll(handleTaskCollection);
        }
Beispiel #5
0
        public TTarget Convert <TSource, TTarget>(TSource source)
        {
            NullGuard.NotNull(source, nameof(source));

            try
            {
                return(Mapper.Map <TSource, TTarget>(source, opt =>
                {
                    foreach (KeyValuePair <string, object> state in StateDictionary)
                    {
                        opt.Items[state.Key] = state.Value;
                    }
                }));
            }
            catch (AutoMapperMappingException ex)
            {
                throw ex.InnerException ?? ex;
            }
        }
        private static IServiceCollection AddHandlers(this IServiceCollection serviceCollection, Assembly assembly, TypeInfo handlerInterfaceTypeInfo)
        {
            NullGuard.NotNull(serviceCollection, nameof(serviceCollection))
            .NotNull(assembly, nameof(assembly))
            .NotNull(handlerInterfaceTypeInfo, nameof(handlerInterfaceTypeInfo));

            foreach (TypeInfo implementingClassTypeInfo in GetImplementingClassTypeInfos(assembly, handlerInterfaceTypeInfo))
            {
                TypeInfo[] interfaceTypeInfoArray = GetInterfaceTypeInfos(implementingClassTypeInfo)
                                                    .Where(interfaceTypeInfo => interfaceTypeInfo == handlerInterfaceTypeInfo || GetInterfaceTypeInfos(interfaceTypeInfo).Contains(handlerInterfaceTypeInfo))
                                                    .ToArray();

                foreach (TypeInfo interfaceTypeInfo in interfaceTypeInfoArray)
                {
                    serviceCollection.AddTransient(interfaceTypeInfo.AsType(), implementingClassTypeInfo.AsType());
                }
            }

            return(serviceCollection);
        }
        public Task <TResult> PublishAsync <TCommand, TResult>(TCommand command) where TCommand : ICommand
        {
            NullGuard.NotNull(command, nameof(command));

            return(Task.Run(async() =>
            {
                try
                {
                    ICommandHandler <TCommand, TResult> commandHandler = _commandHandlers.OfType <ICommandHandler <TCommand, TResult> >().SingleOrDefault();
                    if (commandHandler == null)
                    {
                        throw new IntranetExceptionBuilder(ErrorCode.NoCommandHandlerSupportingCommandWithResultType, command.GetType().Name, typeof(TResult).Name).Build();
                    }

                    using (TransactionScope scope = new TransactionScope(commandHandler.TransactionScopeOption, commandHandler.TransactionOptions, TransactionScopeAsyncFlowOption.Enabled))
                    {
                        TResult result = await commandHandler.ExecuteAsync(command);

                        scope.Complete();

                        return result;
                    }
                }
                catch (IntranetExceptionBase)
                {
                    throw;
                }
                catch (AggregateException aggregateException)
                {
                    throw Handle(aggregateException, innerException => new IntranetExceptionBuilder(ErrorCode.ErrorWhilePublishingCommandWithResultType, command.GetType().Name, typeof(TResult).Name, innerException.Message).WithInnerException(innerException));
                }
                catch (Exception ex)
                {
                    throw new IntranetExceptionBuilder(ErrorCode.ErrorWhilePublishingCommandWithResultType, command.GetType().Name, typeof(TResult).Name, ex.Message)
                    .WithInnerException(ex)
                    .Build();
                }
            }));
        }
        private static IntranetExceptionBase Build(Type exceptionType, Func <Type, ConstructorInfo> constructorGetter, params object[] argumentCollection)
        {
            NullGuard.NotNull(exceptionType, nameof(exceptionType))
            .NotNull(constructorGetter, nameof(constructorGetter))
            .NotNull(argumentCollection, nameof(argumentCollection));

            ConstructorInfo constructorInfo = constructorGetter(exceptionType);

            if (constructorInfo == null)
            {
                throw new MissingMethodException($"Unable to find a suitable constructor on the type named '{exceptionType.Name}'.", exceptionType.Name);
            }

            try
            {
                return((IntranetExceptionBase)constructorInfo.Invoke(argumentCollection.ToArray()));
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException ?? ex;
            }
        }
        public CommandBus(IEnumerable <ICommandHandler> commandHandlers)
        {
            NullGuard.NotNull(commandHandlers, nameof(commandHandlers));

            _commandHandlers = commandHandlers;
        }
        public void AddSubscriber(IEventHandler eventHandler)
        {
            NullGuard.NotNull(eventHandler, nameof(eventHandler));

            TryAdd(eventHandler, eventHandler.GetHashCode());
        }
        public NullableDateTimeResolver(Func <TSource, DateTime?> valueGetter)
        {
            NullGuard.NotNull(valueGetter, nameof(valueGetter));

            _valueGetter = valueGetter;
        }
        public static IServiceCollection AddResolvers(this IServiceCollection serviceCollection)
        {
            NullGuard.NotNull(serviceCollection, nameof(serviceCollection));

            return(serviceCollection.AddSingleton <IAcmeChallengeResolver, AcmeChallengeResolver>());
        }
        public static IServiceCollection AddEventPublisher(this IServiceCollection serviceCollection)
        {
            NullGuard.NotNull(serviceCollection, nameof(serviceCollection));

            return(serviceCollection.AddScoped <IEventPublisher, EventPublisher>());
        }
        public static IServiceCollection AddQueryBus(this IServiceCollection serviceCollection)
        {
            NullGuard.NotNull(serviceCollection, nameof(serviceCollection));

            return(serviceCollection.AddTransient <IQueryBus, QueryBus>());
        }
        public QueryBus(IEnumerable <IQueryHandler> queryHandlers)
        {
            NullGuard.NotNull(queryHandlers, nameof(queryHandlers));

            _queryHandlers = queryHandlers;
        }