Export Import and Bootstrapper.

Oct 5, 2009 at 6:06 AM


I wonder why we should declare import and export via attributes instead of moving all injection logic to bootstrapper.

Currently with unity 2.0 fluent configuration I am able to remove all injection attributes from my code.


Alexey Zakharov.

Oct 5, 2009 at 6:24 AM

Hi Lexer

Thanks for the feedback.

MEF's attributed programming model is designed for open systems where the host has no fore-knowledge of the available set of components. In MEF, exports and imports are discovered on the fly, there is no central authority (such as centralized configuration / policy) which pre-determines the set. This makes MEF ideal for third-party scenarios where components are added after the application is deployed simply by adding new binaries and without touching the application or modifying configuration files.




Oct 5, 2009 at 6:42 AM

But won't it cause "Magic String Hell"?

Oct 5, 2009 at 7:16 AM

How do you mean? Are you talking about if one is specifying strings in attributes?

Oct 5, 2009 at 7:29 AM

Yes I'm talking about string in export and import.

Or third-party have an access to public class with all that constants?

public class ModuleNames

  public const string Module1  = "Module1"


Oct 5, 2009 at 9:10 AM
public class Module1 : IModule {

In general you should use types rather than explicit strings for contracts. For example for a module you would do this...

public class Module1 : IModule {

Now if you needed to you could provide the module name through metadata. That could be a string but it is not required. You could pass the type if you chose to by having a Metadata property.

public class Module1 : IModule {

the custom export attribute would look like this

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] 
public class ExportModuleAttribute : ExportAttribute {
  public ExportModuleAttribute(Type moduleType) : base(typeof(IModule))
    this.ModuleType = moduleType;

  public Type ModuleType {get; private set;}

HTH Glenn
Oct 6, 2009 at 6:20 AM

you can build custom catalog (see this sample)