解决统一DI / IOC中的特定依赖关系

时间:2014-01-17 11:18:18

标签: wpf c#-4.0 unity-container ioc-container

在使用Unity与WPF探索DI / IOC时,我遇到了一个问题并需要您的反馈。请考虑以下情况...

=============================================== =================

public interface IDataServices
{
   string GetData();
}

=============================================== =================

public class CopyTextDataServices : IDataServices
{
   public string GetData()
   {
      return "copy text from CopyTextDataServices";
   }
}

=============================================== =================

public class TextDataServices : IDataServices
{
   public string GetData()
   {
      return "I am injected by setter property injection";
   }
}

=============================================== =================

public interface ITextViewModel
{
   string LabelContnet { get; set; }
}

=============================================== =================

public class TextViewModel : ITextViewModel
{
        public TextViewModel()
        {
            LabelContnet = "This is from view model";
        }

        public string LabelContnet { get; set; }
}

=============================================== =================

public partial class MainWindow : Window
{
    public MainWindow(ITextViewModel textViewModel)
    {
        InitializeComponent();
        Loaded += MainWindow_Loaded;
        DataContext = textViewModel;
    }

    [Dependency]
    public IDataServices Services { get; set; }

    containing the event data.</param>
    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        LabelLeft.Content = Services.GetData();
    }
}

=============================================== =================

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        IUnityContainer container = new UnityContainer();

        container.RegisterType<IDataServices, TextDataServices>();
        container.RegisterType<IDataServices, CopyTextDataServices>();
        container.RegisterType<ITextViewModel, TextViewModel>();

        var window = container.Resolve<MainWindow>();
        window.Show();
    }
}

=============================================== =================

   <Window x:Class="TestAppWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" FontSize="20">
    <StackPanel>
        <Label Content="{Binding Path=LabelContnet,FallbackValue=Left}" HorizontalAlignment="Left" Name="LabelLeft" />
        <Label Content="{Binding Path=LabelContnet,FallbackValue=Right}" HorizontalAlignment="Left" Name="LabelRight" />
    </StackPanel>
   </Window>

=============================================== ====================

现在标签中出现的结果是

从CopyTextDataServices复制文本 这是来自视图模型

但我想知道我是否想从TextDataServices获取数据,我该怎么做?

1 个答案:

答案 0 :(得分:0)

问题出在这一行:

container.RegisterType<IDataServices, TextDataServices>();

// This overwrites the previous mapping.
// All dependencies to IDataServices will use CopyTextDataServices.
container.RegisterType<IDataServices, CopyTextDataServices>(); 

如果你想同时拥有两个IDataServices,你需要将一个或两个注册为命名实例。

container.RegisterType<IDataServices, TextDataServices>("TextDataServicesName");
container.RegisterType<IDataServices, CopyTextDataServices>("CopyTextDataServicesName"); 

在你的控制中:

[Dependency("TextDataServicesName")]
public IDataServices Services { get; set; }