c# - 如何处理 WPF/MVVM 应用程序中的依赖注入(inject)

标签 c# wpf mvvm dependency-injection dependencies

我正在启动一个新的桌面应用程序,我想使用 MVVM 和 WPF 构建它。

我也打算使用 TDD。

问题是我不知道我应该如何使用 IoC 容器将我的依赖项注入(inject)到我的生产代码中。

假设我有以下类和接口(interface):

public interface IStorage
{
    bool SaveFile(string content);
}

public class Storage : IStorage
{
    public bool SaveFile(string content){
        // Saves the file using StreamWriter
    }
}

然后我有另一个类将 IStorage 作为依赖项,还假设这个类是 ViewModel 或业务类...

public class SomeViewModel
{
    private IStorage _storage;

    public SomeViewModel(IStorage storage){
        _storage = storage;
    }
}

有了它,我可以轻松编写单元测试以确保它们正常工作,使用模拟等。

问题是在实际应用中使用它的时候。我知道我必须有一个 IoC 容器来链接 IStorage 接口(interface)的默认实现,但我该怎么做呢?

例如,如果我有下面的xaml会怎样:

<Window 
    ... xmlns definitions ...
>
   <Window.DataContext>
        <local:SomeViewModel />
   </Window.DataContext>
</Window>

在这种情况下,我如何才能正确地“告诉”WPF 注入(inject)依赖项?

另外,假设我需要一个来 self 的 C# 代码的 SomeViewModel 实例,我应该怎么做?

我觉得我完全迷失在这件事上,我将不胜感激任何关于如何最好地处理它的例子或指导。

我熟悉 StructureMap,但我不是专家。另外,如果有更好/更简单/开箱即用的框架,请告诉我。

最佳答案

我一直在使用 Ninject,发现与它一起工作很愉快。一切都在代码中设置,语法相当简单,并且有很好的文档(以及关于 SO 的大量答案)。

所以基本上它是这样的:

创建 View 模型,并将IStorage接口(interface)作为构造函数参数:

class UserControlViewModel
{
    public UserControlViewModel(IStorage storage)
    {

    }
}

为 View 模型创建一个带有 get 属性的 ViewModelLocator,它从 Ninject 加载 View 模型:

class ViewModelLocator
{
    public UserControlViewModel UserControlViewModel
    {
        get { return IocKernel.Get<UserControlViewModel>();} // Loading UserControlViewModel will automatically load the binding for IStorage
    }
}

在 App.xaml 中使 ViewModelLocator 成为应用程序范围的资源:

<Application ...>
    <Application.Resources>
        <local:ViewModelLocator x:Key="ViewModelLocator"/>
    </Application.Resources>
</Application>

UserControlDataContext 绑定(bind)到 ViewModelLocator 中的相应属性。

<UserControl ...
             DataContext="{Binding UserControlViewModel, Source={StaticResource ViewModelLocator}}">
    <Grid>
    </Grid>
</UserControl>

创建一个继承 NinjectModule 的类,它将设置必要的绑定(bind)(IStorage 和 View 模型):

class IocConfiguration : NinjectModule
{
    public override void Load()
    {
        Bind<IStorage>().To<Storage>().InSingletonScope(); // Reuse same storage every time

        Bind<UserControlViewModel>().ToSelf().InTransientScope(); // Create new instance every time
    }
}

在应用程序启动时使用必要的 Ninject 模块(现在是上面的模块)初始化 IoC 内核:

public partial class App : Application
{       
    protected override void OnStartup(StartupEventArgs e)
    {
        IocKernel.Initialize(new IocConfiguration());

        base.OnStartup(e);
    }
}

我使用静态IocKernel 类来保存应用程序范围内的 IoC 内核实例,因此我可以在需要时轻松访问它:

public static class IocKernel
{
    private static StandardKernel _kernel;

    public static T Get<T>()
    {
        return _kernel.Get<T>();
    }

    public static void Initialize(params INinjectModule[] modules)
    {
        if (_kernel == null)
        {
            _kernel = new StandardKernel(modules);
        }
    }
}

此解决方案确实使用了静态 ServiceLocator(IocKernel),这通常被视为一种反模式,因为它隐藏了类的依赖项。然而,很难避免对 UI 类进行某种手动服务查找,因为它们必须具有无参数的构造函数,而且无论如何您都无法控制实例化,因此您无法注入(inject) VM。至少这种方式允许您隔离测试 VM,这是所有业务逻辑所在的地方。

如果大家有更好的方法,欢迎分享。

编辑: Lucky Likey 通过让 Ninject 实例化 UI 类,提供了摆脱静态服务定位器的答案。详细回答可见here

关于c# - 如何处理 WPF/MVVM 应用程序中的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25366291/

相关文章:

c# - 在 .NET v4.6.1 上的 ASP.NET MVC 4 中,我有什么选项可以替代 GetAwaiter.GetResult()?

c# - StringSplitOptions.RemoveEmptyEntries 不像宣传的那样工作

wpf - 文本框MVVM内的按键

c# - 根据父宽度绑定(bind) UniformGrid 列

wpf - MVVM - 如何将 View 模型绑定(bind)到 View

c# - 如何确定包含在 Asp.net 页面中的重复 Javascript 函数

c# - 当从不同的窗口调用 repository.save 时,mvvm 更新 ListView

visual-studio-2010 - 有关ItemsControl的XAML设计数据的问题

javascript - Knockout.js:将 ViewModel 逻辑与模型分离

c# - 如何在测试设置方法中获取测试类别