public void Context()
        {
            HttpContext.Current = FakeHttpContextHelper.GetFakeHttpContext();

            var domainEventHandlerFactory = new FakeDomainEventHandlerFactory(domainEvent => _raisedDomainEvent = (TestDomainEvent)domainEvent);

            DomainEvents.Initialize(domainEventHandlerFactory, isDelayedDomainEventHandlingEnabled: true);
            DomainEvents.ResetDelayedEventsStorage();

            var unitOfWorkFactory = IoC.Resolve <IUnitOfWorkFactory>();

            UnitOfWorkHttpModule.Initialize(unitOfWorkFactory);
            var unitOfWorkHttpModule = new UnitOfWorkHttpModule();
            var httpApplication      = new FakeHttpApplication();

            unitOfWorkHttpModule.Init(httpApplication);

            httpApplication.FireBeginRequest();

            try
            {
                _simulateApplicationTransactionWhichThrowsAnException();
            }
            catch
            {
                httpApplication.FireError();
            }
        }
Пример #2
0
        private void _RegisterServicesIntoCastleWindsorIoCContainer()
        {
            _castleWindsorIoCContainer = new WindsorContainer();

            CoreDddNhibernateInstaller.SetUnitOfWorkLifeStyle(x => x.PerWebRequest);

            _castleWindsorIoCContainer.Install(
                FromAssembly.Containing <CoreDddInstaller>(),
                FromAssembly.Containing <CoreDddNhibernateInstaller>()
                );
            _castleWindsorIoCContainer.Register(
                Component
                .For <INhibernateConfigurator>()
                .ImplementedBy <CoreDddSampleNhibernateConfigurator>()
                .LifestyleSingleton()
                );

            // register command handlers
            _castleWindsorIoCContainer.Register(
                Classes
                .FromAssemblyContaining <CreateNewShipCommandHandler>()
                .BasedOn(typeof(ICommandHandler <>))
                .WithService.FirstInterface()
                .Configure(x => x.LifestyleTransient())
                );
            // register query handlers
            _castleWindsorIoCContainer.Register(
                Classes
                .FromAssemblyContaining <GetShipsByNameQueryHandler>()
                .BasedOn(typeof(IQueryHandler <>))
                .WithService.FirstInterface()
                .Configure(x => x.LifestyleTransient())
                );
            // register domain event handlers
            _castleWindsorIoCContainer.Register(
                Classes
                .FromAssemblyContaining <ShipUpdatedDomainEventHandler>()
                .BasedOn(typeof(IDomainEventHandler <>))
                .WithService.FirstInterface()
                .Configure(x => x.LifestyleTransient())
                );

            UnitOfWorkHttpModule.Initialize(
                _castleWindsorIoCContainer.Resolve <IUnitOfWorkFactory>(),
                isolationLevel: System.Data.IsolationLevel.ReadCommitted
                );

            DomainEvents.Initialize(
                _castleWindsorIoCContainer.Resolve <IDomainEventHandlerFactory>(),
                isDelayedDomainEventHandlingEnabled: true
                );

            IoC.Initialize(new CastleContainer(_castleWindsorIoCContainer));
        }
Пример #3
0
        private void _RegisterServicesIntoNinjectIoCContainer()
        {
            var ninjectIoCContainer = NinjectWebCommon.Bootstrapper.Kernel;

            CoreDddNhibernateBindings.SetUnitOfWorkLifeStyle(x => x.InRequestScope());

            ninjectIoCContainer.Load(
                typeof(CoreDddBindings).Assembly,
                typeof(CoreDddNhibernateBindings).Assembly
                );
            ninjectIoCContainer
            .Bind <INhibernateConfigurator>()
            .To <CoreDddSampleNhibernateConfigurator>()
            .InSingletonScope();

            // register controllers
            ninjectIoCContainer.Bind(x => x
                                     .FromAssemblyContaining <HomeController>()
                                     .SelectAllClasses()
                                     .InheritedFrom <ControllerBase>()
                                     .BindAllInterfaces()
                                     .Configure(y => y.InTransientScope()));
            // register command handlers
            ninjectIoCContainer.Bind(x => x
                                     .FromAssemblyContaining <CreateNewShipCommandHandler>()
                                     .SelectAllClasses()
                                     .InheritedFrom(typeof(ICommandHandler <>))
                                     .BindAllInterfaces()
                                     .Configure(y => y.InTransientScope()));
            // register query handlers
            ninjectIoCContainer.Bind(x => x
                                     .FromAssemblyContaining <GetShipsByNameQueryHandler>()
                                     .SelectAllClasses()
                                     .InheritedFrom(typeof(IQueryHandler <>))
                                     .BindAllInterfaces()
                                     .Configure(y => y.InTransientScope()));
            // register domain event handlers
            ninjectIoCContainer.Bind(x => x
                                     .FromAssemblyContaining <ShipUpdatedDomainEventHandler>()
                                     .SelectAllClasses()
                                     .InheritedFrom(typeof(IDomainEventHandler <>))
                                     .BindAllInterfaces()
                                     .Configure(y => y.InTransientScope()));

            UnitOfWorkHttpModule.Initialize(
                ninjectIoCContainer.Get <IUnitOfWorkFactory>(),
                isolationLevel: System.Data.IsolationLevel.ReadCommitted
                );

            DomainEvents.Initialize(
                ninjectIoCContainer.Get <IDomainEventHandlerFactory>(),
                isDelayedDomainEventHandlingEnabled: true
                );
        }
