public void Map(ICommand command, IMappedCommandExecutor executor)
        {
            var commandType = command.GetType();
            IEnumerable <dynamic> attributes = commandType.GetCustomAttributes(false);

            dynamic attributeHandler;

            foreach (dynamic attribute in attributes)
            {
                if (_handlers.TryGetValue(attribute.GetType(), out attributeHandler))
                {
                    attributeHandler.Map(attribute, command, executor);
                    return;
                }
            }
            throw new CommandMappingException(string.Format("Could not find any mapping attribute handlers for mapping command of type {0}.", command.GetType().AssemblyQualifiedName));
        }
Esempio n. 2
0
        public void Map(MapsToAggregateRootConstructorAttribute attribute, ICommand command, IMappedCommandExecutor executor)
        {
            var commandType = command.GetType();

            ValidateCommandType(commandType);

            var match = GetMatchingConstructor(attribute, commandType);
            Func <ICommand, AggregateRoot> create = (c) =>
            {
                var parameter = match.Item2.Select(p => p.GetValue(c, null));
                return((AggregateRoot)match.Item1.Invoke(parameter.ToArray()));
            };

            Action executorAction = () => executor.ExecuteActionCreatingNewInstance(create);

            if (commandType.IsDefined(typeof(TransactionalAttribute), false))
            {
                var transactionService = NcqrsEnvironment.Get <ITransactionService>();
                transactionService.ExecuteInTransaction(executorAction);
            }
            else
            {
                executorAction();
            }
        }
        public void Map(MapsToAggregateRootMethodAttribute attribute, ICommand command, IMappedCommandExecutor executor)
        {
            var commandType = command.GetType();

            ValidateCommandType(commandType);

            var match = GetMatchingMethod(attribute, commandType, attribute.MethodName);

            Action <AggregateRoot, ICommand> action =
                (agg, cmd) =>
            {
                var parameter = match.Item2.Select(p => p.GetValue(cmd, null));
                match.Item1.Invoke(agg, parameter.ToArray());
            };

            Action executorAction = () => executor.ExecuteActionOnExistingInstance(GetAggregateRootId, GetAggregateRootType, action);

            if (commandType.IsDefined(typeof(TransactionalAttribute), false))
            {
                var transactionService = NcqrsEnvironment.Get <ITransactionService>();
                transactionService.ExecuteInTransaction(executorAction);
            }
            else
            {
                executorAction();
            }
        }