Esempio n. 1
0
        public void ExpressionRegistrationCanBeChanged()
        {
            var container = new IocContainer();
            var lifetime  = new TransientLifetime();
            var reg       = new ExpressionRegistration <IFoo>(container, x => new Foo1(), lifetime, CompileMode.Delegate);

            Assert.AreSame(reg.Lifetime, lifetime);
            Assert.AreEqual(reg.CompileMode, CompileMode.Delegate);

            var get1 = reg.GetInstance();

            var newLifetime    = new ContainerLifetime();
            var newCompileMode = CompileMode.Dynamic;

            // Set new lifetime
            reg.SetLifetime(newLifetime);
            Assert.AreSame(reg.Lifetime, newLifetime);

            // Set different compile mode
            reg.SetCompileMode(newCompileMode);
            Assert.AreEqual(reg.CompileMode, newCompileMode);

            var get2 = reg.GetInstance();
            var get3 = reg.GetInstance();

            // Check that the lifetime is also being used
            Assert.AreNotSame(get1, get2);
            Assert.AreSame(get2, get3);
        }
		public void ExpressionRegistrationCanBeChanged()
		{
			var container = new IocContainer();
			var lifetime = new TransientLifetime();
			var reg = new ExpressionRegistration<IFoo>(container, x => new Foo1(), lifetime, CompileMode.Delegate);

			Assert.AreSame(reg.Lifetime, lifetime);
			Assert.AreEqual(reg.CompileMode, CompileMode.Delegate);

			var get1 = reg.GetInstance();

			var newLifetime = new ContainerLifetime();
			var newCompileMode = CompileMode.Dynamic;

			// Set new lifetime
			reg.SetLifetime(newLifetime);
			Assert.AreSame(reg.Lifetime, newLifetime);

			// Set different compile mode
			reg.SetCompileMode(newCompileMode);
			Assert.AreEqual(reg.CompileMode, newCompileMode);

			var get2 = reg.GetInstance();
			var get3 = reg.GetInstance();

			// Check that the lifetime is also being used
			Assert.AreNotSame(get1, get2);
			Assert.AreSame(get2, get3);
		}
        public void CanSetDefaultLifetimeManagerToContainerLifetime()
        {
            var lifetime = new ContainerLifetime();

            iocContainer.UsesDefaultLifetimeManagerOf(lifetime);

            Verify.That(iocContainer.DefaultLifetimeManager).IsTheSameObjectAs(lifetime);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a new instance of IoC Container
        /// </summary>
        public IoCContainer(bool asSingleton = true)
        {
            this._lifetime = new ContainerLifetime(t => this._registeredTypes[t]);

            if (asSingleton)
            {
                Current = this;
            }
        }
Esempio n. 5
0
        public void DisposableWithContainerLifetimeAreDisposed()
        {
            var c   = new Container();
            var clt = new ContainerLifetime();

            c.Register <IBar>(x => new DisposableBar()).WithLifetimeManager(clt);
            var df = c.Resolve <IBar>();

            disposeWasCalled = false;

            c.Dispose();

            Assert.IsTrue(disposeWasCalled);
        }
        public void ContainerLifetimeManagerAlwaysReturnsSameInstance()
        {
            var lifetime = new ContainerLifetime();

            iocContainer.UsesDefaultLifetimeManagerOf(lifetime);
            iocContainer.Register <IFoo>(c => new Foo1());

            var result1 = iocContainer.Resolve <IFoo>();
            var result2 = iocContainer.Resolve <IFoo>();
            var result3 = iocContainer.Resolve <IFoo>();

            Verify.That(result3).IsNotNull();
            Verify.That(result2).IsNotNull();
            Verify.That(result1).IsNotNull()
            .IsTheSameObjectAs(result2)
            .IsTheSameObjectAs(result3);
        }
Esempio n. 7
0
        /// <summary>
        ///     Register a specific service
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="concrete">The class that implements one or more services.</param>
        /// <param name="lifetime">Lifetime.</param>
        /// <remarks>
        ///     This method will check which interfaces the type implements. It will registered the type as any non <c>System.*</c>
        ///     interfaces. If the class do not implement any interfaces it will be registered as itself.
        /// </remarks>
        public static void RegisterService(this IServiceCollection builder, Type concrete, ContainerLifetime lifetime)
        {
            var interfaces = concrete.GetInterfaces().Where(x => !x.Namespace.StartsWith("System"));

            if (interfaces.Any())
            {
                foreach (var @interface in interfaces)
                {
                    switch (lifetime)
                    {
                    case ContainerLifetime.Scoped:
                        builder.AddScoped(@interface, concrete);
                        break;

                    case ContainerLifetime.SingleInstance:
                        builder.AddSingleton(@interface, concrete);
                        break;

                    case ContainerLifetime.Transient:
                        builder.AddTransient(@interface, concrete);
                        break;
                    }
                }

                return;
            }


            switch (lifetime)
            {
            case ContainerLifetime.Scoped:
                builder.AddScoped(concrete);
                break;

            case ContainerLifetime.SingleInstance:
                builder.AddSingleton(concrete);
                break;

            case ContainerLifetime.Transient:
                builder.AddTransient(concrete);
                break;
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Creates a new instance of IoC Container
 /// </summary>
 public SvgContainer()
 {
     _registeredTypes = new Dictionary <Type, Func <ILifetime, object> >();
     _lifetime        = new ContainerLifetime(t => _registeredTypes[t]);
 }
Esempio n. 9
0
 public ScopeLifetime(ContainerLifetime parentContainer)
 {
     _parentLifetime = parentContainer;
 }
        /// <summary>
        ///     Register a specific service
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="concrete">The class that implements one or more services.</param>
        /// <param name="lifetime">Lifetime.</param>
        /// <remarks>
        ///     This method will check which interfaces the type implements. It will registered the type as any non <c>System.*</c>
        ///     interfaces. If the class do not implement any interfaces it will be registered as itself.
        /// </remarks>
        public static void RegisterService(this IUnityContainer container, Type concrete, ContainerLifetime lifetime)
        {
            switch (lifetime)
            {
            case ContainerLifetime.Scoped:
                Register(container, concrete, new HierarchicalLifetimeManager());
                break;

            case ContainerLifetime.SingleInstance:
                Register(container, concrete, new ContainerControlledLifetimeManager());
                break;

            case ContainerLifetime.Transient:
                Register(container, concrete, new TransientLifetimeManager());
                break;
            }
        }
Esempio n. 11
0
 /// <summary>
 /// Creates a new instance of IoC Container
 /// </summary>
 public Container()
 {
     _lifetime = new ContainerLifetime(GetRegisteredTypes);
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="ContainerServiceAttribute" /> class.
 /// </summary>
 /// <param name="lifetime">how long the object should live in the container..</param>
 public ContainerServiceAttribute(ContainerLifetime lifetime)
 {
     Lifetime = lifetime;
 }
 /// <summary>
 ///     Register a specific service
 /// </summary>
 /// <param name="container">The container.</param>
 /// <param name="concrete">The class that implements one or more services.</param>
 /// <param name="lifetime">Lifetime.</param>
 /// <remarks>
 ///     This method will check which interfaces the type implements. It will registered the type as any non <c>System.*</c>
 ///     interfaces. If the class do not implement any interfaces it will be registered as itself.
 /// </remarks>
 public static void RegisterService(this IUnityContainer container, Type concrete, ContainerLifetime lifetime)
 {
     switch (lifetime)
     {
         case ContainerLifetime.Scoped:
             Register(container, concrete, new HierarchicalLifetimeManager());
             break;
         case ContainerLifetime.SingleInstance:
             Register(container, concrete, new ContainerControlledLifetimeManager());
             break;
         case ContainerLifetime.Transient:
             Register(container, concrete, new TransientLifetimeManager());
             break;
     }
 }
        /// <summary>
        ///     Register a specific service
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="concrete">The class that implements one or more services.</param>
        /// <param name="lifetime">Lifetime.</param>
        /// <remarks>
        ///     This method will check which interfaces the type implements. It will registered the type as any non <c>System.*</c>
        ///     interfaces. If the class do not implement any interfaces it will be registered as itself.
        /// </remarks>
        public static void RegisterService(this ContainerBuilder builder, Type concrete, ContainerLifetime lifetime)
        {
            var interfaces = concrete.GetInterfaces().Where(x => !x.Namespace.StartsWith("System"));

            if (interfaces.Any())
            {
                switch (lifetime)
                {
                case ContainerLifetime.Scoped:
                    builder.RegisterType(concrete).AsImplementedInterfaces().AsSelf().OwnedByLifetimeScope();
                    break;

                case ContainerLifetime.SingleInstance:
                    builder.RegisterType(concrete).AsImplementedInterfaces().AsSelf().SingleInstance();
                    break;

                case ContainerLifetime.Transient:
                    builder.RegisterType(concrete).AsImplementedInterfaces().AsSelf();
                    break;
                }

                return;
            }


            switch (lifetime)
            {
            case ContainerLifetime.Scoped:
                builder.RegisterType(concrete).AsSelf().InstancePerLifetimeScope();
                break;

            case ContainerLifetime.SingleInstance:
                builder.RegisterType(concrete).AsSelf().SingleInstance();
                break;

            case ContainerLifetime.Transient:
                builder.RegisterType(concrete).AsSelf();
                break;
            }
        }
Esempio n. 15
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="ContainerServiceAttribute" /> class.
 /// </summary>
 /// <param name="lifetime">how long the object should live in the container..</param>
 public ContainerServiceAttribute(ContainerLifetime lifetime)
 {
     Lifetime = lifetime;
 }