Пример #4
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            _windsorContainer = new WindsorContainer();
            _configureBus(_windsorContainer);

            CoreDddNhibernateInstaller.SetUnitOfWorkLifeStyle(x => x.PerWebRequest);

            _windsorContainer.Install(
                FromAssembly.Containing <CoreDddInstaller>(),
                FromAssembly.Containing <CoreDddNhibernateInstaller>(),
                FromAssembly.Containing <ControllerInstaller>(),
                FromAssembly.Containing <CommandHandlerInstaller>(),
                FromAssembly.Containing <EventHandlerInstaller>(),
                FromAssembly.Containing <QueryHandlerInstaller>(),
                FromAssembly.Containing <EmailMakerNhibernateInstaller>()
                );

            _setupTransactionScopeUnitOfWork();
            //_setupDelayedDomainEventHandlingForUnitOfWork();

            ControllerBuilder.Current.SetControllerFactory(new IoCControllerFactory(_windsorContainer));
            ModelBinders.Binders.DefaultBinder = new EnumConverterModelBinder();

            _UpgradeDatabase();

            void _setupTransactionScopeUnitOfWork()
            {
                // call this method only when TransactionScopeUnitOfWorkHttpModule is used instead of UnitOfWorkHttpModule (see web.config system.webServer -> modules)

                TransactionScopeUnitOfWorkHttpModule.Initialize(
                    _windsorContainer.Resolve <IUnitOfWorkFactory>(),
                    transactionScopeEnlistmentAction: transactionScope => transactionScope.EnlistRebus()
                    );

                DomainEvents.Initialize(_windsorContainer.Resolve <IDomainEventHandlerFactory>());
            }

            void _setupDelayedDomainEventHandlingForUnitOfWork()
            {
                // call this method only when UnitOfWorkHttpModule is used instead of TransactionScopeUnitOfWorkHttpModule (see web.config system.webServer -> modules)

                UnitOfWorkHttpModule.Initialize(_windsorContainer.Resolve <IUnitOfWorkFactory>());

                DomainEvents.Initialize(
                    _windsorContainer.Resolve <IDomainEventHandlerFactory>(),
                    isDelayedDomainEventHandlingEnabled: true
                    );
            }

            void _configureBus(WindsorContainer container)
            {
                var rebusConfigurer = Configure.With(new CastleWindsorContainerAdapter(container));

                var rebusInputQueueName = ConfigurationManager.AppSettings["RebusInputQueueName"];
                var rebusTransport      = ConfigurationManager.AppSettings["RebusTransport"];

                switch (rebusTransport)
                {
                case "MSMQ":
                    rebusConfigurer
                    .Transport(x => x.UseMsmq(rebusInputQueueName))
                    .Subscriptions(x => x.UseJsonFile($"{Path.GetTempPath()}\\emailmaker_msmq_subscriptions.json"))
                    ;
                    break;

                case "RabbitMQ":
                    var rebusRabbitMqConnectionString = ConfigurationManager.AppSettings["RebusRabbitMqConnectionString"];
                    rebusConfigurer.Transport(x => x.UseRabbitMq(rebusRabbitMqConnectionString, rebusInputQueueName));
                    break;

                default:
                    throw new Exception($"Unknown rebus transport: {rebusTransport}");
                }

                rebusConfigurer.Start();
            }
        }