基本上,我该如何指定要选择哪个实现?
FooService.cs:
public interface IFooService
{
Int32 GetFoo();
}
[Export(typeof(IFooService))]
public sealed class Foo100 : IFooService
{
public Int32 GetFoo()
{
return 100;
}
}
[Export(typeof(IFooService))]
public sealed class Foo200 : IFooService
{
public Int32 GetFoo()
{
return 200;
}
}
ClientViewModel.cs:
[Export()]
public class ClientViewModel : NotificationObject
{
[Import()]
private IFooService FooSvc { get; set; }
public Int32 FooNumber
{
get { return FooSvc.GetFoo(); }
}
}
Boostrapper.cs:
public sealed class ClientBootstrapper : MefBootstrapper
{
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
//Add the executing assembly to the catalog.
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
}
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<ClientShell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window)Shell;
Application.Current.MainWindow.Show();
}
}
ClientShell.xaml.cs:
[Export()]
public partial class ClientShell : Window
{
[Import()]
public ClientViewModel ViewModel
{
get
{
return DataContext as ClientViewModel;
}
private set
{
DataContext = value;
}
}
public ClientShell()
{
InitializeComponent();
}
}
我不确定该如何设置我的应用程序以注入正确的内容(在这种情况下,我希望注入Foo100)。我知道可以让它们导出自己并指定一个Foo100而不是IFooService,但这是否是正确的方法?