温莎城堡:启动服务时的自定义处理

发布于 2024-08-26 03:17:23 字数 705 浏览 4 评论 0原文

Windsor实例化类型时是否可以执行一些自定义处理?

类似于:

        container.Register(
                  AllTypes.Pick()
                      .FromAssembly(Assembly.GetExecutingAssembly())
                      .BasedOn<MyMarkerInterface>()
                      .WhenInstantiating(instance => // do some stuff with this instance)
                      .Configure(component => component.Startable().LifeStyle.Singleton)
                      .WithService.Base());

目前我们正在使用 IStartable< /a>.由于“开始”代码(即自定义处理)是相同的,因此最好将此逻辑移出每个类。

谢谢! 布莱恩

Is it possible to perform some custom processing when Windsor instantiates a type?

Something similar to:

        container.Register(
                  AllTypes.Pick()
                      .FromAssembly(Assembly.GetExecutingAssembly())
                      .BasedOn<MyMarkerInterface>()
                      .WhenInstantiating(instance => // do some stuff with this instance)
                      .Configure(component => component.Startable().LifeStyle.Singleton)
                      .WithService.Base());

Currently we are using IStartable. Since the "Start" code (i.e. the custom processing) is identical it would be nice to move this logic out of each class.

Thanks!
Brian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2024-09-02 03:17:23

您是指类似 OnCreate 方法吗?

    container.Register(
              AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
                  .BasedOn<MyMarkerInterface>()
                  .WithService.Base()
                  .OnCreate((kernel, instance) => instance.CreatedAt = DateTime.Now)
);

Singleton 是默认的生活方式,因此您不必明确声明。

但请注意,与可启动设施的工作方式相比,此处的行为略有不同。

  • 当组件可启动时,它会尽快由容器本身实例化并启动(当其所有必需的依赖项都可用时)。
  • OnCreate 在您的组件从容器返回之前被调用,但它不会主动创建它。因此,如果从未拉取此组件,则不会调用其 OnCreate。

此外,虽然文档指出 OnCreate 存在于设施中,但事实已不再如此(是的,我们需要更新文档)。此方法开箱即用。

You mean something like OnCreate method?

    container.Register(
              AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
                  .BasedOn<MyMarkerInterface>()
                  .WithService.Base()
                  .OnCreate((kernel, instance) => instance.CreatedAt = DateTime.Now)
);

Singleton is default lifestyle so you don't have to state that explicitly.

Notice however that behavior is slightly different here, as compared to how Startable facility works.

  • when component is startable it gets instantiated and started by the container itself, as soon as possible (when all its required dependencies become available).
  • OnCreate is called before your component is returned from the container but it does not create it proactively. So if you never pull this component, its OnCreate will not be called.

Also while the docs state that OnCreate lives in a facility, it is not true anymore (yeah, we need to update docs). This method will work out of the box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文