private ActionResult ResultPage(TransferMoneyContext ctx)
 {
     ViewData["Amount"]      = ctx.Amount;
     ViewData["Source"]      = ((Account)ctx.Source).Name;
     ViewData["Destination"] = ((Account)ctx.Sink).Name;
     return(View("Result"));
 }
 public ActionResult SelectSource()
 {
     Session["Context"] = new TransferMoneyContext();
     return(View(new SelectAccountVm
     {
         SelectedAccountId = string.Empty,
         Accounts = accountRepo.Accounts
     }));
 }
        public ActionResult Index(FormCollection form)
        {
            var sourceAccountId      = form["SourceAccounts"];
            var destinationAccountId = form["DestinationAccounts"];
            var amount = form["Amount"];

            var sourceAccount      = accountRepo.GetById(int.Parse(sourceAccountId));
            var destinationAccount = accountRepo.GetById(int.Parse(destinationAccountId));

            var ctx = new TransferMoneyContext(sourceAccount as TransferMoneySource,
                                               destinationAccount as TransferMoneySink,
                                               decimal.Parse(amount));

            ctx.Execute();

            return(ResultPage(ctx));
        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);

            RegisterRoutes(RouteTable.Routes);

            var builder = new ContainerBuilder();

            builder.RegisterModule(new NHibernateSessionModule());

            //builder.RegisterModule(new ReportingSessionModule());

            builder.RegisterModule(new QueryModule());

            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            var container = builder.Build();

            _container = container;

            QueryModule._container = _container;

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            var rabbitMQPublisher = new RabbitMQPublisher(System.Configuration.ConfigurationManager.AppSettings["CLOUDAMQP_URL"]);

            //Use RabbitMQ
            //var commandBus = new CommandBus(rabbitMQPublisher);

            var commandBus = new CommandBus();

            var sessionFactory = container.Resolve<ISessionFactory>();
            IEventStore eventStore = new NHibernateEventStore(commandBus, sessionFactory);

            //Context
            var transferMoneyContext = new TransferMoneyContext(eventStore);
            var createBankAccountContext = new CreateBankAccountContext(eventStore);
            var changeAccountAndBalanceContext = new ChangeAccountNameAndBalanceContext(eventStore);
            var createCustomerContext = new CreateCustomerContext(eventStore);
            var addCustomerAddressContext = new AddCustomerAddressContext(eventStore);
            var stockNewProductContext = new StockNewProductContext(eventStore);
            var placeOrderContext = new PlaceOrderContext(eventStore);

            //Register Command
            commandBus.RegisterHandlerCommand<OpenAccountCommand>(createBankAccountContext.Handle);
            commandBus.RegisterHandlerCommand<TransferMoneyCommand>(transferMoneyContext.Handle);
            commandBus.RegisterHandlerCommand<ChangeAccountNameAndBalanceCommand>(changeAccountAndBalanceContext.Handle);
            commandBus.RegisterHandlerCommand<CreateCustomerCommand>(createCustomerContext.Handle);
            commandBus.RegisterHandlerCommand<AddCustomerAddressCommand>(addCustomerAddressContext.Handle);
            commandBus.RegisterHandlerCommand<CreateProductCommand>(stockNewProductContext.Handle);
            commandBus.RegisterHandlerCommand<PlaceOrderCommand>(placeOrderContext.Handle);

            //Report View
            var accountReportView = new AccountReportView(sessionFactory);

            //Register Event
            commandBus.RegisterHandlerEvent<AccountCreatedEvent>(accountReportView.Handle);
            commandBus.RegisterHandlerEvent<AccountNameAndBalanceChangedEvent>(accountReportView.Handle);
            commandBus.RegisterHandlerEvent<BalanceDecreasedEvent>(accountReportView.Handle);
            commandBus.RegisterHandlerEvent<BalanceIncreasedEvent>(accountReportView.Handle);

            //Report View
            var customerReportView = new CustomerReportView(container.Resolve<ISessionFactory>());

            //Register Event
            commandBus.RegisterHandlerEvent<CustomerCreatedEvent>(customerReportView.Handle);
            commandBus.RegisterHandlerEvent<CustomerAddressAddedEvent>(customerReportView.Handle);

            var placeOrderView = new PlaceOrderView(container.Resolve<ISessionFactory>());

            commandBus.RegisterHandlerEvent<OrderPlacedEvent>(placeOrderView.Handle);

            var productStockReportView = new ProductStockReportView(container.Resolve<ISessionFactory>());

            commandBus.RegisterHandlerEvent<ProductCreatedEvent>(productStockReportView.Handle);

            //ServiceLocator.Pub = rabbitMQPublisher;
            ServiceLocator.Bus = commandBus;
            //ServiceLocator.Sub = rabbitMQSubsriber;
        }