简体   繁体   English

实体框架和 3 层架构

[英]Entity Framework and 3 layer architecture

I have a three layer architecture program.我有一个三层架构程序。 The questions are:问题是:
1. Data access is the layer of EF? 1.数据访问是EF的层吗?
2. If i want to use an entity generated by EF from Presentation Layer, then i reference the Data Access, but this violates the principles of 3 layered architecture. 2.如果我想使用EF从表示层生成的实体,那么我引用数据访问,但这违反了3层架构的原则。

Microsoft Spain released a pretty good documentation, guide and sample application for N-layered applications on codeplex, you can look it up here:微软西班牙在 codeplex 上发布了一个非常好的文档、指南和示例应用程序,用于 N 层应用程序,你可以在这里查看:

http://microsoftnlayerapp.codeplex.com/ http://microsoftnlayerapp.codeplex.com/

You will find many directions and helpful implementation patterns there.你会在那里找到许多方向和有用的实现模式。

hth. hth。

Yes EF would be your Data Access Layer.是的,EF 将是您的数据访问层。 With EF you can use T4 templates with POCO support, you can then extract these POCO into a seperate dll and this will be reference from all of your layers.使用 EF,您可以使用支持 POCO 的 T4 模板,然后您可以将这些 POCO 提取到单独的 dll 中,这将是您所有层的参考。

What type of application are you building?您正在构建什么类型的应用程序? If you are building an ASP.NET MVC 3 application, you can have your View be the presentation layer, your Model is your data access (which can use EF) and the controller and / or Action Filters can contain your business logic and in this scenario you will be using your EF Model in the presentation layer but still satisfy the separation of concerns principle.如果您正在构建 ASP.NET MVC 3 应用程序,您可以让您的 View 成为表示层,您的 Model 是您的数据访问(可以使用 EF),并且 Z594C103F2C6E04C3D18AB059F031E0 可以包含您的业务逻辑和此场景中的业务逻辑和操作/过滤器将在表示层使用您的 EF Model 但仍满足关注点分离原则。

EF does two things: - EF 做了两件事:-

1) Generates an domain model for you (optional, but commonly used) 2) Gives you the ability to query / modify your database via that domain model. 1) 为您生成域 model(可选,但常用) 2) 使您能够通过该域 model 查询/修改数据库。

This can give the appearance of blurring the lines between domain model and data access but the two are indeed separate.这会使域 model 和数据访问之间的界限看起来模糊,但两者确实是分开的。

As long as you're not doing stuff like creating object contexts and writing queries directly in your presentation tierthen IMHO you are not breaking abstraction - the only thing you are "breakin"g is the fact that you will need to reference System.Data.Objects (or whatever the EF dll is) in your presentation project(s) (which is just a physical artifact) unless you go down the route suggested by Jethro to generate your domain model into a separate project.只要您不做诸如创建 object 上下文和直接在您的表示层中编写查询之类的事情,那么恕我直言,您并没有破坏抽象——您“破坏”的唯一一件事是您需要引用 System.Data。您的演示项目(这只是一个物理工件)中的对象(或任何 EF dll 是)除非您 go 沿着 Jethro 建议的路线生成您的域 Z20F35E630DAF44DBFA4C3F68F68F5C3 到一个单独的项目中。

For the three tier architecture.对于三层架构。 I would consider doing Abstraction using Domain Model and Data model pattern rather then doing direct EF from Presentation Layer.我会考虑使用域 Model 和数据 model 模式进行抽象,而不是从表示层直接执行 EF。

So the idea is that you have your Data Model which has EF POCO classes with Repositories which knows how to access these Classes for various CRUDs.所以这个想法是你有你的数据 Model 它有 EF POCO 类和存储库,它知道如何为各种 CRUD 访问这些类。

Your Domain Model would have models related to your Client (so you can put various ViewModels or Validation related code), It can be a WPF or MVC web app.您的域 Model 将具有与您的客户端相关的模型(因此您可以放置各种 ViewModel 或验证相关代码),它可以是 WPF 或 MVC Z2567A5EC9705EB7AC2C984033E0618D 应用程序。 Now between these two there is a business which talks to both Domain and Data models.现在在这两者之间有一个业务可以同时与域和数据模型进行对话。

Your Presentation Layer does know nothing about the EF/Data Layer/Repository.您的表示层对 EF/数据层/存储库一无所知。 When you want to introduce new Data Framework or database, you just need to write new repository classes and data models classes (which prob. be with some sort of code gen).当您想引入新的数据框架或数据库时,您只需要编写新的存储库类和数据模型类(可能带有某种代码生成)。

This also allows your code to be Unit testable as well.这也使您的代码也可以进行单元测试。

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

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