I’ve started working with MEF. In my application I’ve a factory for some Models. My factory has 2 create methods; one takes a name as parameter, the other a type.
[Export(typeof(IFactory))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class MyFactory : IFactory
{
public IModel Create(String name) {...}
public IModel Create(Type type) {...}
}
public class Foo
{
[Import(typeof(IFactory))]
public IFactory Factory { get; set; }
public Foo()
{
IModel modelByName = Factory.Create("name");
IModel modelByType = Factory.Create(typeof(Foo));
}
}
At the moment I’ve to import a factory and then call create on the factory object to get a model. Now I’m wondering if there is a way to import a model directly, something like:
[Import(typeof(IModel), Name:"Name")]
public IModel Model { get; set; }
edit ---------------------------------------------------
The goal would be to replace something like this:
public class Foo
{
[Import(typeof(IFactory))]
public IFactory Factory { get; set; }
public IModel Model { get; set; }
public IModel Model1 { get; set; }
public Foo()
{
Model = Factory.Create("Foo");
Model1 = Factory.Create(typeof(Foo1));
}
}
Whit something like this:
public class Foo
{
//Should internal import a IFactory(singeleton) object
//and call the Create(name:String) method on it
[Import(typeof(IModel), Name:"Foo")]
public IModel Model { get; set; }
//Should internal import a IFactory(singeleton) object
//and call the Create(type:Type) method on it
[Import(typeof(IModel), Type: typeof(Foo1))]
public IModel Model1 { get; set; }
}