Using IoC to modify an object when it's instantiated
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 scen开发者_JS百科ario), 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.
Do you mean other than Autofac's OnActivated
event?
E.g.:
var builder = new ContainerBuilder();
builder.RegisterType<MyComponent>()
.OnActivated(e => DoSomething(e.Instance));
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.
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).
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.
精彩评论