在实例化对象时使用 IoC 修改对象

发布于 2024-10-10 20:50:11 字数 431 浏览 5 评论 0原文

我需要一些管道胶带...我有一个场景,其中由两个单独的数据库(在不同的服务器上)支持的两个对象需要相互关联。我已经有了对象图,但由于一些其他应用程序“功能”,我不能依赖 O/RM 来映射关系;当我获得一个或另一个类实例时,我必须手动填充该特定关系。

我有几个扩展方法可以帮助实现此目的,但现在我正在寻找应用程序中需要调用该扩展方法的所有位置的漫长道路,我正在尝试找到一种方法当其中一个对象被实例化时,使其在全局级别发生。由于前面提到的“功能”创建的原因,无法在对象构造函数中执行此操作。

有没有办法让我在实例化特定对象(不一定是 DI 类型场景)时使用 IoC 容器来捕获/捕获,并在使用该对象之前对其进行修改?就像某种“OnActivated”事件处理程序?

我知道,有点像抓住救命稻草……就其价值而言,这个应用程序是 MVC 2,使用 NHibernate、Autofac 以及大量的吐槽和软骨。

I need some duct tape... I have a scenario where two objects, backed by two separate databases (on separate servers), have need to relate to one another. I have the object graph in place, but due to some other application "features", I can't rely on the O/RM to map the relationship; I have to manually fill that particular relationship when I get one or the other class instance.

I have a couple of extension methods that assist with this, but now that I'm staring down the long road of finding all the places in my app where I need to now call that extension method, I'm trying to find a way to make it happen at a global level when one of the objects is instantiated. Can't do it in the object constructors for reasons created by the previously mentioned "features".

Is there a way for me to use the IoC container to catch/trap when a specific object is instantiated (not necessarily a DI type scenario), and make modifications on that object prior to it's use? Like some kind of "OnActivated" event handler?

Sort of grasping at straws, I know... For what it's worth, this app is MVC 2, using NHibernate, Autofac, and a lot of spit and gristle.

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

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

发布评论

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

评论(4

万劫不复 2024-10-17 20:50:12

您指的是 Autofac 的 OnActivated 事件以外的事件吗?

例如:

var builder = new ContainerBuilder();

builder.RegisterType<MyComponent>()
    .OnActivated(e => DoSomething(e.Instance));

Do you mean other than Autofac's OnActivated event?

E.g.:

var builder = new ContainerBuilder();

builder.RegisterType<MyComponent>()
    .OnActivated(e => DoSomething(e.Instance));
尽揽少女心 2024-10-17 20:50:12

给出了几个适当的答案,但也想发布我的解决方案,该解决方案完全绕过了 IoC。代表们来救援!

由于我实际上有能力修改相关的主要实体,因此我向其中添加了一个静态 Func 属性,该属性期望从我的第二个程序集/数据库返回该类的实例。我还为流氓类型创建了一个 getter,如果委托不为空,它就会简单地调用该委托。在我的应用程序启动中(在我的例子中为 global.asax),我将静态 Func 设置为适当的方法。瞧!我拥有了我渴望的对象图,并且有效地绕过了现有架构的限制,而没有散布不必要的依赖项。

Couple of appropriate answers given, but wanted to post my solution as well, which bypassed IoC entirely. Delegates to the rescue!

Since I did in fact have the ability to modify the main entity in question, I added to it a static Func<T> property that expects to return an instance of the class from my 2nd assembly/database. I also created a getter for the rogue type, that simply invokes the delegate if it's not null. In my application startup (global.asax in my case), I then set the static Func to an appropriate method. Voila! I have the object graph I crave and effectively bypassed the limitations of the existing architecture without needless dependencies littered throughout.

埋情葬爱 2024-10-17 20:50:12

您认为您想要的实际上并不是 IoC 的工作,并且实际上没有任何方法可以在运行时的任何位置观察某种类型的对象的实例化,而无需集中创建这些对象的位置。

如果您需要实例化一个对象或另一个对象,并在此过程中确保另一个对象被第一个对象实例化并引用,我认为最好的模式是工厂,可能由 IoC 通过注册工厂方法来使用。然后,创建这些对象中的任何一个的对象将被赋予工厂方法或工厂本身的句柄(工厂可以在容器中为单例范围,并且只在任何地方都有对一个工厂的引用)。

What you think you want is not really IoC's job, and there isn't really any way to observe the instantiation of a type of object anywhere in the runtime without centralizing where those objects are created.

If you need to instantiate one object or the other, and during that process ensure that the other object is instantiated and referenced by the first, I think the best pattern would be a Factory, possibly utilized by IoC through registering the factory method. Then, objects that create either of these objects would be given a handle to the factory method, or the factory itself (the factory can be singleton-scoped in the container and just have a reference to the one factory everywhere).

蓝海似她心 2024-10-17 20:50:12

我不认为你可以使用 IOC 来做这样的事情。我假设您无法更改该类的实现。您也许可以使用 postsharp 等工具来实现此目的。有了它,您应该能够拦截对构造函数的调用并向它们注入自定义代码。

I don't think you can do that kind of things using IOC. I am assuming that you cannot change the implementation of that class. You might be able to achieve this using a tool like postsharp. With it, you should be able to intercept calls to the constructor and inject custom code to them.

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