tags:

views:

251

answers:

1

Hello, I have this application using CAL. All the modules derive from a special class, ModuleBase, which has an abstract method, say ApplySecurity implemented in each one of them.

OK I load the modules in the bootstrapper, and after i call bootstrapper.Run(), i want to access all the modules that were loaded and call this ApplySecurity method.

I tried this:

IModuleCatalog moduleCatalog = this.Container.Resolve<IModuleCatalog>();

moduleCatalog.Modules.ToList().ForEach(m => 
{
    (this.Container.Resolve(Type.GetType(m.ModuleType, false, false)) 
         as ModuleBase).ApplySecurity(); //^^^ this is making new instances!!
});

but this is making new instances of modules, and i want to reference the ones that were already initialized.

I hope i have been clear enough,

Teodor.

A: 

It looks like the problem is that when the Module is registered to the container it isn't registered as a specific instance, so when you request the resolve a new instance is created. I've had a look through the CAL source code and the registering happens in the middle of a convoluted set of method calls, so I doubt you'll be able to sensibly supply your own implementation.

This means you'll need to register the instance of your module somewhere else. The way to do this off the top of my head is to create some kind of module instance catalog and add a call to each of the module's initilize method to register with it.

public class ModuleInstanceCatalog : IModuleInstanceCatalog
{
     private Dictionary<Type, ModuleBase> _modules = new Dictionary<Type, ModuleBase>();

     public void RegisterModuleInstance(ModuleBase module)
     {
         _modules.Add(module.GetType(), module);
     }

     public ModuleBase GetModuleInstanceByType(Type type)
     {
         return _modules[type];
     }
}

In your Bootstrapper CreateShell():

this.Container.RegisterInstance<IModuleInstanceCatalog>(new ModuleInstanceCatalog());

In each module's Initilize():

IModuleInstanceCatalog catalog = this.Container.Resolve<IModuleInstanceCatalog>();
catalog.RegisterModuleInstance(this);

Then replace the code in your question with:

IModuleCatalog moduleCatalog = this.Container.Resolve<IModuleCatalog>();
IModuleInstanceCatalog instanceCatalog = this.Container.Resolve<IModuleInstanceCatalog>();

moduleCatalog.Modules.ToList().ForEach(m => 
{
    instanceCatalog.GetModuleInstanceByType(m.ModuleType).ApplySecurity();
});

I'm not overly happy with this as it feels like a bit of a fudge, but it should work for what you want.

Martin Harris
Thank you for your answer, this did the job. :)
Teodor