zoukankan      html  css  js  c++  java
  • AutoFac使用总结

    注册部分
    使用RegisterType进行注册


    [csharp] view plaincopy
    [Fact]  
      
        public void can_resolve_myclass()  
      
        {  
      
            var builder = new ContainerBuilder();  
      
            builder.RegisterType<MyClass>();  
      
       
      
            IContainer container = builder.Build();  
      
            var myClass = container.Resolve<MyClass>();  
      
            Assert.NotNull(myClass);  
      
        }  


     注册为接口

    [csharp] view plaincopy
    [Fact]  
        public void register_as_interface()  
        {  
            var builder = new ContainerBuilder();  
            builder.Register(c => new MyClass()).As<MyInterface>();  
      
            IContainer container = builder.Build();  
            Assert.NotNull(container.Resolve<MyInterface>());  
            Assert.Throws(typeof (ComponentNotRegisteredException), () => container.Resolve<MyClass>());  
        }  


    使用lambda表达式进行注册


    [csharp] view plaincopy
    [Fact]  
        public void can_register_with_lambda()  
        {  
            var builder = new ContainerBuilder();  
            builder.Register(c => new MyClass());  
      
            IContainer container = builder.Build();  
            var myClass = container.Resolve<MyClass>();  
            Assert.NotNull(myClass);  
        }  


    带构造参数的注册


    [csharp] view plaincopy
    [Fact]  
        public void register_with_parameter()  
        {  
            var builder = new ContainerBuilder();  
            builder.Register(c => new MyParameter());  
            builder.Register(c => new MyClass(c.Resolve<MyParameter>()));  
            IContainer container = builder.Build();  
            Assert.NotNull(container.Resolve<MyClass>());  
        }  


    带属性赋值的注册


    [csharp] view plaincopy
    [Fact]  
        public void register_with_property()  
        {  
            var builder = new ContainerBuilder();  
            builder.Register(c => new MyProperty());  
            builder.Register(  
                c => new MyClass()  
                         {  
                             Property = c.Resolve<MyProperty>()  
                         });  
            IContainer container = builder.Build();  
            var myClass = container.Resolve<MyClass>();  
            Assert.NotNull(myClass);  
            Assert.NotNull(myClass.Property);  
        }  


    Autofac分离了类的创建和使用,这样可以根据输入参数(NamedParameter)动态的选择实现类。


    [csharp] view plaincopy
    [Fact]  
        public void select_an_implementer_based_on_parameter_value()  
        {  
            var builder = new ContainerBuilder();  
            builder.Register<IRepository>((c, p) =>  
                                 {  
                                     var type = p.Named<string>("type");  
                                     if (type == "test")  
                                     {  
                                         return new TestRepository();  
                                     }  
                                     else  
                                     {  
                                         return new DbRepository();  
                                     }  
                                 }).As<IRepository>();  
      
            IContainer container = builder.Build();  
            var repository = container.Resolve<IRepository>(new NamedParameter("type", "test"));  
            Assert.Equal(typeof(TestRepository),repository.GetType());  
        }  


    AufoFac也可以用一个实例来注册,比如用在单例模式情况下:


    [csharp] view plaincopy
    [Fact]  
        public void register_with_instance()  
        {  
            var builder = new ContainerBuilder();  
            builder.RegisterInstance(MyInstance.Instance).ExternallyOwned();  
            IContainer container = builder.Build();  
            var myInstance1 = container.Resolve<MyInstance>();  
            var myInstance2 = container.Resolve<MyInstance>();  
            Assert.Equal(myInstance1,myInstance2);  
        }  


    注册open generic类型


    [csharp] view plaincopy
    [Fact]  
        public void register_open_generic()  
        {  
            var builder = new ContainerBuilder();  
            builder.RegisterGeneric(typeof (MyList<>));  
            IContainer container = builder.Build();  
            var myIntList = container.Resolve<MyList<int>>();  
            Assert.NotNull(myIntList);  
            var myStringList = container.Resolve<MyList<string>>();  
            Assert.NotNull(myStringList);  
        }  


    对于同一个接口,后面注册的实现会覆盖之前的实现


    [csharp] view plaincopy
    [Fact]  
        public void register_order()  
        {  
            var containerBuilder = new ContainerBuilder();  
            containerBuilder.RegisterType<DbRepository>().As<IRepository>();  
            containerBuilder.RegisterType<TestRepository>().As<IRepository>();  
      
            IContainer container = containerBuilder.Build();  
            var repository = container.Resolve<IRepository>();  
            Assert.Equal(typeof(TestRepository), repository.GetType());  
        }  


    如果不想覆盖的话,可以用PreserveExistingDefaults,这样会保留原来注册的实现。


    [csharp] view plaincopy
    [Fact]  
        public void register_order_defaults()  
        {  
            var containerBuilder = new ContainerBuilder();  
            containerBuilder.RegisterType<DbRepository>().As<IRepository>();  
            containerBuilder.RegisterType<TestRepository>().As<IRepository>().PreserveExistingDefaults();  
      
            IContainer container = containerBuilder.Build();  
            var repository = container.Resolve<IRepository>();  
            Assert.Equal(typeof (DbRepository), repository.GetType());  
        }  


    可以用Name来区分不同的实现,代替As方法


    [csharp] view plaincopy
    [Fact]  
        public void register_with_name()  
        {  
            var containerBuilder = new ContainerBuilder();  
            containerBuilder.RegisterType<DbRepository>().Named<IRepository>("DB");  
            containerBuilder.RegisterType<TestRepository>().Named<IRepository>("Test");  
      
            IContainer container = containerBuilder.Build();  
            var dbRepository = container.ResolveNamed<IRepository>("DB");  
            var testRepository = container.ResolveNamed<IRepository>("Test");  
            Assert.Equal(typeof(DbRepository), dbRepository.GetType());  
            Assert.Equal(typeof(TestRepository), testRepository.GetType());  
        }  
    如果一个类有多个构造函数的话,可以在注册时候选择不同的构造函数


    [csharp] view plaincopy
    [Fact]  
        public void choose_constructors()  
        {  
            var builder = new ContainerBuilder();  
            builder.RegisterType<MyParameter>();  
            builder.RegisterType<MyClass>().UsingConstructor(typeof (MyParameter));  
            IContainer container = builder.Build();  
            var myClass = container.Resolve<MyClass>();  
            Assert.NotNull(myClass);  
        }  


    AutoFac可以注册一个Assemble下所有的类,当然,也可以根据类型进行筛选


    [csharp] view plaincopy
    [Fact]  
        public void register_assembly()  
        {  
            var builder = new ContainerBuilder();  
            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).  
                Where(t => t.Name.EndsWith("Repository")).  
                AsImplementedInterfaces();  
      
            IContainer container = builder.Build();  
            var repository = container.Resolve<IRepository>();  
            Assert.NotNull(repository);  

        }  

    注:如果你要想实现autofac的自动属性注入,在属性所在的class中,也要注册到autofac中,获取属性所在的class的实例必须通过autofac获取,也就是绝对不要通过new来获取,因为autofac的存在就是为了让你在一定程度上减少new的使用。

  • 相关阅读:
    调用Android中的软键盘
    EditText图文混排
    android开源框架
    Android 菜单(OptionMenu)
    onRetainNonConfigurationInstance和getLastNonConfigurationInstance
    Android HttpClient基本使用方法
    Eclipse中文注释乱码解决
    mysql怎么定义外键
    javaproject积累——java 反射 invoke
    Floodlight 启动过程分析
  • 原文地址:https://www.cnblogs.com/littlewrong/p/4783102.html
Copyright © 2011-2022 走看看