简体   繁体   English

WPF和ASP.NET MVC的通用模型

[英]Common models for WPF and ASP.NET MVC

I am going to build two applications. 我将构建两个应用程序。 One of them in WPF and second one in ASP.MVC. 其中之一在WPF中,第二在ASP.MVC中。 I decided to create a webservice which will be a layer between database and my applications. 我决定创建一个Web服务,它将成为数据库和应用程序之间的一层。 I don't want to use entity models in my apps. 我不想在我的应用程序中使用实体模型。 I want to create lightweight models, send them to webservice and then translate them to the entity models. 我想创建轻量级模型,将其发送到Web服务,然后将其转换为实体模型。 My question is how to create a common models for WPF and MVC? 我的问题是如何为WPF和MVC创建通用模型? I don't know how to do that because I want to use data annotation attributes for MVC and also implement INotifyPropertyChanged interface for WPF. 我不知道该怎么做,因为我想为MVC使用数据注释属性,并为WPF实现INotifyPropertyChanged接口。 There were will be also others things I think. 我认为还会有其他事情。 Is there some good approach for doing it? 有一些好的方法吗? Or it is impossible? 还是不可能?

Well, first and foremost, there's nothing wrong with exposing entity classes outside of the DAL. 嗯,首先,将实体暴露在DAL之外没有任何问题。 They're just classes. 他们只是课程。 There's nothing special about them. 他们没有什么特别的。 What makes them do database stuff is their inclusion in a DbContext class, and you could actually move all the annotations and other config into the context using fluent config and have pristine classes without any sort of outside influence in them. 使它们完成数据库工作的原因是它们包含在DbContext类中,并且您实际上可以使用流畅的config将所有注释和其他配置移到上下文中,并且具有原始类,而没有任何外部影响。

Also, there's a tendency to add too much config to entity classes anyways. 另外,总有一种趋势是向实体类添加过多的配置。 They should only have things that are important at a database level . 他们只应具有在数据库级别上重要的内容 Things like [EmailAddress] which instruct how a property should be validated on post are inappropriate on an entity class. 诸如[EmailAddress]内容指示如何在发布时验证属性的方法在实体类上不合适。

That said, the place to put things like [EmailAddress] and other view-specific things is on view models. 也就是说,将诸如[EmailAddress]和其他特定于视图的内容放置在视图模型上。 Which should pretty much clear up the rest of your confusion. 哪个应该可以消除您其余的困惑。 Need to implement INotifyPropertyChanged ? 需要实现INotifyPropertyChanged吗? Do so with a view model. 使用视图模型执行此操作。 and map your entity data to/from it. 并映射您的实体数据。

Just don't do like I've seen some people do and create a DTO class that's basically an exact copy of your entity class solely to transfer the data from one place to another in your app. 只是不喜欢我见过的人所做的那样,所以创建了一个DTO类,该类基本上是您的实体类的精确副本,只是将数据从应用程序中的一个地方转移到另一个地方。 That's useless and only adds additional things to maintain and additional work your app needs to perform with no benefit. 那是没有用的,只会增加需要维护的其他内容以及您的应用需要执行的其他工作,而没有任何好处。 The entity class is a DTO. 实体类是DTO。 Return it from your service and then map to your view model. 从您的服务中将其返回,然后映射到您的视图模型。 Done. 做完了

Just to clarify the last bit, the classes that matter to your WPF/MVC apps should be their view models, and those will be unique to the app. 为了澄清最后一点,与WPF / MVC应用程序相关的类应该是它们的视图模型,而这些模型对于该应用程序是唯一的。 The logic for what is needed in WPF and how to map entity data to/from that belongs to the WPF app. WPF中需要什么以及如何将实体数据映射到WPF应用程序中的逻辑,或从WPF应用程序映射实体数据的逻辑。 It would be 100% inappropriate to have code creating class instances to be used directy in a WPF app to be created by some service that's supposed to be concerned with getting the data in the first place. 让代码创建类实例直接在WPF应用中直接由某些本应与获取数据有关的服务创建,这将是100%不适当的。 This would be a violation of the single-responsibility principle. 这将违反单一责任原则。 Let each thing do what it's designed to do. 让每件事做它打算做的事情。 Keep WPF code with WPF code, in other words. 换句话说,将WPF代码与WPF代码保持一致。 You could still have some library that handles mapping between the data layer and your WPF view models, but that should be a separate thing which would only be a dependency in your WPF app. 您仍然可以使用一些库来处理数据层和WPF视图模型之间的映射,但这应该是单独的事情,在WPF应用程序中只是一个依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM