InstancePerLifetimeScope与OwnedByLifetimeScope

时间:2014-05-22 12:03:32

标签: autofac

两者之间有什么区别。后者是新的选择吗?我想我以前没见过。

该文件说后者将处理IDisposables,但IIRC也是InstancePerLifetimeScope的行为?

1 个答案:

答案 0 :(得分:2)

OwnedByLifetimeScopeInstancePerLifetimeScope设置了注册的不同属性。如果你看一下源代码,你会看到:

    ... OwnedByLifetimeScope()
    {
        RegistrationData.Ownership = InstanceOwnership.OwnedByLifetimeScope;
        return this;
    }

    ... InstancePerLifetimeScope()
    {
        RegistrationData.Sharing = InstanceSharing.Shared;
        RegistrationData.Lifetime = new CurrentScopeLifetime();
        return this;
    }

对于OwnedByLifetimeScope,这实际上是默认值,因此显式设置它通常是无操作,除非其他一些代码明确地将其更改为ExternallyOwned