简体   繁体   English

温莎城堡如何“ BeginScope”实现特定的自定义范围生活方式?

[英]How to “BeginScope” for a specific Custom Scoped lifestyle in Castle Windsor?

I've created a custom Scope Accessor (it simply returns the DefaultLifetimeScope) to be able to add a custom scoped lifestyle. 我创建了一个自定义范围访问器 (它只是返回DefaultLifetimeScope),以便能够添加自定义范围的生活方式。 The component is then registered as 然后将该组件注册为

Component
   .For<..>
   .LifestyleScoped<CustomScope>()

However, I am at a loss as how to actually start a new CustomScope scope/lifetime. 但是,我对如何真正启动新的CustomScope范围/生命期感到迷惑。 The documentation shows starting a new scope using 该文档显示了使用开始新作用域

using (Container.BeginScope()) {
   // ..
}

but my goal is to create/begin/start a specific scope, not that for a generic LifestyleScoped() registration. 但我的目标是创建/开始/启动一个特定的范围,而不是针对通用的LifestyleScoped()注册。 The new scope should only affect components explicitly registered to CustomScope; 新作用域应仅影响显式注册到CustomScope的组件。 not generic scoped components or components registered against other scoped lifestyles. 不是通用范围内的组件或针对其他范围内的生活方式注册的组件。

What is the process to begin a (My Custom Scope) scope / lifetime? 开始(我的自定义范围)范围/生存期的过程是什么?

Please link to the relevant documentation; 请链接到相关文档; as I'm asking because I was not able to find it readily. 正如我所问的那样,因为我无法立即找到它。 The code is using Castle Windsor 3.3. 该代码正在使用Castle Windsor 3.3。


Background: 背景:

I am coming from Autofac and am looking for the equivalent of an Instance Per Matching Lifetime Scope to establish a UoW over an EF context. 我来自Autofac,正在寻找与每个匹配生命周期作用域等效的实例,以在EF上下文中建立UoW。 There may be multiple UoWs "per request" and there might be different UoWs for different repositories - I would also like the support for nesting in the future. 每个请求可能有多个UoW,并且对于不同的存储库可能会有不同的UoW-我也希望将来支持嵌套。

While there are numerous articles talking about creating a UoW pattern, they are [all] tied (incorrectly, IMOHO) to some context such as HTTP or WFC request - and that is not what this question is about. 虽然有许多文章讨论如何创建UoW模式,但它们(全部)绑定(错误地是IMOHO)与诸如HTTP或WFC请求之类的某些上下文相关联-而这并不是这个问题。 I am specifically interested in how to start a custom scope that flows down through the call graph and "lives inside" the using block. 我对如何启动一个自定义范围特别感兴趣,该自定义范围向下流过调用图并“驻留” using块。


Notes: 笔记:

The BoundTo() (and LifestyleBoundTo() / LifestyleBoundToNearest() ) lifestyles work against the object-graph (and requires altering the types) and switching to such is not strictly a solution/answer to this question. BoundTo() (和LifestyleBoundTo() / LifestyleBoundToNearest() )生活方式与对象图相对应(并需要更改类型),而切换到该严格意义上并不是严格解决此问题的方法。 However, if a good case can be made for them .. 但是,如果可以为他们做一个好案例..

Container.BeginScope() initializes a new CallContextLifeTimeScope . Container.BeginScope()初始化一个新的CallContextLifeTimeScope

https://github.com/castleproject/Windsor/blob/aa9b8b353ee2e533d586495eec254e216f800c09/src/Castle.Windsor/MicroKernel/Lifestyle/LifestyleExtensions.cs https://github.com/castleproject/Windsor/blob/aa9b8b353ee2e533d586495eec254e216f800c09/src/Castle.Windsor/MicroKernel/Lifestyle/LifestyleExtensions.cs

using Scope = Castle.MicroKernel.Lifestyle.Scoped.CallContextLifetimeScope;

public static class LifestyleExtensions
{
    public static IDisposable BeginScope(this IKernel kernel)
    {
        return new Scope(kernel);
    }
    /* rest of the code removed for simplicity */
}

This extension method doesn't care about your custom ILifetimeScope implementation. 此扩展方法不关心您的自定义ILifetimeScope实现。

Instead of calling Container.BeginScope() , you can just new CustomScope() and dispose it in the end to make sure your "custom scoped objects" released properly. 除了调用Container.BeginScope() ,您还可以使用new CustomScope()并对其进行最后处理,以确保正确释放“自定义范围的对象”。

You can call Container.BeginScope() inside your CustomScope and dispose them both in the end in order to support components registered with default lifetime scope. 您可以在CustomScope内调用Container.BeginScope()并将它们放在最后,以便支持使用默认生存期范围注册的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM