public void Disposable()
 {
     IFarseerContainer container = new FarseerContainer();
     var register = container.CreateRegister();
     register.RegisterType<DisposableTestClass>().AsScopedLifetime();
     using (var resolver = container.CreateResolver())
     {
         using (var scoped = resolver.CreateScopedResolver())
         {
             var dis = resolver.Resolve<DisposableTestClass>();
             var dis2 = scoped.Resolve<DisposableTestClass>();
         }
     }
 }
Example #2
0
        public void AsDynamicProxy()
        {
            IFarseerContainer container = new FarseerContainer();
            var register = container.CreateRegister();

            ///注册为动态代理
            register.RegisterType <UserRepository>().AsDynamicProxy();

            using (var resolver = container.CreateResolver())
            {
                var proxy = resolver.Resolve <UserRepository>();
                proxy.GetById(1);
                Assert.IsNotNull(proxy);
            }
        }
Example #3
0
        public void Disposable()
        {
            IFarseerContainer container = new FarseerContainer();
            var register = container.CreateRegister();

            register.RegisterType <DisposableTestClass>().AsScopedLifetime();
            using (var resolver = container.CreateResolver())
            {
                using (var scoped = resolver.CreateScopedResolver())
                {
                    var dis  = resolver.Resolve <DisposableTestClass>();
                    var dis2 = scoped.Resolve <DisposableTestClass>();
                }
            }
        }
        public void AsDynamicProxy()
        {
            IFarseerContainer container = new FarseerContainer();
            var register = container.CreateRegister();

            ///注册为动态代理
            register.RegisterType<UserRepository>().AsDynamicProxy();

            using (var resolver = container.CreateResolver())
            {
                var proxy = resolver.Resolve<UserRepository>();
                proxy.GetById(1);
                Assert.IsNotNull(proxy);
            }
        }
