Search code examples
inversion-of-controlioc-containerspring.net

Notify parent context object about child context object creation


this is a situation I would like to implement.

public class ComponentRepository 
{
    public void Register(IComponent component) 
    {
        // store component in collection
    }
}
<!-- root context -->    
<object id="Repository" type="NameSpace.ComponentRepository" />

<!-- child context (0 - n contexts) -->
<object id="Component" type="NameSpace.Component"/>

I would like to register all IComponent objects with ComponentRepository. I suppose it can be done with some kind of publish/subscribe mechanism, but I would like to keep my classes clean (without implementing any of spring.net interfaces). What is the best way to achieve this?


Solution

  • I understood your question as this: After the instantiation of the child-context's Component, the Register-method of a ComponentRepository defined in another context should be called.

    As far as I know, there is no xml-declarative way to achieve what you want.

    I would suggest to either make your ComponentRepository IApplicationContextAware (which is what you explicitly don't want) or to create a new IApplicationContextAware Class which takes a dependency of ComponentRepository.

    That way you can call the ApplicationContext's GetObjectsOfType Method and retrieve all IComponent objects to pass to the ComponentRepository.