IoC: Existing runtime objects, not container-initialized prerequisites for components

In my case, I use Castle Windsor as my IoC container.

I would like to declare a component when one of the constructor arguments is set to an existing object at runtime, instead of the IoC container creating the object when it was created. For example, an instance of the main form of my application.

I suspect there are two approaches:

  • Create the object through the container for the first time, then configure it with any necessary runtime values ​​so that when the component later asks for Windsor from it, it is ready to use.
  • Create an IFormProvider and a specific FormProvider where the component requires an IFormProvider value, which then requests an instance of the form (i.e. formProvider.Form ).

Any progress on this?

0
source share
1 answer

This is trivial to do with Unity and its free interface; just call the container's RegisterInstance method. It's nice to discuss this in Configuring a Unity container (for v1.1, but 1.2 should be similar).

I have not used Castle Windsor, but apparently AddComponentInstance is equivalent. See Is it possible to register an existing type instance in a Windsor container .

+1
source

Source: https://habr.com/ru/post/1301390/


All Articles