public void Initialize(IObjectContainer container, IEnumerable <Type> types) { BasicTypes.CommandTypes.Values.ForEach(commandType => { var commandHandlers = container.ResolveAll(typeof(ICommandHandler <>).MakeGenericType(commandType)).ToList(); switch (commandHandlers.Count) { case 0: break; case 1: _commandHandlerDescriptors[commandType] = new HandlerDescriptor(commandHandlers[0], handlerType => handlerType.GetMethod("Handle", new[] { typeof(ICommandContext), commandType }), HandlerStyle.Special); break; default: throw new SystemException(string.Format( "Found more than one handler for this type('{0}') with ICommandHandler<>.", commandType.FullName)); } if (commandHandlers.Count == 0) { Initialize(container, commandType); } var handlerDescriptor = GetHandlerDescriptors(commandType).First(); var callHandlers = HandlerAttributeHelper.GetHandlersFor(handlerDescriptor.Method, container); _pipelineManager.InitializePipeline(handlerDescriptor.Method, callHandlers); }); }
public void Initialize(IObjectContainer container, IEnumerable <Type> types) { var contactTypeMap = new Dictionary <Type, Type>(); types.Where(type => type.IsClass && !type.IsAbstract) .ForEach(type => { type.GetInterfaces().Where(interfaceType => interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IQueryHandler <,>)) .ForEach(interfaceType => { var queryType = interfaceType.GetGenericArguments().First(); if (contactTypeMap.ContainsKey(queryType)) { var errorMessage = string.Format( "There are have duplicate IQueryHandler<> interface type for {0}.", queryType.FullName); throw new SystemException(errorMessage); } contactTypeMap[queryType] = interfaceType; }); }); BasicTypes.QueryTypes.Values.ForEach(queryType => { Type contactType; if (!contactTypeMap.TryGetValue(queryType, out contactType)) { var errorMessage = string.Format("The query handler of this type('{0}') is not found.", queryType.FullName); throw new SystemException(errorMessage); } var handlers = container.ResolveAll(contactType).ToList(); switch (handlers.Count) { case 0: var errorMessage = string.Format("The query handler for {0} is not found.", contactType.GetFriendlyTypeName()); throw new SystemException(errorMessage); case 1: var handlerDescriptor = new HandlerDescriptor(handlers[0], handlerType => handlerType.GetMethod("Handle", new[] { queryType }), HandlerStyle.Special); _handlerDescriptors[queryType] = handlerDescriptor; var callHandlers = HandlerAttributeHelper.GetHandlersFor(handlerDescriptor.Method, container); _pipelineManager.InitializePipeline(handlerDescriptor.Method, callHandlers); break; default: errorMessage = string.Format( "Found more than one handler for '{0}' with IQueryHandler<>.", queryType.FullName); throw new SystemException(errorMessage); } }); }