Search code examples
silverlightioc-container

Using IOC container in silverlight to dynamically load assembly. Is this possible?


I have two silverlight assemblies. In assembly 1 I have implemented an interface ISnProvider. In assembly 2 I have a concrete class which implements the ISnProvider. I plan on having more assemblies which will contain different implementations of ISnProvider. Assembly 1 has no reference to assembly 2.

When my silverlight app starts up, I want to resolve the ISnProvider interface using an IOC container. Ideally I won't have to recompile my app to use different providers. I don't want to hardcode 'assembly2.dll' in my assembly 1.

Which IOC container will allow me to dynamically load these assemblies using silverlight?

Thank you!

santiago


Solution

  • You might want to take a look at the Managed Extensibility Framework (MEF) from Microsoft. That will certainly support your scenario (example here) although it does unfortunately mean you need to use attributes (e.g. [Import] / [Export]) at various places in your code.

    Autofac is available in a Silverlight version and I would certainly recommend it for general IOC usage with Silverlight but I've never tried to get it to dynamically load an assembly without a direct reference and I'm not sure if it will support that in Silverlight (I'd love to hear if anyone has accomplished this with Autofac)

    You could also use a semi-manual approach - here's an example of loading an assembly dynamically in Silverlight without an IOC container, which may or may not be useful for you.