public IValidationResult Validate(CommnadMessage message) { AppDomain.CurrentDomain.Load(AssemblyNameList.CommandValidation); var commandType = message.Command.GetType(); var commnadValidationType = AppDomain.CurrentDomain.GetAssemblies() .Where(a => a.GetName().Name == AssemblyNameList.CommandValidation) .SelectMany(a => a.GetTypes()) .SingleOrDefault(t => t.GetInterfaces().Any(ifc => ifc.IsGenericType && ifc.GetGenericTypeDefinition() == typeof (ICommandValidator<>) && ifc.GetGenericArguments().First() == commandType)); if (commnadValidationType == null) return new ValidationResult(); var validatorMethod = commnadValidationType.GetMethods() .Single(m => m.Name == "Validate" && m.GetParameters().First().ParameterType == commandType); var arguments = commnadValidationType.GetConstructors()[0].GetParameters() .Select(p => DependencyManager.Resolve(p.ParameterType)).ToArray(); var instance = Activator.CreateInstance(commnadValidationType, arguments); commnadValidationType.GetProperty("Current").SetValue(instance, message.Current); validatorMethod.Invoke(instance, new object[] {message.Command}); return instance.As<DomainValidator>().ValidationResult; }
public object Handle(CommnadMessage message) { var command = message.Command; var current = message.Current; AppDomain.CurrentDomain.Load(AssemblyNameList.CommandHandlers); var commandType = command.GetType(); var commnadHandlerType = AppDomain.CurrentDomain.GetAssemblies() .Where(a => a.GetName().Name == AssemblyNameList.CommandHandlers) .SelectMany(a => a.GetTypes()) .Single(t => t.GetInterfaces().Any(ifc => ifc.IsGenericType && ifc.GetGenericTypeDefinition() == typeof(ICommandHandler<>) && ifc.GetGenericArguments().First() == commandType)); var handleMethod = commnadHandlerType.GetMethods() .Single(m => m.Name == "Handle" && m.GetParameters().First().ParameterType == commandType); var arguments = commnadHandlerType.GetConstructors()[0].GetParameters() .Select(p => DependencyManager.Resolve(p.ParameterType)).ToArray(); var instance = Activator.CreateInstance(commnadHandlerType, arguments); commnadHandlerType.GetProperty("Current").SetValue(instance, current); handleMethod.Invoke(instance, new object[] { command }); return instance.As<DomainService>().ReturnValue; }