AutoFac使用方法总结一
程序员文章站
2022-06-27 19:43:33
AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系。在使用方面主要是register和resolve两类操作。 这篇文章用单元测试的形式列举了AutoFac的常用使用方法: 注册部分 使用RegisterType进行注册 注册为接口 使用lambda表达式进行注册 带构 ......
AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系。在使用方面主要是register和resolve两类操作。 这篇文章用单元测试的形式列举了AutoFac的常用使用方法:
注册部分
使用RegisterType进行注册
1 [Fact] 2 public void can_resolve_myclass() 3 { 4 var builder = new ContainerBuilder(); 5 builder.RegisterType<MyClass>(); 6 7 IContainer container = builder.Build(); 8 var myClass = container.Resolve<MyClass>(); 9 Assert.NotNull(myClass); 10 }
注册为接口
1 [Fact] 2 public void register_as_interface() 3 { 4 var builder = new ContainerBuilder(); 5 builder.Register(c => new MyClass()).As<MyInterface>(); 6 7 IContainer container = builder.Build(); 8 Assert.NotNull(container.Resolve<MyInterface>()); 9 Assert.Throws(typeof (ComponentNotRegisteredException), () => container.Resolve<MyClass>()); 10 }
使用lambda表达式进行注册
1 [Fact] 2 public void can_register_with_lambda() 3 { 4 var builder = new ContainerBuilder(); 5 builder.Register(c => new MyClass()); 6 7 IContainer container = builder.Build(); 8 var myClass = container.Resolve<MyClass>(); 9 Assert.NotNull(myClass); 10 }
带构造参数的注册
1 [Fact] 2 public void register_with_parameter() 3 { 4 var builder = new ContainerBuilder(); 5 builder.Register(c => new MyParameter()); 6 builder.Register(c => new MyClass(c.Resolve<MyParameter>())); 7 IContainer container = builder.Build(); 8 Assert.NotNull(container.Resolve<MyClass>()); 9 }
带属性赋值的注册
1 [Fact] 2 public void register_with_property() 3 { 4 var builder = new ContainerBuilder(); 5 builder.Register(c => new MyProperty()); 6 builder.Register( 7 c => new MyClass() 8 { 9 Property = c.Resolve<MyProperty>() 10 }); 11 IContainer container = builder.Build(); 12 var myClass = container.Resolve<MyClass>(); 13 Assert.NotNull(myClass); 14 Assert.NotNull(myClass.Property); 15 }
Autofac分离了类的创建和使用,这样可以根据输入参数(NamedParameter)动态的选择实现类。
1 [Fact] 2 public void select_an_implementer_based_on_parameter_value() 3 { 4 var builder = new ContainerBuilder(); 5 builder.Register<IRepository>((c, p) => 6 { 7 var type = p.Named<string>("type"); 8 if (type == "test") 9 { 10 return new TestRepository(); 11 } 12 else 13 { 14 return new DbRepository(); 15 } 16 }).As<IRepository>(); 17 18 IContainer container = builder.Build(); 19 var repository = container.Resolve<IRepository>(new NamedParameter("type", "test")); 20 Assert.Equal(typeof(TestRepository),repository.GetType()); 21 }
AufoFac也可以用一个实例来注册,比如用在单例模式情况下:
1 [Fact] 2 public void register_with_instance() 3 { 4 var builder = new ContainerBuilder(); 5 builder.RegisterInstance(MyInstance.Instance).ExternallyOwned(); 6 IContainer container = builder.Build(); 7 var myInstance1 = container.Resolve<MyInstance>(); 8 var myInstance2 = container.Resolve<MyInstance>(); 9 Assert.Equal(myInstance1,myInstance2); 10 }
注册open generic类型
1 [Fact] 2 public void register_open_generic() 3 { 4 var builder = new ContainerBuilder(); 5 builder.RegisterGeneric(typeof (MyList<>)); 6 IContainer container = builder.Build(); 7 var myIntList = container.Resolve<MyList<int>>(); 8 Assert.NotNull(myIntList); 9 var myStringList = container.Resolve<MyList<string>>(); 10 Assert.NotNull(myStringList); 11 }
对于同一个接口,后面注册的实现会覆盖之前的实现
1 [Fact] 2 public void register_order() 3 { 4 var containerBuilder = new ContainerBuilder(); 5 containerBuilder.RegisterType<DbRepository>().As<IRepository>(); 6 containerBuilder.RegisterType<TestRepository>().As<IRepository>(); 7 8 IContainer container = containerBuilder.Build(); 9 var repository = container.Resolve<IRepository>(); 10 Assert.Equal(typeof(TestRepository), repository.GetType()); 11 }
如果不想覆盖的话,可以用PreserveExistingDefaults,这样会保留原来注册的实现。
1 [Fact] 2 public void register_order_defaults() 3 { 4 var containerBuilder = new ContainerBuilder(); 5 containerBuilder.RegisterType<DbRepository>().As<IRepository>(); 6 containerBuilder.RegisterType<TestRepository>().As<IRepository>().PreserveExistingDefaults(); 7 8 IContainer container = containerBuilder.Build(); 9 var repository = container.Resolve<IRepository>(); 10 Assert.Equal(typeof (DbRepository), repository.GetType()); 11 }
可以用Name来区分不同的实现,代替As方法
1 [Fact] 2 public void register_with_name() 3 { 4 var containerBuilder = new ContainerBuilder(); 5 containerBuilder.RegisterType<DbRepository>().Named<IRepository>("DB"); 6 containerBuilder.RegisterType<TestRepository>().Named<IRepository>("Test"); 7 8 IContainer container = containerBuilder.Build(); 9 var dbRepository = container.ResolveNamed<IRepository>("DB"); 10 var testRepository = container.ResolveNamed<IRepository>("Test"); 11 Assert.Equal(typeof(DbRepository), dbRepository.GetType()); 12 Assert.Equal(typeof(TestRepository), testRepository.GetType()); 13 }
如果一个类有多个构造函数的话,可以在注册时候选择不同的构造函数
1 [Fact] 2 public void choose_constructors() 3 { 4 var builder = new ContainerBuilder(); 5 builder.RegisterType<MyParameter>(); 6 builder.RegisterType<MyClass>().UsingConstructor(typeof (MyParameter)); 7 IContainer container = builder.Build(); 8 var myClass = container.Resolve<MyClass>(); 9 Assert.NotNull(myClass); 10 }
AutoFac可以注册一个Assemble下所有的类,当然,也可以根据类型进行筛选
1 [Fact] 2 public void register_assembly() 3 { 4 var builder = new ContainerBuilder(); 5 builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()). 6 Where(t => t.Name.EndsWith("Repository")). 7 AsImplementedInterfaces(); 8 9 IContainer container = builder.Build(); 10 var repository = container.Resolve<IRepository>(); 11 Assert.NotNull(repository); 12 }
参考链接:http://niuyi.github.io/blog/2012/04/06/autofac-by-unit-test/
上一篇: 物归原主,分手