Autofac 触发生命周期创建
Autofac trigger lifetimescope creation
有什么方法可以在解析组件时创建新的lifetimescope
?
意思类似于
container.registerType().As().TriggersNewScope()
还有其他方法吗?除了依赖 ILifetimeScope
并直接通过它解决?
如果没有,有没有办法抽象ILifetimeScope
?我不希望应用程序的那部分引用 Autofac..
谢谢
实际上并没有 "component triggers lifetime scope creation" 这样的事情。但是,您可能对 Owned<T>
感兴趣。这会导致一个组件在它自己的小生命周期内被解析。
您需要将重复使用的服务注册为 InstancePerOwned。
剩下的未解决部分是避免引用 Autofac,这里是一个可能的解决方法:
- 在您的 Autofac 不可知程序集中定义您的 IOwned 接口。
在 Autofac 感知组合根中定义和注册开放通用 CustomOwned 实现:
class CustomOwned<T> : IOwned<T>, IDisposable
{
public T Value { get { return _owned.Value; } }
public CustomOwned(Autofac.Owned<T> owned)
{
_owned = owned;
}
// IDisposable implementation and whatever additional stuff
}
在您的类型中使用 IOwned 而不是 Owned。
有什么方法可以在解析组件时创建新的lifetimescope
?
意思类似于
container.registerType().As().TriggersNewScope()
还有其他方法吗?除了依赖 ILifetimeScope
并直接通过它解决?
如果没有,有没有办法抽象ILifetimeScope
?我不希望应用程序的那部分引用 Autofac..
谢谢
实际上并没有 "component triggers lifetime scope creation" 这样的事情。但是,您可能对 Owned<T>
感兴趣。这会导致一个组件在它自己的小生命周期内被解析。
您需要将重复使用的服务注册为 InstancePerOwned。
剩下的未解决部分是避免引用 Autofac,这里是一个可能的解决方法:
- 在您的 Autofac 不可知程序集中定义您的 IOwned 接口。
在 Autofac 感知组合根中定义和注册开放通用 CustomOwned 实现:
class CustomOwned<T> : IOwned<T>, IDisposable { public T Value { get { return _owned.Value; } } public CustomOwned(Autofac.Owned<T> owned) { _owned = owned; } // IDisposable implementation and whatever additional stuff }
在您的类型中使用 IOwned 而不是 Owned。