ReactiveUI,查看/视图模型注射DI一般 [英] ReactiveUI, View/ViewModel injection and DI in general

查看:166
本文介绍了ReactiveUI,查看/视图模型注射DI一般的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直想自己进入UI开发的新时代,并发现ReactiveUI。我喜欢它的声明性特性。



我想作一个完整的开关,所以我试图理解如何在ReactiveUI的这个新的世界做出的东西。我选择ReactiveUI,因为我已经看到了由一个非常聪明的家伙(保罗C.贝茨)维护。



我很新的这一点,我很可能会有关它的问题洪水计算器,因为我有一个巨大的动力,我认为这值得学习并掌握



让我们进入细节



我一直使用视图 - 第一。我是莲花框架的老将用户( http://cinch.codeplex.com/



它使用MEF到的ViewModels注入到每个视图。你只需要装点[视图模型(SampleView)]您的视图模型,并添加附加属性到View(ViewModelLocator.ViewModel =SampleView),并且每当视图被加载,对应的视图模型被实例化,注入其的DataContext与您选择的生命周期。



这机制,而它是有效的,具有一定的不便。最严重的:它采用了定位



正如马克·塞曼在他的书中建议,服务定位是应该避免反模式




  1. 所以我的第一个问题是:是ReactiveUI建在$ b为基础的定位$ b基础架构之上

  2. 查看-first或视图模型优先?什么是良好做法,去耦,SOLID,和类似的东西,这些是一个疯狂的,亲微软清洁守则的爱人像我这样的顾虑方面更好?这将让我睡得更好,并提供我的应用程序与所有这些* ibility善?


解决方案

保罗可能会附和官方的答案,但我要在我的$ 0.02为谁使用了框架的几个项目,但绝不是一个专家的人。



1)我是一个大标志塞曼风扇和我与他有关的ServiceLocator反模式的结论一致。虽然ReactiveUI不使用定位器啪,我不认为这是建立在基于定位基础设施上。但是也有一些像使用的线程调度和几个主要设置一些全球项目,但这些主要是获得应用程序的启动设定(像任何DI容器),你不跟他们直接在你的类处理大部分。唯一真正的位置是哪一方面的意见使用特定的接口( IViewFor )反对的ViewModels注册 ViewModelHost 控制。这比该属性的方法更好,因为它保持的ViewModels一无所知的意见。但是,这种情况在控制本身就是框架的一部分,所以我不觉得这是在服务定位反模式的滥用。我不觉得这是比任何登记在建立DI容器没有什么不同。



2)因为使用ReactiveUI只是我的经验,我的观点已经得到了超级-简单。基本上拍了一些基本的XAML获得的外观和布局正确的,在代码执行 IViewFor ,尽我所有的构造函数的结合,这是我现在更容易找到ReactiveUI比XAML做(尽管你仍然可以,如果你愿意的话)。然后一切逻辑明智的在做的ViewModels。我想,我通常做一个ViewModel优先的方法纯粹的事实,我需要把它(或至少它的接口)中定义的实施 IViewFor<> 它的视图。我喜欢的类型检查和东西(另一个原因,我喜欢在构造函数中没有绑定XAML)。但我不认为这是一个强有力的理由这样做的一种方式或其他,从我的经验。


Lately I've trying to get myself into the new age of UI development and discovered ReactiveUI. I love its declarative nature.

I wanted to make a complete switch, so I tried to understand how are things made in this new world of ReactiveUI. I choose ReactiveUI because I've seen that is maintained by a very clever guy (Paul C. Betts).

I'm very new to it and I will likely be flooding StackOverflow with questions about it because I has a huge power and I think it deserves to be learnt and mastered.

Let's get into the details:

I've always used View-First. I'm a veteran user of the Cinch Framework (http://cinch.codeplex.com/)

It uses MEF to inject the ViewModels to each View. You just have to decorate your ViewModel with [ViewModel("SampleView")] and add an Attached Property to your View (ViewModelLocator.ViewModel="SampleView"), and whenever the View is Loaded, the corresponding ViewModel is instantiated and injected as its DataContext with the life cycle you choose.

This mechanism, while it's valid, has some inconveniences. The worst of them: It uses a Locator.

As Mark Seemann suggest in his book, ServiceLocator is an anti-pattern that should be avoided.

  1. So my first question is: is ReactiveUI built on top of a Locator-based infrastructure?
  2. View-First or ViewModel-First? What's better in terms of good practices, decoupling, SOLID, and stuff like these that are concerns of a crazy, pro-Microsoft Clean Code lover like me? Which will make me sleep better and provide my application with all those *ibility goodness?

解决方案

Paul will probably chime in with the official answer, but I will put in my $0.02 as a person who has used the framework for a few projects but is by no means an expert.

1) I'm a big Mark Seemann fan and I do agree with his conclusion about the ServiceLocator anti-pattern. While ReactiveUI does use the Locator "Splat", I wouldn't consider it built on top of a Locator based infrastructure. There are a few Global items that are used like thread schedulers and a few major settings, but these mainly get set in application startup (like any DI container) and you don't deal with them directly in your classes for the most part. The only real location is the ViewModelHost control which uses a specific interface (IViewFor) on views to register against ViewModels. This is better than the attribute method since it keeps the ViewModels blissfully unaware of the Views. But this happens in the control itself and is part of the framework, so I don't feel it is an abuse of the ServiceLocator anti-pattern. I don't feel it is any different than registering anything else in setting up a DI container.

2) In just my experience since using ReactiveUI, my Views have gotten super-simple. Basically slap up some basic XAML to get the look and layout right, implement the IViewFor in the code behind, and do all my binding in the constructor, which I find easier now with ReactiveUI than doing in XAML (although you still can if you want to). Then everything logic-wise is done in the ViewModels. I think I usually do a ViewModel First approach solely for the fact that I need to have it (or at least its interface) defined to implement IViewFor<> for it on the View. I like the type-checking and stuff (another reason I like to bind in the constructor not in XAML). But I don't think there is a strong reason to do it one way or the other, from my experience.

这篇关于ReactiveUI,查看/视图模型注射DI一般的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