EF 4 POCO类库MVC 2模型 [英] EF 4 with POCO in class library as MVC 2 model

查看:102
本文介绍了EF 4 POCO类库MVC 2模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索实体框架4 POCO作为我的一个MVC2 Web应用程序模型。我需要在一个单独的库中的模型和数据访问code的原因是这样我就可以与作为一个门户网站,为广大客户访问我们的数据另一个Web应用程序共享。

I am exploring Entity Framework 4 with POCO as my Model for an MVC2 web app. The reason I'll need the model and data access code in a separate library is so I can share it with another web application that serves as a portal for our clients to access our data.

我的问题是,我将在另外的一个项目我EF4 POCO和code丢失任何典型的MVC2模式的特点?或者,也许另一种方式来问这个是将MVC框架能够与模型层的工作是一样的,如果它是在一个单独的项目与在Models文件夹?

My question is, will I lose any of the typical MVC2 Model features by having my EF4 and POCO code in another project? Or maybe another way to ask this is will the MVC framework be able to work with the Model layer just the same if it is in a separate project versus in the Models folder?

编辑:我忘了提,我使用POCO,而不是生成的类通常用EF得到每个实体

I forgot to mention that I am using POCO rather than the generated classes you normally get with EF for each entity.

推荐答案

我从来不喜欢模型文件夹反正。我宁愿把独立的层/层在不同的组件。 MVC项目模板只是让单一组件中的分层应用。

Single assembly projects only for small projects

I never liked "Models" folder anyway. I rather put separate layers/tiers in separate assemblies. MVC project template just makes a layered application in a single assembly.

做的几个项目我preFER解决方案:

I prefer solutions made of several projects:


  • 网络 - 前端Web应用程序的IE浏览器。 Asp.net MVC(引用的服务对象

  • 服务 - 业务层组件(引用数据对象

  • 对象 - POCO类,这都是通过共享接口等(不引用任何其他解决方案项目)

  • 数据 - 实际EF模型和扩展,仓库等(参考文献对象

  • Web - front end web application ie. Asp.net MVC (references Services and Objects)
  • Services - business tier assembly (references Data and Objects)
  • Objects - POCO classes, interfaces etc. that are shared all over (doesn't reference any other solution project)
  • Data - The actual EF model and extensions, repositories etc (references Objects)

这是一个更加可控性和结构化的方式,如果你的应用是不是只有几个微不足道的屏幕更大。

This is a much more controllable and structured way if your application is larger than just a few trivial screens.

因此​​,在过去的项目中:EF模式是在数据项目(以及仓库等),并波苏斯其中的对象。项目之间的所有通信与类做的对象的项目。

So in a past project: EF model was in the Data project (as well as repositories etc), and POCOs where in Objects. All communication between projects was done with classes from Objects project.

所以网络使用的服务,结果
这反过来又使用存储库(在数据),结果
这又被称为EF操纵数据。结果
所有的来回传递数据使用波苏斯对象

So Web used Services,
which in turn used repositories (in Data),
which in turn called EF to manipulate data.
All data passing back and forth was using POCOs in Objects.

所以库不得不实体转换为实际波苏斯(它们不是1:1)返回数据和创建当有写操作实体时。而这种转变是通过对实体扩展方法完成的,所以转型总是在一个单一的方法,这使得它易于维护的时候,我们要么改变实体或POCO类来完成。我们没有为扫描所有code用于所有转换,我们只是调整了 ToPoco()扩展方法(实际上他们被称为是这样)。

So repositories had to transform entities to actual POCOs (they were not 1:1) when returning data and creating entities when there were write operations. And this transformation was done via extension methods on entities, so transformation was always done in a single method, which made it simpler to maintain when we either changed an entity or a POCO class. We didn't have to scan all the code for all conversions, we just adjusted our ToPoco() extension method (they were actually called like this).

这篇关于EF 4 POCO类库MVC 2模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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