我们正在使用 WPF/Prism(复合应用程序库)。当我们即将完成与客户的合作时,我们的团队被置于新的管理之下,不久之后:
-
然后我们被指示放弃 Prism 框架以保持事情简单。这包括不使用任何类型的控制反转。
-
我们被指示在不使用 MVVM 或类似技术的情况下构建 WPF 应用程序;以及更多与传统 WinForm 应用程序类似的内容。这个想法是,如果开发人员在 Visual Studio 的设计器视图中看到一个控件,那么他(她)应该能够单击该控件并准确地看到它在做什么,而无需遍历视图模型(或类似的模型)。
-
我们现在的任务是使用一个主窗口构建 WPF 应用程序,使用 框架控件 来包含内容,并使用框架外部的功能区作为菜单项。我们被允许使用帧控制的原因:
a.我们将在框架中显示一个带有 页面(不是用户控件),然后在框架中加载该页面。
b.当要在 Frame 中显示新视图时,当前视图(Page)将被关闭/处置,新视图(Page)将在 Frame 中取代其位置。
c.当开发人员在设计视图中查看页面时,他(她)将能够单击任何控件并准确查看正在执行的操作。
鉴于上述 1 和 2 的限制,我们希望提出另一种构建应用程序的方法:
-
可以作为使用“框架方法”(上面第 3 项)的替代方案,但仍然提供相同的功能 。
-
不使用 MVVM(请参阅上面的 #1 和 #2)。
根据给定的方向,我们可以提出替代方案吗?我要求您的回复保持专业水平,并提前感谢您。
We were building out the next version of an in-house thick-client application using WPF/Prism (Composite Application Library). As we were nearly done with the client our team was put under new management and shortly thereafter:
-
We were then directed to drop the Prism framework to keep things simple. This includes not using any type of Inversion of Control.
-
We were directed to build out the WPF application without using MVVM or similar; and more along the lines of a traditional WinForm application. The idea is that if a developer sees a control in Visual Studio’s designer view, then (s)he should be able to click on the control and see exactly what it's doing without having to traverse through a view-model (or similar).
-
We have now been tasked with building out the WPF application using one primary Window, use a Frame Control to contain the content, and use a Ribbon outside of the frame for the menu items. Reason we were provided to use Frame Control:
a. We will show a view in the Frame with a Page (not a user control) and then load the page in the Frame.
b. When a new view is to be shown in the Frame, the current view (Page) will be closed/disposed and the new view (Page) will take its place in the Frame.
c. When a developer looks at the Page in design view, (s)he will be able to click on any control and see exactly what is being done.
Given the restrictions of 1 and 2 above, we’d like to present another method of building out the application that:
-
Can be presented as an alternative to using the “Frame Methodology” (item 3 above) but still provides the same type of functionality.
-
Does not use MVVM (see #1 and #2 above).
Provided the direction we’ve been given, any suggestions as to an alternative we can present? I’d request that the responses be kept on the professional level and thank you in advance.
发布评论
评论(4)
我个人会尝试主张使用 Martin Fowler 的演示模型。 (顺便说一句,这是个笑话...)
基本上,您受到的限制是“使用 WPF,但不要使用任何使 WPF 可用的功能”。听起来您的要求确实如此,因此您最好合理地解释 MVVM 等模式的优点。
听起来奇怪的要求实际上可以归结为:
如果这是主要问题,以及您避免使用 MVVM 和其他类似的模式,我会认真花时间教育管理层。按名称查看命令,而不是按名称查看事件(这是您在设计器中看到的)实际上并不困难。
然而,在大规模应用程序中,关注点分离是关键。即使正确设计的 Windows 窗体应用程序也需要完全分离关注点 - 但对于基于事件的编程,这变得更加困难,尤其是对于设计者而言。如果您尝试使用事件方法开发大规模、干净的应用程序,您将拥有事件处理程序,但这些事件处理程序最终都需要将其工作委托给单独的组件。
从可理解性和维护的角度来看,这实际上是在 MVVM 的基础上增加了额外的工作量。使用 MVVM,您只需查看 ViewModel,这是非常容易发现的。
顺便说一句 - 使用页面而不是用户控件的“理由”没有任何意义。您可以执行与 UserControls 描述的完全相同的操作...使用框架和页面的唯一原因是如果您想利用导航,在这种情况下,您不能直接处理旧页面(或它们不断地再生)。此外,导航工具可能不会与功能区一起使用 - 这两个概念模型完全不同。
I'd personally try to argue to use Martin Fowler's Presentation Model. (That's a joke, btw...)
Basically, you're being given a restriction that says "Use WPF, but don't use any of the features that make WPF usable." It really sounds like your requirements are such that you would be much better off explaining, reasonably, the advantages of patterns like MVVM.
It sounds like the weird requirements are really boiling down to this:
If that's the main issue, and the reason you're avoiding MVVM and other similar patterns, I would seriously take the time to educate the management. Looking at a Command, by name, instead of an event, by name (which is what you see in the designer) is really no more difficult.
However, in a large scale application, the separation of concerns is key. Even a properly designed Windows Forms application requires a clean separation of concerns - but with event based programming, this becomes much more difficult, especially from the designer. If you try to develop a large scale, clean, application using an event approach, you'll have event handlers, but those event handlers will all eventually need to delegate their work to a separate component.
This is actually adding an extra level of effort, from an understandability and maintenance point of view, on top of what you get with MVVM. With MVVM, you only look to the ViewModel, which is very discoverable.
BTW - The "rationale" for using a Page instead of a UserControl doesn't make any sense. You can do exactly the same thing you're describing with UserControls... The only reason to use a Frame and Page is if you want to take advantage of navigation, in which case, you can't dispose the old pages directly (or they get regenerated constantly). Also, the navigation tools probably wouldn't be used with a ribbon - the two conceptual models are quite different.
有一些对 MVVM 的批评可能适用于你的项目;然而,对编程方法的不合理规定总是会导致灾难。
我们拥有框架并花时间构建层和分离的原因之一是为了避免当您可以“只需单击 Visual Studio 中的按钮即可查看正在执行的代码”时总是会导致编码混乱。
如果没有类似于 MVVM 的东西,可能就没有办法实现您被要求做的事情,因为任何具有架构的东西都可能被标记为过于相似。
然而,我多年来一直使用一个系统,该系统提供简单的对象间管道,当前称为 Emesary,您可能想阅读我的 C# .NET Emesary 演练。
但基本上它允许我的按钮这样实现:
这可能是您问题的答案。它没有被大肆宣传,很小,而且很简单,但效果很好。
我通过使用窗口、功能区栏的用户控件(用户控件包含列表视图)以及框架部分的另一个用户控件,实现了第二个问题的解决方案。显然,第二个用户控件是使用其他用户控件使用非常简单的视图类构建的。所有视图和控件均使用 Emesary 连接。
There are criticisms of MVVM which may be applicable to your project; however having unreasonable dictates of programming methodology is always a recipe for disaster.
One of the reasons that we have frameworks and spending time building layers and separation is to avoid the coding mess that always results when you can "simply click on the button in visual studio to see the code that is being executed".
There may not be a way of achieving what you've been asked to do without something similar to MVVM, because anything that has an architecture may well be labelled as being too similar.
However I have been using a system for many years that provides simple inter-object plumbing currently called Emesary you may want to read my C# .NET Emesary walkthrough.
But basically it allows my buttons to be implemented thus:
This may be an answer to your problem. It's under hyped, small and so simple but it just works well.
I've achieved a solution to the second question by using a Window, a user control for the ribbon bar (the user control contains the listview), and another user control for the Frame part. This second user control obvious is built using other user controls using a very simple view class. All views and controls are connected using Emesary.
作为一个学校项目,我必须开发一个 WPF 客户端,允许多人同时使用它。我使用了 Pages。我的结论是:为自己节省大量精力,而使用 UserControls。
有时,页面导航器(您将用来滚动浏览)往往会出现错误并给您带来很多问题。也许这是我糟糕的编码,但谁知道呢?
尽管我必须说,称为“页面”的控件有些误导......我“尤里卡!”当我找到他们时,我就咒骂他们。
我完全同意#2(MS 大佬们注意了!)。如果您可以双击一个控件,它会直接带您到它的命令(如果缺少它的命令,则为事件),那就太酷了。但在此之前,请确保将视图和视图模型组织在单独的文件夹中。
拥有双屏幕(或非常宽的屏幕)将允许您在项目上打开两个 VS 实例,一个专注于视图,另一个专注于 ViewModel(我个人的选择是在视图上使用 Expression Blend)。
虽然不是一个非常大的应用程序,但我设法在几天内将我的项目转换为正确的 MVVM(即每个 UI 元素的 ViewModel、RelayCommands 和 Mediator),所以一旦你理解了它,就不会太难了实施起来很复杂。另外,还有一些工具(例如 Josh Smith 的 RelayCommand 和 Marlon Grech 的 Mediator - 完全免费)可以使 MVVM 的难度降低一半,功能强大一倍。
在没有 MVVM 的情况下使用 WPF 就像尝试没有叉子吃米饭一样。如果您不打算利用 WPF 提供的功能,那么最好使用 WinForms。我的2分钱。
As a school project I had to develop a WPF client which allowed for multiple people to make use of it at the same time. And I used Pages. My verdict: Save yourself a huge amount of effort, and use UserControls instead.
Sometimes the Page Navigator (which you'll use to scroll through) tends to bug out and cause you a lot of problems. Maybe it was my crappy coding, but who knows?
Though I must say, the control being called "Pages" is somewhat misleading... I went "Eureka!" when I found them, and swore at them thereafter.
I totally agree with #2 (MS bigs take note!). It would be cool if you could double-click a Control and it would take you straight to its command (or event if its command is lacking). However till then, make sure that you organize your Views and ViewModels in separate folders.
Having a dual screen (or a very wide one) will allow you to have two instances of VS open on the project, one focused around the View and the other around the ViewModel (my personal choice was having Expression Blend on the View).
Although not a very big application, I managed to convert my project to proper MVVM (ie. ViewModel for every UI element, RelayCommands and Mediator) in a matter of days, so once you understand it it's not too complicated to implement. Plus, there are tools out there (such as Josh Smith's RelayCommand and Marlon Grech's Mediator - totally free, by the way) which make MVVM half as difficult, and twice as powerful.
Using WPF without MVVM is like trying to eat rice without a fork. You'd be better off using WinForms if you're not going to take advantage of what WPF has to offer. My 2 cents.
我希望我能说你的管理是完全错误的..但我不能这么说,因为这不是最准确的事实。我猜你所描述的变化的主要原因要么是因为新经理对 MVVM 作为 UI 开发的新救世主的概念感到不舒服,要么/和另一个原因可能是受过教育的成熟开发人员与廉价开发人员的成本,可以被指示尽快完成工作,这一概念被广泛称为精益开发。
因此,将我迄今为止写的所有内容放在“不是您要求的”下,这就是我的建议:
您仍然可以使用面向对象的纯方法,这意味着您可以拥有一个已经具有显示 UI 信息的方法的模型对象。所以每个对象都将是一个窗口派生对象,这样你就会失去 SOC,但你仍然会是 OOP/OOD。
但是哈哈,下一阶段将使您将模型与视图分离,以便不在许多依赖相同数据的派生窗口中重复相同的代码...因此您的管理层将认可 MVC/MVP 作为良好的解决方案...并且如果他们想要 WPF,那么从它到 MVVM 的距离有点短。
结论:你必须告诉你的经理为什么最好选择 MVVM,除非项目非常短。
I wish I could say your management is totally wrong.. but I cannot say that as it will not be the most accurate truth. I guess that the main reason to the changes you described is either because the new manager is not comfortable with the concept of MVVM being the new messiah of UI development or/and another reason could be the cost of educated sophisticated developers vs. cheap developers which can be instructed to get the things done as fast as possible, a concept which is widely known as lean development.
So, putting all I wrote so far under "not what you asked for", here is what I suggest:
you can still use object oriented pure approach, meaning you can have a model object which already have method to show UI information. so every object will be a window derived object, that way you will loose on SOC but you still going to be OOP/OOD.
But LOL, The next phase will bring you to seperation of model from view in order to not repeat the same code in many derrived windows which relay on the same data... so your management will endorse MVC/MVP as good solution .. and the distance from it to MVVM is kinda of short if they want WPF.
Conclusion: you will have to teach your manager why it is better to go for MVVM, unless the project is very short.