public void LifetimeScopeDispose_TwoScopedRegistationsForTheSameServiceType_DisposesBothInstances()
        {
            // Arrange
            var disposedInstances = new HashSet <object>();

            var lifestyle = new LifetimeScopeLifestyle();

            var container = new Container();

            var reg1 = lifestyle.CreateRegistration <ICommand, DisposableCommand>(container);
            var reg2 = lifestyle.CreateRegistration <ICommand, DisposableCommand>(container);

            container.AppendToCollection(typeof(ICommand), reg1);
            container.AppendToCollection(typeof(ICommand), reg2);

            using (container.BeginLifetimeScope())
            {
                var commands = container.GetAllInstances <ICommand>().Cast <DisposableCommand>().ToArray();

                Assert.AreNotSame(commands[0], commands[1], "Test setup failed.");

                commands[0].Disposing += sender => disposedInstances.Add(sender);
                commands[1].Disposing += sender => disposedInstances.Add(sender);

                // Act
            }

            // Assert
            Assert.AreEqual(2, disposedInstances.Count, "Two instances were expected to be disposed.");
        }
        public void LifetimeScope_TwoScopedRegistationsForTheSameServiceType_CreatesTwoInstances()
        {
            // Arrange
            var lifestyle = new LifetimeScopeLifestyle();

            var container = new Container();

            var reg1 = lifestyle.CreateRegistration <ICommand, DisposableCommand>(container);
            var reg2 = lifestyle.CreateRegistration <ICommand, DisposableCommand>(container);

            container.AppendToCollection(typeof(ICommand), reg1);
            container.AppendToCollection(typeof(ICommand), reg2);

            using (container.BeginLifetimeScope())
            {
                // Act
                var commands = container.GetAllInstances <ICommand>().Cast <DisposableCommand>().ToArray();

                // Assert
                Assert.AreNotSame(commands[0], commands[1], "Two instances were expected.");
            }
        }
コード例 #3
0
        private static void RegisterDataServices(this Container container, string connectionString)
        {
            var scopedLifestyle             = new LifetimeScopeLifestyle();
            var databaseContextRegistration = scopedLifestyle.CreateRegistration(() =>
            {
#if DEBUG
                Database.SetInitializer(new DropCreateDatabaseIfModelChangesInitializer());
#else
                Database.SetInitializer <HiLaarischEntities>(null);
#endif
                return(new HiLaarischEntities(connectionString));
            }, container);

            container.AddRegistration(typeof(HiLaarischEntities), databaseContextRegistration);
            container.AddRegistration(typeof(DbContext), databaseContextRegistration);

            container.Register(typeof(IRepository <>), typeof(Repository <>));
        }