本文原著:牛毅 原文路径 http://niuyi.github.io/blog/2012/04/06/autofac-by-unit-test/
理解IOC容器请看下图:
没有使用IOC容器的情况下:
使用IOC容器的情况下:
去掉IOC容器的情况后:
IOC容器又像一个插座,将电输送到需要的每一处。需要充电的话,就连接,不需要就不连接,节省资源,不用时时刻刻连上电源了。省电的,哈哈。
使用IOC容器的好处:
1) 可维护性比较好
2) 便于单元测试,调试程序和诊断故障
2) 可复用性好
实现组件之间的解耦,提高程序的灵活性和可维护性
AutoFac使用方法总结:Part I
APR 6TH, 2012 | COMMENTS
AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系。在使用方面主要是register和resolve两类操作。 这篇文章用单元测试的形式列举了AutoFac的常用使用方法:
注册部分
使用RegisterType进行注册
1
2
3
4
5
6
7
8
9
10
|
[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);
}
|
注册为接口
1
2
3
4
5
6
7
8
9
10
|
[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表达式进行注册
1
2
3
4
5
6
7
8
9
10
|
[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);
}
|
带构造参数的注册
1
2
3
4
5
6
7
8
9
|
[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>());
}
|
带属性赋值的注册
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[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)动态的选择实现类。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[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也可以用一个实例来注册,比如用在单例模式情况下:
1
2
3
4
5
6
7
8
9
10
|
[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类型
1
2
3
4
5
6
7
8
9
10
11
|
[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);
}
|
对于同一个接口,后面注册的实现会覆盖之前的实现
1
2
3
4
5
6
7
8
9
10
11
|
[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,这样会保留原来注册的实现。
1
2
3
4
5
6
7
8
9
10
11
|
[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方法
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[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());
}
|
如果一个类有多个构造函数的话,可以在注册时候选择不同的构造函数
1
2
3
4
5
6
7
8
9
10
|
[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下所有的类,当然,也可以根据类型进行筛选
1
2
3
4
5
6
7
8
9
10
11
12
|
[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使用方法总结:Part II
APR 6TH, 2012 | COMMENTS
事件
AutoFac支持三种事件:OnActivating,OnActivated,OnRelease。OnActivating在注册组件使用之前会被调用,此时可以替换实现类或者进行一些其他的初始化工作,OnActivated在实例化之后会被调用,OnRelease在组件释放之后会被调用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class MyEvent : IDisposable
{
public MyEvent(string input)
{
Console.WriteLine(input);
}
public MyEvent()
{
Console.WriteLine("Init");
}
public void Dispose()
{
Console.WriteLine("Dispose");
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public void test_event()
{
var builder = new ContainerBuilder();
builder.RegisterType<MyEvent>().
OnActivating(e => e.ReplaceInstance(new MyEvent("input"))).
OnActivated(e => Console.WriteLine("OnActivated")).
OnRelease(e => Console.WriteLine("OnRelease"));
using (IContainer container = builder.Build())
{
using (var myEvent = container.Resolve<MyEvent>())
{
}
}
}
|
此时的输出为:
1
2
3
4
5
|
Init
input
OnActivated
Dispose
OnRelease
|
利用事件可以在构造对象之后调用对象的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[Fact]
public void call_method_when_init()
{
var builder = new ContainerBuilder();
builder.RegisterType<MyClassWithMethod>().OnActivating(e => e.Instance.Add(5));
IContainer container = builder.Build();
Assert.Equal(5, container.Resolve<MyClassWithMethod>().Index);
}
public class MyClassWithMethod
{
public int Index { get; set; }
public void Add(int value)
{
Index = Index + value;
}
}
|
循环依赖
循环依赖是个比较头疼的问题,在AutoFac中很多循环依赖的场景不被支持:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class ClassA
{
private readonly ClassB b;
public ClassA(ClassB b)
{
this.b = b;
}
}
public class ClassB
{
public ClassA A { get; set; }
}
[Fact]
public void circular_dependencies_exception()
{
var builder = new ContainerBuilder();
builder.Register(c => new ClassB(){A = c.Resolve<ClassA>()});
builder.Register(c => new ClassA(c.Resolve<ClassB>()));
IContainer container = builder.Build();
Assert.Throws(typeof(DependencyResolutionException), ()=>container.Resolve<ClassA>());
}
|
可以部分的解决这种循环依赖的问题,前提是ClassA和ClassB的生命周期不能都是InstancePerDependency
1
2
3
4
5
6
7
8
9
10
11
12
|
[Fact]
public void circular_dependencies_ok()
{
var builder = new ContainerBuilder();
builder.RegisterType<ClassB>().
PropertiesAutowired(PropertyWiringFlags.AllowCircularDependencies).SingleInstance();
builder.Register(c => new ClassA(c.Resolve<ClassB>()));
IContainer container = builder.Build();
Assert.NotNull(container.Resolve<ClassA>());
Assert.NotNull(container.Resolve<ClassB>());
Assert.NotNull(container.Resolve<ClassB>().A);
}
|
AutoFac使用方法总结:Part III
APR 6TH, 2012 | COMMENTS
生命周期
AutoFac中的生命周期概念非常重要,AutoFac也提供了强大的生命周期管理的能力。
AutoFac定义了三种生命周期:
Per Dependency
Single Instance
Per Lifetime Scope
Per Dependency为默认的生命周期,也被称为’transient’或’factory’,其实就是每次请求都创建一个新的对象
1
2
3
4
5
6
7
8
9
10
|
[Fact]
public void per_dependency()
{
var builder = new ContainerBuilder();
builder.RegisterType<MyClass>().InstancePerDependency();
IContainer container = builder.Build();
var myClass1 = container.Resolve<MyClass>();
var myClass2 = container.Resolve<MyClass>();
Assert.NotEqual(myClass1,myClass2);
}
|
Single Instance也很好理解,就是每次都用同一个对象
1
2
3
4
5
6
7
8
9
10
11
12
|
[Fact]
public void single_instance()
{
var builder = new ContainerBuilder();
builder.RegisterType<MyClass>().SingleInstance();
IContainer container = builder.Build();
var myClass1 = container.Resolve<MyClass>();
var myClass2 = container.Resolve<MyClass>();
Assert.Equal(myClass1,myClass2);
}
|
Per Lifetime Scope,同一个Lifetime生成的对象是同一个实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[Fact]
public void per_lifetime_scope()
{
var builder = new ContainerBuilder();
builder.RegisterType<MyClass>().InstancePerLifetimeScope();
IContainer container = builder.Build();
var myClass1 = container.Resolve<MyClass>();
var myClass2 = container.Resolve<MyClass>();
ILifetimeScope inner = container.BeginLifetimeScope();
var myClass3 = inner.Resolve<MyClass>();
var myClass4 = inner.Resolve<MyClass>();
Assert.Equal(myClass1,myClass2);
Assert.NotEqual(myClass2,myClass3);
Assert.Equal(myClass3,myClass4);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[Fact]
public void life_time_and_dispose()
{
var builder = new ContainerBuilder();
builder.RegisterType<Disposable>();
using (IContainer container = builder.Build())
{
var outInstance = container.Resolve<Disposable>(new NamedParameter("name", "out"));
using(var inner = container.BeginLifetimeScope())
{
var inInstance = container.Resolve<Disposable>(new NamedParameter("name", "in"));
}//inInstance dispose here
}//out dispose here
}
|