Exemplo n.º 1
0
        protected virtual bool PrepareAndValidateCommand <TCommand>(TCommand command)
            where TCommand : ICommand <TAuthenticationToken>
        {
            Type commandType = command.GetType();

            if (command.Frameworks.Contains("Azure"))
            {
                Logger.LogInfo("The provided command has already been processed in Azure.", string.Format("{0}\\Handle({1})", GetType().FullName, commandType.FullName));
                return(false);
            }

            ICommandValidator <TAuthenticationToken, TCommand> commandValidator = null;

            try
            {
                commandValidator = DependencyResolver.Resolve <ICommandValidator <TAuthenticationToken, TCommand> >();
            }
            catch (Exception exception)
            {
                Logger.LogDebug("Locating an ICommandValidator failed.", string.Format("{0}\\Handle({1})", GetType().FullName, commandType.FullName), exception);
            }

            if (commandValidator != null && !commandValidator.IsCommandValid(command))
            {
                Logger.LogInfo("The provided command is not valid.", string.Format("{0}\\Handle({1})", GetType().FullName, commandType.FullName));
                return(false);
            }

            PrepareCommand(command);
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Prepares and validates a <see cref="ICommand{TAuthenticationToken}"/> to be sent specifying the framework it is sent via.
        /// </summary>
        /// <typeparam name="TCommand">The <see cref="Type"/> of<see cref="ICommand{TAuthenticationToken}"/> being sent.</typeparam>
        /// <param name="command">The <see cref="ICommand{TAuthenticationToken}"/> to send.</param>
        /// <param name="framework">The framework the <paramref name="command"/> is being sent from.</param>
        public virtual bool PrepareAndValidateCommand <TCommand>(TCommand command, string framework)
            where TCommand : ICommand <TAuthenticationToken>
        {
            Type commandType = command.GetType();

            if (command.Frameworks != null && command.Frameworks.Contains(framework))
            {
                // if this is the only framework in the list, then it's fine to handle as it's just pre-stamped, if there is more than one framework, then exit.
                if (command.Frameworks.Count() != 1)
                {
                    Logger.LogInfo("The provided command has already been processed in Azure.", string.Format("{0}\\PrepareAndValidateEvent({1})", GetType().FullName, commandType.FullName));
                    return(false);
                }
            }

            ICommandValidator <TAuthenticationToken, TCommand> commandValidator = null;

            try
            {
                commandValidator = DependencyResolver.Resolve <ICommandValidator <TAuthenticationToken, TCommand> >();
            }
            catch (Exception exception)
            {
                Logger.LogDebug("Locating an ICommandValidator failed.", string.Format("{0}\\PrepareAndValidateEvent({1})", GetType().FullName, commandType.FullName), exception);
            }

            if (commandValidator != null && !commandValidator.IsCommandValid(command))
            {
                Logger.LogInfo("The provided command is not valid.", string.Format("{0}\\PrepareAndValidateEvent({1})", GetType().FullName, commandType.FullName));
                return(false);
            }

            PrepareCommand(command, framework);
            return(true);
        }
Exemplo n.º 3
0
        public virtual void Send <TCommand>(TCommand command)
            where TCommand : ICommand <TAuthenticationToken>
        {
            Type commandType = command.GetType();

            switch (command.Framework)
            {
            case FrameworkType.Akka:
                Logger.LogInfo(string.Format("A command arrived of the type '{0}' but was marked as coming from the '{1}' framework, so it was dropped.", commandType.FullName, command.Framework));
                return;
            }

            ICommandValidator <TAuthenticationToken, TCommand> commandValidator = null;

            try
            {
                commandValidator = DependencyResolver.Resolve <ICommandValidator <TAuthenticationToken, TCommand> >();
            }
            catch (Exception exception)
            {
                Logger.LogDebug("Locating an ICommandValidator failed.", string.Format("{0}\\Handle({1})", GetType().FullName, commandType.FullName), exception);
            }

            if (commandValidator != null && !commandValidator.IsCommandValid(command))
            {
                Logger.LogInfo("The provided command is not valid.", string.Format("{0}\\Handle({1})", GetType().FullName, commandType.FullName));
                return;
            }

            if (command.AuthenticationToken == null)
            {
                command.AuthenticationToken = AuthenticationTokenHelper.GetAuthenticationToken();
            }
            command.CorrelationId = CorrelationIdHelper.GetCorrelationId();

            bool isRequired;

            if (!ConfigurationManager.TryGetSetting(string.Format("{0}.IsRequired", commandType.FullName), out isRequired))
            {
                isRequired = true;
            }

            RouteHandlerDelegate commandHandler = Routes.GetSingleHandler(command, isRequired);

            // This check doesn't require an isRequired check as there will be an exception raised above and handled below.
            if (commandHandler == null)
            {
                Logger.LogDebug(string.Format("The command handler for '{0}' is not required.", commandType.FullName));
                return;
            }

            Action <IMessage> handler = commandHandler.Delegate;

            handler(command);
        }
Exemplo n.º 4
0
        public void Handle(CreateInventoryItemCommand command)
        {
            ICommandValidator <Cqrs.Authentication.ISingleSignOnToken, CreateInventoryItemCommand> commandValidator = null;

            try
            {
                commandValidator = DependencyResolver.Resolve <ICommandValidator <Cqrs.Authentication.ISingleSignOnToken, CreateInventoryItemCommand> >();
            }
            catch (Exception exception)
            {
                Logger.LogDebug("Locating an ICommandValidator failed.", "CreateInventoryItemCommandHandler\\Handle(CreateInventoryItemCommand)", exception);
            }

            if (commandValidator != null && !commandValidator.IsCommandValid(command))
            {
                Logger.LogInfo("The provided command is not valid.", "CreateInventoryItemCommandHandler\\Handle(CreateInventoryItemCommand)");
                return;
            }

            InventoryItem item = null;

            OnCreateInventoryItem(command, ref item);
            if (item == null)
            {
                item = new InventoryItem(DependencyResolver, Logger, command.Rsn == Guid.Empty ? Guid.NewGuid() : command.Rsn);
                UnitOfWork.Add(item);
            }
            item.CreateInventoryItem(command.Name);
            OnCreateInventoryItemDone(command, item);
            OnCommit(command, item);
            try
            {
                UnitOfWork.Commit();
            }
            catch (ConcurrencyException exception)
            {
                Logger.LogDebug(string.Format("Committing the Unit Of Work for command of type '{0}' with Rsn '{1}' failed.", command.GetType().FullName, command.Rsn), "Handle/CreateInventoryItemCommand", exception: new DuplicateCreateCommandException(command.GetType(), command.Rsn, exception));
                var eventPublisher = DependencyResolver.Resolve <IEventPublisher <Cqrs.Authentication.ISingleSignOnToken> >();

                var duplicateCreateCommandEvent = new DuplicateCreateCommandEvent <Cqrs.Authentication.ISingleSignOnToken>
                {
                    Id            = Guid.NewGuid(),
                    AggregateRsn  = command.Rsn,
                    AggregateType = typeof(InventoryItem)
                };
                eventPublisher.Publish(duplicateCreateCommandEvent);
            }
            OnCommited(command, item);
        }
Exemplo n.º 5
0
        protected virtual bool PrepareAndValidateCommand <TCommand>(TCommand command, out RouteHandlerDelegate commandHandler)
            where TCommand : ICommand <TAuthenticationToken>
        {
            Type commandType = command.GetType();

            if (command.Frameworks != null && command.Frameworks.Contains("Akka"))
            {
                // if this is the only framework in the list, then it's fine to handle as it's just pre-stamped, if there is more than one framework, then exit.
                if (command.Frameworks.Count() != 1)
                {
                    Logger.LogInfo("The provided command has already been processed in Akka.", string.Format("{0}\\PrepareAndValidateEvent({1})", GetType().FullName, commandType.FullName));
                    commandHandler = null;
                    return(false);
                }
            }

            ICommandValidator <TAuthenticationToken, TCommand> commandValidator = null;

            try
            {
                commandValidator = DependencyResolver.Resolve <ICommandValidator <TAuthenticationToken, TCommand> >();
            }
            catch (Exception exception)
            {
                Logger.LogDebug("Locating an ICommandValidator failed.", string.Format("{0}\\Handle({1})", GetType().FullName, commandType.FullName), exception);
            }

            if (commandValidator != null && !commandValidator.IsCommandValid(command))
            {
                Logger.LogInfo("The provided command is not valid.", string.Format("{0}\\Handle({1})", GetType().FullName, commandType.FullName));
                commandHandler = null;
                return(false);
            }

            PrepareCommand(command);

            bool isRequired = BusHelper.IsEventRequired(commandType);

            commandHandler = Routes.GetSingleHandler(command, isRequired);
            // This check doesn't require an isRequired check as there will be an exception raised above and handled below.
            if (commandHandler == null)
            {
                Logger.LogDebug(string.Format("The command handler for '{0}' is not required.", commandType.FullName));
            }

            return(true);
        }
Exemplo n.º 6
0
        protected virtual bool PrepareAndValidateCommand <TCommand>(TCommand command, out RouteHandlerDelegate commandHandler)
            where TCommand : ICommand <TAuthenticationToken>
        {
            Type commandType = command.GetType();

            switch (command.Framework)
            {
            case FrameworkType.Akka:
                Logger.LogInfo(string.Format("A command arrived of the type '{0}' but was marked as coming from the '{1}' framework, so it was dropped.", commandType.FullName, command.Framework));
                commandHandler = null;
                return(false);
            }

            ICommandValidator <TAuthenticationToken, TCommand> commandValidator = null;

            try
            {
                commandValidator = DependencyResolver.Resolve <ICommandValidator <TAuthenticationToken, TCommand> >();
            }
            catch (Exception exception)
            {
                Logger.LogDebug("Locating an ICommandValidator failed.", string.Format("{0}\\Handle({1})", GetType().FullName, commandType.FullName), exception);
            }

            if (commandValidator != null && !commandValidator.IsCommandValid(command))
            {
                Logger.LogInfo("The provided command is not valid.", string.Format("{0}\\Handle({1})", GetType().FullName, commandType.FullName));
                commandHandler = null;
                return(false);
            }

            PrepareCommand(command);

            bool isRequired = BusHelper.IsEventRequired(commandType);

            commandHandler = Routes.GetSingleHandler(command, isRequired);
            // This check doesn't require an isRequired check as there will be an exception raised above and handled below.
            if (commandHandler == null)
            {
                Logger.LogDebug(string.Format("The command handler for '{0}' is not required.", commandType.FullName));
                return(false);
            }

            return(true);
        }
Exemplo n.º 7
0
        public void Handle(WithdrawFundsFromAccountCommand command)
        {
            ICommandValidator <Cqrs.Authentication.ISingleSignOnToken, WithdrawFundsFromAccountCommand> commandValidator = null;

            try
            {
                commandValidator = DependencyResolver.Resolve <ICommandValidator <Cqrs.Authentication.ISingleSignOnToken, WithdrawFundsFromAccountCommand> >();
            }
            catch (Exception exception)
            {
                Logger.LogDebug("Locating an ICommandValidator failed.", "WithdrawFundsFromAccountCommandHandler\\Handle(WithdrawFundsFromAccountCommand)", exception);
            }

            if (commandValidator != null && !commandValidator.IsCommandValid(command))
            {
                Logger.LogInfo("The provided command is not valid.", "WithdrawFundsFromAccountCommandHandler\\Handle(WithdrawFundsFromAccountCommand)");
                return;
            }

            bool continueExecution = true;

            OnHandle(command, ref continueExecution);
            if (continueExecution)
            {
                Account item = null;
                OnWithdrawFunds(command, ref item);
                if (item == null)
                {
                    item = new Account(DependencyResolver, Logger, command.Rsn);
                    UnitOfWork.Add(item);
                }
                item.WithdrawFunds(command.Amount);
                OnWithdrawFundsHandled(command, item);
                OnAddToUnitOfWork(command, item);
                UnitOfWork.Add(item);
                OnAddedToUnitOfWork(command, item);
                OnCommit(command, item);
                UnitOfWork.Commit();
                OnCommited(command, item);
            }
        }
Exemplo n.º 8
0
        public void Handle(RenameInventoryItemCommand command)
        {
            ICommandValidator <Cqrs.Authentication.ISingleSignOnToken, RenameInventoryItemCommand> commandValidator = null;

            try
            {
                commandValidator = DependencyResolver.Resolve <ICommandValidator <Cqrs.Authentication.ISingleSignOnToken, RenameInventoryItemCommand> >();
            }
            catch (Exception exception)
            {
                Logger.LogDebug("Locating an ICommandValidator failed.", "RenameInventoryItemCommandHandler\\Handle(RenameInventoryItemCommand)", exception);
            }

            if (commandValidator != null && !commandValidator.IsCommandValid(command))
            {
                Logger.LogInfo("The provided command is not valid.", "RenameInventoryItemCommandHandler\\Handle(RenameInventoryItemCommand)");
                return;
            }

            OnHandle(command);
        }