I'm new to MEF and have some trouble figuring it out. I want to create and ASP.NET MVC application that supports several user stores dynamically. I thought I could use MEF for this. I defined the following contract.
public interface IUserProvider
{
List<ConfigurationOption> SupportedConfigurationKeys { get; }
void Start(List<ConfigurationOption> configuration);
List<UserInfo> GetUsers(UserInfo userInfo);
UserInfo GetUser(string id);
UserInfo Save(UserInfo userInfo);
bool Delete(UserInfo userInfo);
List<UserProperty> SupportedUserProperties { get; }
}
I implemented this two times. Creating a catalog and composing like so
[ImportMany(typeof(IUserProvider))]
public IEnumerable<Lazy<IUserProvider, IDictionary<string, object>>> UserProviders { get; set; }
ApplicationCatalog userProviderCatalog = new ApplicationCatalog();
CompositionContainer container new CompositionContainer(userProviderCatalog);
container.ComposeParts(this);
I can create an instance like so:
UserProviders.Where(x => x.Metadata.ContainsKey("SystemName") && x.Metadata["SystemName"].ToString() == "ActiveDirectory").FirstOrDefault();
After creation I would configure the provider and use it. But I do not want to repeat these steps for every request. So the question arises.
How do you make the catalog container and all that available application wide? All I can find are controller examples where the references are kept by controllerfactories, but that is not relevant this time. Another use case would be content plugins for working with different file types. I would not want to compose them for every request too. I would want to do this at application_start and keep them around. I thought about a static class but how to combine that with container.compose ?