0

I am writing a program that uses IoC(Windsor v3.0) at startup to load all assemblies in a directory, if implementing an interface/service, into a repository for the core of the application. I am, however, a newcomer to Windsor. My app polls a DB table and when it finds a row that needs to be processed, it checks the name of the service to process the record and requests it from the repository. I can load all the modules into the dictionary and then into the repository via configuration as in this post. Good and well, but I need it to be more dynamic.

How I envision it (pseudo-code):

List<string> enabledServices = GetServicesFromDb();
IDictionary<string, IModule> dict = new IDictionary<string, IModule>();

//Load the assemblies (This works currently!)
_container.Register(AllTypes
                   .FromAssemblyInDirectory(new AssemblyFilter("Modules"))
                   .BasedOn<IModule>());

// Build dictionary
foreach(string service in enabledServices)
{
     foreach(?? asmble in _container.assemblies)
     {
         if(asmble.Id == service)
             dict.Add(service, asmble);
     }
}

// Register the repository from constructed dictionary
_container.Register(
    Component
    .For<IModuleRepository>()
    .ImplementedBy<IntegrationRepository>()
    .Parameters(new { modules = dict})
);

The repository:

public class IntegrationRepository : IModuleRepository
{
    private readonly IDictionary<string, IModule> _modules;

    public IntegrationRepository(IDictionary<string, IModule> modules)
    {
        _modules = modules;
    }

    public IModule GetModule(string moduleName)
    {
        return _modules.ContainsKey(moduleName) ? _modules[moduleName] : null;
    }
}

IModule looks like this:

public interface IModule : IDisposable
{
    string Id { get; }
    string Description { get; }
    bool Enabled { get; set; }
    bool Validate();
    string EmailSubject { get; }
}

All modules:

  1. Implement "IModule" interface
  2. Reside in the "Modules" subfolder
  3. Share a common namespace

I don't have enough experience with Windsor to know how to iterate through the container or if its possible, and _container.ResolveAll(); doesn't seem to work... at least the way I have it in my mind.

My thoughts come from this example which alludes to passing the object in if the object is already created. And this which is similar. I also saw some interesting things on the DictionaryAdapterFactory() but not confident enough to know how to use it

Is something like this possible? Any ideas?

Community
  • 1
  • 1
sighlent
  • 65
  • 5

2 Answers2

0

instead of all this you can just store your container globally and you can resolve by full name your modules everywhere

_container.Resolve<IModule>(serviceFullName)
maxlego
  • 4,864
  • 3
  • 31
  • 38
0

you can register all available services to your container and then create a provider that returns only the services that are enabled in the database. Your components then should of course only access these services through the provider.

Dirk Trilsbeek
  • 5,873
  • 2
  • 25
  • 23
  • That is the goal of having a repository. :) My question centers on how to fluently get the assemblies into the repository in the first place. It is looking as though I might have to wrap the container instead of a dictionary. – sighlent Mar 20 '12 at 11:00