コード例 #1
0
        public static void Setup()
        {
            var container           = new Container();
            var perRequest          = new WebRequestLifestyle();
            var dataAccessAssembly  = typeof(CinemaContext).Assembly;
            var moviesAssembly      = typeof(Seat).Assembly;
            var promotionsAssembly  = typeof(Promotions.Promotion).Assembly;
            var applicationAssembly = typeof(RenamePromotionCommand).Assembly;
            var connectionString    = ConfigurationManager.ConnectionStrings["DDDCinema"].ConnectionString;

            container.Register(() => new CinemaContext(connectionString), perRequest);
            container.Register(() => new PromotionsContext(connectionString), perRequest);
            container.Register(() => new InfrastructureContext(connectionString), perRequest);
            container.Register(() => new DDDCinemaReadonly(), perRequest);
            var userProviderRegistration = Lifestyle.Singleton.CreateRegistration <ContextUserProvider>(container);

            container.AddRegistration(typeof(ICurrentUserProvider), userProviderRegistration);
            container.AddRegistration(typeof(ContextUserProvider), userProviderRegistration);
            container.Register <IWinChanceCalculatorFactory, SimpleInjectorWinChanceCalculatorFactory>(Lifestyle.Singleton);

            foreach (var repositorType in dataAccessAssembly.GetExportedTypes()
                     .Where(t => t.Name.Contains("Repository")))
            {
                container.Register(repositorType.GetInterfaces().Single(), repositorType, perRequest);
            }

            container.RegisterDecorator(typeof(ICommandHandler <LoginCommand>), typeof(AuditingLoginCommandHandler));
            container.RegisterDecorator(typeof(ICommandHandler <>), typeof(AuditingCommandHandler <>),
                                        p => !p.AppliedDecorators.Any(t => t.Name.Contains("Auditing")));
            container.RegisterDecorator(typeof(ICommandHandler <>), typeof(CinemaTransactionalCommandHandler <>));
            container.RegisterDecorator(typeof(ICommandHandler <>), typeof(PromotionTransactionalCommandHandler <>));
            container.RegisterDecorator(typeof(ICommandHandler <>), typeof(InfrastructureTransactionalCommandHandler <>));
            container.Register(typeof(ICommandHandler <>), new[] { applicationAssembly });

            container.RegisterCollection(typeof(INotificationSender), new[] { moviesAssembly });
            var registration = perRequest.CreateRegistration <SendNotificationWhenSeatTaken>(container);

            container.AppendToCollection(typeof(IDomainEventHandler <>), typeof(AuditOccurrenceEventHandler <>));
            container.RegisterCollection(typeof(IDomainEventHandler <>), moviesAssembly, promotionsAssembly);
            container.RegisterDecorator(typeof(IDomainEventHandler <>), typeof(AuditingEventHandler <>),
                                        p => !p.ImplementationType.Name.Contains("Audit"));


            container.Register <List <INotificationSender> >(() => container.GetAllInstances <INotificationSender>().ToList(), perRequest);
            container.Register <ISheduler, SagaTimeoutSheduler>(perRequest);
            container.Register <IPromotionCodeGenerator, PromoCodeGenerator>(perRequest);
            DomainEventBus.Current = new SimpleInjectorEventBus(container);
            DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
        }
コード例 #2
0
ファイル: IoCRegister.cs プロジェクト: levanvunam/EasyCMS
        public static void RegisterDepencies(Container containter)
        {
            var assemblies           = ReflectionUtilities.GetAssemblies(FrameworkConstants.EzSolution).ToList();
            var dependencyInterfaces = assemblies.SelectMany(i => i.ExportedTypes).Where(i => i.IsInterface && i.GetCustomAttribute <RegisterAttribute>() != null);

            foreach (var dependencyInterface in dependencyInterfaces)
            {
                var registerAttr = dependencyInterface.GetCustomAttribute <RegisterAttribute>();

                // Get all implementations of RegisterAttribute
                var       implementations = dependencyInterface.GetAllImplementTypes(FrameworkConstants.EzSolution).ToList();
                Lifestyle lifestyle       = Lifestyle.Transient;

                switch (registerAttr.ScopeLifetime)
                {
                case Lifetime.PerInstance:
                    lifestyle = Lifestyle.Transient;
                    break;

                case Lifetime.PerRequest:
                    lifestyle = new WebRequestLifestyle();
                    break;

                case Lifetime.SingleTon:
                    lifestyle = Lifestyle.Singleton;
                    break;
                }

                if (implementations.Count == 1)
                {
                    containter.Register(dependencyInterface, implementations.First(), lifestyle);
                }
                else
                {
                    var registrations = implementations.Select(i => lifestyle.CreateRegistration(i, containter));
                    containter.RegisterCollection(dependencyInterface, registrations);
                }
            }

            #region Module Registers

            // Register generic dependencies first
            var dependencyRegisters = typeof(IDependencyRegister).GetAllImplementTypesOf(FrameworkConstants.EzSolution);
            foreach (var dependencyRegister in dependencyRegisters)
            {
                var implementaton = (IDependencyRegister)Activator.CreateInstance(dependencyRegister);
                implementaton.Register(containter);
            }

            // Register service
            dependencyInterfaces = assemblies.SelectMany(i => i.ExportedTypes).Where(i => i.IsInterface && i.GetCustomAttribute <ModuleRegisterAttribute>() != null);
            foreach (var dependencyInterface in dependencyInterfaces)
            {
                var registerAttr = dependencyInterface.GetCustomAttribute <ModuleRegisterAttribute>();

                // Get all implementations of RegisterAttribute
                var       implementations = dependencyInterface.GetAllImplementTypes(FrameworkConstants.EzSolution).ToList();
                Lifestyle lifestyle       = Lifestyle.Transient;

                switch (registerAttr.ScopeLifetime)
                {
                case Lifetime.PerInstance:
                    lifestyle = Lifestyle.Transient;
                    break;

                case Lifetime.PerRequest:
                    lifestyle = new WebRequestLifestyle();
                    break;

                case Lifetime.SingleTon:
                    lifestyle = Lifestyle.Singleton;
                    break;
                }

                if (implementations.Count == 1)
                {
                    containter.Register(dependencyInterface, implementations.First(), lifestyle);
                }
                else
                {
                    var registrations = implementations.Select(i => lifestyle.CreateRegistration(i, containter));
                    containter.RegisterCollection(dependencyInterface, registrations);
                }
            }

            #endregion
        }