栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在WPF / MVVM应用程序中处理依赖项注入

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在WPF / MVVM应用程序中处理依赖项注入

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

所以基本上它是这样的:

创建视图模型,并将IStorage接口作为构造函数参数:

class UserControlViewModel{    public UserControlViewModel(IStorage storage)    {    }}

使用视图属性的get属性创建一个ViewModelLocator,该模型从Ninject加载视图模型:

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>

将UserControl的DataContext绑定到ViewModelLocator中的相应属性。

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

创建一个继承NinjectModule的类,该类将设置必要的绑定(IStorage和viewmodel):

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类进行某种形式的手动服务查找非常困难,因为它们必须具有无参数的构造函数,而且您无论如何也无法控制实例化,因此无法注入VM。至少通过这种方式,您可以隔离地测试VM,这是所有业务逻辑所在的位置。

如果有人有更好的方法,请分享。

编辑:Lucky
Likey通过让Ninject实例化UI类来提供摆脱静态服务定位器的答案。答案的细节可以在这里看到



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/433076.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号