【问题标题】:resolve specific dependency in unity DI/IOC解决统一 DI/IOC 中的特定依赖关系
【发布时间】:2014-02-06 17:01:37
【问题描述】:

在使用 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 中获取数据,我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    问题出在这一行:

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

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

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

    在您的控制之下:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      相关资源
      最近更新 更多