在ASP.NET MVC和WPF MVVM中重用相同的模型 [英] Reuse the same models in ASP.NET MVC and WPF MVVM

查看:87
本文介绍了在ASP.NET MVC和WPF MVVM中重用相同的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF MVVM开发应用程序,但是我还需要在同一解决方案中创建一个MVC项目.

我想知道是否有任何方法可以重用两个类中的相同模型,并且是达到MVVM规范的最佳方法

例如,我需要在我的属性集中添加一个"PropertyChanged",这在MVC项目中是不必要的.

解决方案

我同意@@ Douglas Gandini.您应该在一个单独的程序集中定义您的业务对象,您可以从ASP.NET和WPF应用程序中引用这些程序集.这些类不应实现任何特定于客户端的接口,例如INotifyPropertyChanged,而应是仅包含业务逻辑的纯POCO类.

然后在WPF应用程序中,您可以创建一个实现INotifyPropertyChanged接口的视图模型类,并包装有意义的业务对象属性,以暴露给该视图并从该视图绑定.

然后,视图模型具有对该模型的引用,即在单独的程序集中定义的业务对象,该组件可以在多个不同的客户端应用程序之间共享,并且该视图绑定到该视图模型.通常,这是在WPF应用程序中(或应该)实现MVVM设计模式的方式.视图模型类包含您的应用程序逻辑,例如,如何更改数据绑定属性值时如何通知视图,并且该模型包含的业务逻辑在所有平台和应用程序中都是相同的./p>

当然,这意味着您最终将获得大量的班级,但这不一定是一件坏事,因为每个班级都有自己的责任.视图模型的职责是充当特定于应用程序的XAML视图的模型,而模型类的职责是实现业务逻辑.

I'm developing an application in WPF MVVM, but I'll also need to create an MVC project in the same solution.

I would like to know if there is any way to reuse the same Models in both classes, and the best way to do it, since to meet the MVVM specifications

For example, I need to add a "PropertyChanged" to the set of my properties, something unnecessary in MVC project.

解决方案

I agree with @@Douglas Gandini. You should define your business objects in a separate assembly that you can reference from both your ASP.NET and WPF applications. These classes should not implement any client-specific interfaces such as INotifyPropertyChanged but be pure POCO classes that contains business logic only.

In your WPF application, you could then create a view model class that implements the INotifyPropertyChanged interface and wraps any properties of the business object that it makes sense to expose to and bind to from the view.

The view model then has a reference to the model, i.e. your business object that is defined in a separate assembly that may be shared between several different client applications, and the view binds to the view model. This is typically how the MVVM design pattern is (or should be) implemented in a WPF application. The view model class contains your application logic, for example how to notify the view when a data bound property value is changed, and the model contains the business logic that is the same across all platforms and applications.

Of course this means that you will end up with a larger number of classes in total but this is not necessarily a bad thing as each class has its own responsibility. The responsibility of a view model is to act as a model for the application specific XAML view whereas the responsibility of the model class is to implement the business logic.

这篇关于在ASP.NET MVC和WPF MVVM中重用相同的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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