Example #5
0
        public void Scoped()
        {
            ///创建注册器
            IDependencyRegister register = container.CreateRegister();

            ///注册瞬时生命周期的类型
            register.RegisterType <IUserRepository, UserRepository>().AsScopedLifetime();
            ///创建解析器
            using (IDependencyResolver resolver = container.CreateResolver())
            {
                IUserRepository userRepository1 = resolver.Resolve <IUserRepository>();
                ///创建作用域
                using (IScopedResolver scoped = resolver.CreateScopedResolver())
                {
                    IUserRepository scopedRepository1 = scoped.Resolve <IUserRepository>();
                    IUserRepository scopedRepository2 = scoped.Resolve <IUserRepository>();

                    Assert.AreNotEqual <IUserRepository>(userRepository1, scopedRepository1);

                    Assert.AreEqual <IUserRepository>(scopedRepository1, scopedRepository2);
                    ///创建作用域
                    using (IScopedResolver scoped1 = scoped.CreateScopedResolver())
                    {
                        IUserRepository scopedRepository3 = scoped1.Resolve <IUserRepository>();
                        IUserRepository scopedRepository4 = scoped1.Resolve <IUserRepository>();

                        Assert.AreNotEqual <IUserRepository>(userRepository1, scopedRepository3);
                        Assert.AreNotEqual <IUserRepository>(scopedRepository1, scopedRepository3);

                        Assert.AreEqual <IUserRepository>(scopedRepository3, scopedRepository4);
                    }
                }
            }

            IFarseerContainer c1 = new FarseerContainer();
            ///创建注册器
            IDependencyRegister register1 = c1.CreateRegister();

            ///注册瞬时生命周期的类型
            register1.RegisterType <IUserRepository, UserRepository>().AsScopedLifetime();
            ///创建解析器
            using (IDependencyResolver resolver = c1.CreateResolver())
            {
                IUserRepository userRepository1 = resolver.Resolve <IUserRepository>();
                ///创建作用域
                using (IScopedResolver scoped = resolver.CreateScopedResolver())
                {
                    IUserRepository scopedRepository1 = scoped.Resolve <IUserRepository>();
                    IUserRepository scopedRepository2 = scoped.Resolve <IUserRepository>();

                    Assert.AreNotEqual <IUserRepository>(userRepository1, scopedRepository1);

                    Assert.AreEqual <IUserRepository>(scopedRepository1, scopedRepository2);
                    ///创建作用域
                    using (IScopedResolver scoped1 = scoped.CreateScopedResolver())
                    {
                        IUserRepository scopedRepository3 = scoped1.Resolve <IUserRepository>();
                        IUserRepository scopedRepository4 = scoped1.Resolve <IUserRepository>();

                        Assert.AreNotEqual <IUserRepository>(userRepository1, scopedRepository3);
                        Assert.AreNotEqual <IUserRepository>(scopedRepository1, scopedRepository3);

                        Assert.AreEqual <IUserRepository>(scopedRepository3, scopedRepository4);
                    }
                }
            }
        }
        public void Scoped()
        {
            ///创建注册器
            IDependencyRegister register = container.CreateRegister();
            ///注册瞬时生命周期的类型
            register.RegisterType<IUserRepository, UserRepository>().AsScopedLifetime();
            ///创建解析器
            using (IDependencyResolver resolver = container.CreateResolver())
            {
                IUserRepository userRepository1 = resolver.Resolve<IUserRepository>();
                ///创建作用域
                using (IScopedResolver scoped = resolver.CreateScopedResolver())
                {
                    IUserRepository scopedRepository1 = scoped.Resolve<IUserRepository>();
                    IUserRepository scopedRepository2 = scoped.Resolve<IUserRepository>();

                    Assert.AreNotEqual<IUserRepository>(userRepository1, scopedRepository1);

                    Assert.AreEqual<IUserRepository>(scopedRepository1, scopedRepository2);
                    ///创建作用域
                    using (IScopedResolver scoped1 = scoped.CreateScopedResolver())
                    {
                        IUserRepository scopedRepository3 = scoped1.Resolve<IUserRepository>();
                        IUserRepository scopedRepository4 = scoped1.Resolve<IUserRepository>();

                        Assert.AreNotEqual<IUserRepository>(userRepository1, scopedRepository3);
                        Assert.AreNotEqual<IUserRepository>(scopedRepository1, scopedRepository3);

                        Assert.AreEqual<IUserRepository>(scopedRepository3, scopedRepository4);
                    }
                }
            }

            IFarseerContainer c1 = new FarseerContainer();
            ///创建注册器
            IDependencyRegister register1 = c1.CreateRegister();
            ///注册瞬时生命周期的类型
            register1.RegisterType<IUserRepository, UserRepository>().AsScopedLifetime();
            ///创建解析器
            using (IDependencyResolver resolver = c1.CreateResolver())
            {
                IUserRepository userRepository1 = resolver.Resolve<IUserRepository>();
                ///创建作用域
                using (IScopedResolver scoped = resolver.CreateScopedResolver())
                {
                    IUserRepository scopedRepository1 = scoped.Resolve<IUserRepository>();
                    IUserRepository scopedRepository2 = scoped.Resolve<IUserRepository>();

                    Assert.AreNotEqual<IUserRepository>(userRepository1, scopedRepository1);

                    Assert.AreEqual<IUserRepository>(scopedRepository1, scopedRepository2);
                    ///创建作用域
                    using (IScopedResolver scoped1 = scoped.CreateScopedResolver())
                    {
                        IUserRepository scopedRepository3 = scoped1.Resolve<IUserRepository>();
                        IUserRepository scopedRepository4 = scoped1.Resolve<IUserRepository>();

                        Assert.AreNotEqual<IUserRepository>(userRepository1, scopedRepository3);
                        Assert.AreNotEqual<IUserRepository>(scopedRepository1, scopedRepository3);

                        Assert.AreEqual<IUserRepository>(scopedRepository3, scopedRepository4);
                    }
                }
            }
        }