簡體   English   中英

嘗試了解生命周期范圍在IoC(使用Autofac)中意味着什么?

[英]Try to understand what lifetime scope means in IoC (with Autofac)?

最初,我確實以保密的方式理解了我的理解,但是通過一個使用Autofac簡單示例,看來我可能理解不正確,這是我嘗試過的代碼:

//register the service
autofacBuilder.RegisterType<MyService>()
              .As<IMyService>().InstancePerLifetimeScope();
//testing code
void _test1()
{
   var myService = autofacContainer.Resolve<IMyService>();
}
void _test2()
{
    _test1();
    var myService = autofacContainer.Resolve<IMyService>();
}

通過運行_test2()對其進行測試,您可以簡單地檢查兩種方法中解析的實例。

因此,與上面的代碼,我理解myService_test1myService_test2應該是不同的。 因為我認為_test1myService的生存期范圍應僅在該方法中,而_test2myService的生存期范圍_test2應在_test2中。 因此,我們在這里有2個不同的作用域 ,但是以某種方式解析的myService實例是相同的。

因此,您能否向我解釋該問題, lifetime scope在這里到底意味着什么? 在同一個班級里? 或者更大的東西?

您混淆了c#范圍autofac的范圍 這就像比較蘋果和籬笆。 :)它們只是不同而已,彼此無關。

因此,為澄清起見,請查看下面的基本示例。 請注意,如果您是像在示例1中那樣啟動示波器的人,那么實際上應該由您銷毀示波器。在其他示例中,為簡潔起見,我將其跳過了。

// example 1
autofacBuilder.RegisterType<MyService>().As<IMyService>().InstancePerLifetimeScope();
var container = autofacBuilder.Build();

void _test1(IComponentContext scope){
    var myService = scope.Resolve<IMyService>();
}

void _test2(IComponentContext scope){
    // the same scope is used here and in _test1()
    // this means that the service instance will be the same here and there
    _test1(scope);
    var myService = scope.Resolve<IMyService>();
}

// it's only here that DI lifetime scope starts
using (var scope = container.BeginLifetimeScope()) {
    _test2(scope);
}



// example 2
autofacBuilder.RegisterType<MyService>().As<IMyService>().InstancePerLifetimeScope();
var container = autofacBuilder.Build();

void _test1(IComponentContext scope){
    var myService = scope.Resolve<IMyService>();
}

void _test2(IComponentContext scope){
    // now new scope is used in _test1() call
    // this means that instances will be different here and there since they are resolved from different scopes
    _test1(scope.BeginLifetimeScope());
    var myService = scope.Resolve<IMyService>();
}

var scope = container.BeginLifetimeScope();
_test2(scope);



// example 3
// NOTE THIS!
autofacBuilder.RegisterType<MyService>().As<IMyService>().InstancePerDependency();
var container = autofacBuilder.Build();

void _test1(IComponentContext scope){
    var myService = scope.Resolve<IMyService>();
}

void _test2(IComponentContext scope){
    // the same scope is used here and in _test1()
    // but now service instances will be different even though they are resolved from the same scope
    // since registration directs to create new instance each time the service is requested.
    _test1(scope);
    var myService = scope.Resolve<IMyService>();
}

var scope = container.BeginLifetimeScope();
_test2(scope);



// example 4
autofacBuilder.RegisterType<MyService>().As<IMyService>().SingleInstance();
var container = autofacBuilder.Build();

void _test0(IComponentContext scope){
    var myService = scope.Resolve<IMyService>();
}

void _test1(IComponentContext scope){
    _test0(scope.BeginLifetimeScope());
    var myService = scope.Resolve<IMyService>();
}

void _test2(IComponentContext scope){
    // all the scopes used here and in other calls are different now
    // but the service instance will be the same in all of them even though it is requested from different scopes
    // since registration directs to get the same instance each time the service is requested regardless of the lifetime scope.
    _test1(scope.BeginLifetimeScope());
    var myService = scope.Resolve<IMyService>();
}

var scope = container.BeginLifetimeScope();
_test2(scope);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM