结合依赖注入和动态方面编织

发布于 2024-12-15 04:06:49 字数 637 浏览 1 评论 0原文

对于 DI,我使用 Microsoft 的 Unity。 对于动态方面编织,我使用 Rapier-LOOM。

方面编织器要求我使用工厂方法 Weaver.CreateInstance(System.Type) 实例化编织对象,并且不提供交织现有实例的方法。

DI 容器允许我使用 IUnityContainer.Resolve(System.Type) 方法解决依赖关系,该方法解决依赖关系并实例化注入类型的对象。

这两种方法显然是冲突的。 解决此冲突的推荐方法是什么?

到目前为止我的想法:

  • 查询映射并“手动解析”依赖关系(使用 IUnityContainer.Registrations 属性)。创建一个组合的“DI+AOP”机制,在给定要解析的类型的情况下,找到目标映射类型,然后使用 Wea​​ver 进行实例化。
  • 创建我自己的 IUnityContainer 接口实现,该实现使用 Wea​​ver(而不是 Activator)进行实例化

PS

如果我在这里偏离了轨道,冲突可能会发生避免而不是解决 - 请让我知道。

For DI I'm using Microsoft's Unity.
For dynamic Aspect Weaving I'm using Rapier-LOOM.

The aspect weaver requires me to instantiate woven objects using the factory method Weaver.CreateInstance(System.Type), and provides no means of interweaving an existing instance.

The DI container allows me to resolve dependencies by using IUnityContainer.Resolve(System.Type) method, which resolves the dependency and also instantiates an object of the injected type.

These two approaches obviously conflict.
What would be a recommended way to resolve this conflict?

Ideas I've had so far:

  • Inquire the mapping and "manually resolve" the dependency (using the IUnityContainer.Registrations property). Create a combined "DI+AOP" mechanism which - given a type to resolve - finds the target mapped type then instantiates using the Weaver.
  • Create my own implementation of the IUnityContainer interface, which instantiates using the Weaver (instead of the Activator)

P.S.

If I'm off-track here and the conflict could be avoided rather than resolved - please let me know.

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

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

发布评论

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

评论(3

忆离笙 2024-12-22 04:06:49

我对 Rapier-LOOM 不熟悉,所以我只从 Unity 方面来谈谈。有几种不同功能/复杂性的方法。幸运的是,它们都不涉及重新实现 IUnityContainer。

您可以做的最简单的事情是使用 InjectionFactory 注册您想要通过编织器创建的类型。这使您可以指定将执行以创建实例的委托,而不是默认行为。像这样的事情:

  container.RegisterType<ISomething>(
    new InjectionFactory(c => {
        var newObject = (Something)Weaver.CreateInstance(typeof(Something));
        newObject.Property1 = c.Resolve<TypeOfProperty1>();
        newObject.Property2 = c.Resolve<TypeofProperty2>();
        return newObject;
    });

然后当你调用container.Resolve()时,该委托将运行。

第二种方法是创建一个 Unity 扩展,将 Weaver.CreateInstance 调用挂钩到创建链中。您可以在主策略链中使用自定义策略,或者尝试覆盖构建计划。前者要容易得多。

我手头没有创建 Unity 扩展的参考资料,因此我现在不打算尝试在该文本框中键入代码。在网络上查找 Unity 扩展的示例,一旦您了解了事物如何组合在一起,它们就会非常简单。

I'm not familiar with Rapier-LOOM, so I'll just talk from the Unity side of things. There are a couple approaches of varying capability / complexity. None of them involve reimplementing IUnityContainer, luckily.

The simplest thing you could do is register types you want created via the weaver using InjectionFactory. This lets you specify a delegate that will be executed to create the instance instead of the default behavior. Something like this:

  container.RegisterType<ISomething>(
    new InjectionFactory(c => {
        var newObject = (Something)Weaver.CreateInstance(typeof(Something));
        newObject.Property1 = c.Resolve<TypeOfProperty1>();
        newObject.Property2 = c.Resolve<TypeofProperty2>();
        return newObject;
    });

Then when you call container.Resolve(), that delegate will run.

The second approach would be to create a Unity extension that hooks in the Weaver.CreateInstance call into the creation chain. You could either use a custom strategy in the main strategy chain, or try overriding the build plan. The former is much easier.

I don't have my references for creating Unity extensions at hand, so I'm not going to try to type code into this textbox right now. Look around the web for examples of Unity extensions, they're pretty straightforward once you understand how things go together.

不奢求什么 2024-12-22 04:06:49

浏览一下 LOOM codeplex 页面,似乎没有提供任何使用 Unity 方法拦截无法完成的功能。从这里开始阅读:面向方面的编程、拦截和 Unity 2.0

A glance at the LOOM codeplex page doesn’t appear to provide any features that can’t be done using Unity method interception. Start reading here: Aspect-Oriented Programming, Interception and Unity 2.0

(り薆情海 2024-12-22 04:06:49

这是一个糟糕的论点。如果我说 Microsoft 的 Unity 不提供任何 .NET 框架无法完成的功能,这也是一样的。问题是,对于我的问题来说,最好的编程模型是什么。答案可能是实现需求所需的代码量。
AOP,尤其是 Rapier-LOOM.NET 不是一个简单的方法启动工具。 AOP 的目标是封装横切关注点。为此,您需要建议、介绍、连接点变量、基于代码的注释等。如果我想实现的不仅仅是一个简单的跟踪示例,您需要比方法初始更强大的概念。

this is a poor argument. It's the same if I say Microsoft's Unity doesn't provide any features that can't be done with the .NET-framework. The question is, what is the best programming model for my problem. A answer could be the amount of code, that need to implement the requirements.
AOP, especially Rapier-LOOM.NET is not a simple method inception facility. The goal of AOP is to encapsulate crosscutting concerns. For that you need advices, introductions, join-point variables, code-based annotations etc. If I want to implement more than a simple Tracing example you need more powerful concepts than method inception.

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