事件 AutoFac支持三種事件:OnActivating,OnActivated,OnRelease。OnActivating在註冊組件使用之前會被調用,此時可以替換實現類或者進行一些其他的初始化工作,OnActivated在實例化之後會被調用,OnRelease在組件釋放之後會被調用。 此時的輸 ...
事件
AutoFac支持三種事件:OnActivating,OnActivated,OnRelease。OnActivating在註冊組件使用之前會被調用,此時可以替換實現類或者進行一些其他的初始化工作,OnActivated在實例化之後會被調用,OnRelease在組件釋放之後會被調用。
1 public class MyEvent : IDisposable
2 {
3 public MyEvent(string input)
4 {
5 Console.WriteLine(input);
6 }
7
8 public MyEvent()
9 {
10 Console.WriteLine("Init");
11 }
12
13 public void Dispose()
14 {
15 Console.WriteLine("Dispose");
16 }
17 }
1 public void test_event()
2 {
3 var builder = new ContainerBuilder();
4 builder.RegisterType<MyEvent>().
5 OnActivating(e => e.ReplaceInstance(new MyEvent("input"))).
6 OnActivated(e => Console.WriteLine("OnActivated")).
7 OnRelease(e => Console.WriteLine("OnRelease"));
8
9
10 using (IContainer container = builder.Build())
11 {
12 using (var myEvent = container.Resolve<MyEvent>())
13 {
14 }
15 }
16 }
此時的輸出為:
1 Init
2 input
3 OnActivated
4 Dispose
5 OnRelease
利用事件可以在構造對象之後調用對象的方法:
1 [Fact]
2 public void call_method_when_init()
3 {
4 var builder = new ContainerBuilder();
5 builder.RegisterType<MyClassWithMethod>().OnActivating(e => e.Instance.Add(5));
6 IContainer container = builder.Build();
7 Assert.Equal(5, container.Resolve<MyClassWithMethod>().Index);
8 }
9
10 public class MyClassWithMethod
11 {
12 public int Index { get; set; }
13 public void Add(int value)
14 {
15 Index = Index + value;
16 }
17 }
迴圈依賴
迴圈依賴是個比較頭疼的問題,在AutoFac中很多迴圈依賴的場景不被支持:
1 public class ClassA
2 {
3 private readonly ClassB b;
4
5 public ClassA(ClassB b)
6 {
7 this.b = b;
8 }
9 }
10
11 public class ClassB
12 {
13 public ClassA A { get; set; }
14
15 }
16
17 [Fact]
18 public void circular_dependencies_exception()
19 {
20 var builder = new ContainerBuilder();
21 builder.Register(c => new ClassB(){A = c.Resolve<ClassA>()});
22 builder.Register(c => new ClassA(c.Resolve<ClassB>()));
23 IContainer container = builder.Build();
24 Assert.Throws(typeof(DependencyResolutionException), ()=>container.Resolve<ClassA>());
25 }
可以部分的解決這種迴圈依賴的問題,前提是ClassA和ClassB的生命周期不能都是InstancePerDependency
1 [Fact]
2 public void circular_dependencies_ok()
3 {
4 var builder = new ContainerBuilder();
5 builder.RegisterType<ClassB>().
6 PropertiesAutowired(PropertyWiringFlags.AllowCircularDependencies).SingleInstance();
7 builder.Register(c => new ClassA(c.Resolve<ClassB>()));
8 IContainer container = builder.Build();
9 Assert.NotNull(container.Resolve<ClassA>());
10 Assert.NotNull(container.Resolve<ClassB>());
11 Assert.NotNull(container.Resolve<ClassB>().A);
12 }
轉載鏈接:http://niuyi.github.io/blog/2012/04/06/autofac-by-unit-test-2/