简体   繁体   English

实体框架,LINQ和模式

[英]Entity Framework, LINQ and patterns

I've started using EF and LINQ in a project and I'm trying to decide on the best approach/pattern to use. 我已经开始在项目中使用EF和LINQ,并且正在尝试确定要使用的最佳方法/模式。 Until now I've been using a custom persistence framework that was based on DataSets and XML configuration. 到目前为止,我一直在使用基于DataSet和XML配置的自定义持久性框架。 Basically it was a VS Custom Tool that would read the XML configuration file and the DataSets and would generate Object Oriented classes with all the necessary properties/associations/methods. 基本上,这是一个VS Custom Tool,它可以读取XML配置文件和DataSet,并生成具有所有必要属性/关联/方法的面向对象的类。 This auto-generated classes then were used from the UI and I had the flexibility to expose only what the UI would need. 然后从UI中使用了自动生成的类,我可以灵活地仅公开UI所需的内容。

Now with EF and LINQ, I'm not comfortable with the idea that the UI can use directly the auto-generated classes and all the LINQ stuff. 现在有了EF和LINQ,我对UI可以直接使用自动生成的类和所有LINQ东西的想法感到不满意。 It seems that this approach would have a very tight integration between UI and the database. 看来这种方法将在UI和数据库之间实现非常紧密的集成。

So I'm looking for some pattern that would "hide" all the EF and LINQ goodies and basically limit what the UI can do. 因此,我正在寻找某种“隐藏”所有EF和LINQ好东西并基本上限制UI可以执行的模式。 Is there any standard way to do this? 有什么标准的方法可以做到这一点吗?

What you're looking for is an n-tier application. 您正在寻找的是n层应用程序。 It's not so much a pattern as an architecture. 它不只是架构的一种模式。 You break your app up into 2 or more pieces, typically 3 composed of UI, business and Data. 您将您的应用程序分为两部分或更多部分,通常是由UI,业务和数据组成的三部分。 You might implement this through other patterns such as the Facade or Repository patterns to keep a strong seperation of concerns. 您可以通过诸如Facade或Repository模式之类的其他模式来实现此目的,以保持关注点的强烈分离。

You might also use a Service Layer, which could be implemented by a facade or as a web service. 您还可以使用服务层,该服务层可以由外观或作为Web服务实现。

You would, ideally, pass data through objects called DTO's or Data Transfer Objects, and you might adapt those DTO's by using a view model in your UI (not to be confused with MVVM which another poster erroneously mentioned.) 理想情况下,您将通过称为DTO或数据传输对象的对象传递数据,并且可以通过在UI中使用视图模型来适应这些DTO(不要与其他海报错误提及的MVVM混淆)。

Beyond that, much of it depends on the type of app you're buiding. 除此之外,这很大程度上取决于您要构建的应用程序的类型。 Desktop app, server app, web app, etc.. 桌面应用,服务器应用,网络应用等。

The pattern you're looking for is, in general, Model-View-ViewModel, or MVVM. 通常,您要寻找的模式是Model-View-ViewModel或MVVM。

Here's a tutorial that seems to hit on the high points of the design pattern: http://csharperimage.jeremylikness.com/2010/04/model-view-viewmodel-mvvm-explained.html 这是一个似乎可以达到设计模式最高要求的教程: http : //csharperimage.jeremylikness.com/2010/04/model-view-viewmodel-mvvm-explained.html

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

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