实体框架 – MVC3应用程序/服务层/存储库层/ POCO类/ EF4 – 问题!

我对这个整体设计理念是新的,在过去几个星期的阅读中,我收集了很多信息,但似乎分散和冲突。条款是混合的,我只是很困惑地围绕着这一点。

我使用的模式是这样的,并假设流程如下:

MVC应用
控制器处理来自客户端的给定视图的请求/响应。在控制器操作方法中,他们联系服务(服务层)和请求对象以构建视图模型,并从视图模型中读取对象。

查看模型
我使用强力类型的视图模型来和从视图。

观看模型DTO的?它们是否包含简单的属性,如Name,AddressLine1,Address City等,或者它们应该包含复杂的属性,多个对象等。

是视图模型中的验证。如果是这样的话,它将像需要的字段,字段长度等一样进行验证。然后像用户名一样的验证已经存在,或者您需要与服务层中的其他对象进行交互?

视图模型可以包含从EF返回的POCO类,还是应该使用AutoMapper?

如果使用AutoMapper和DTO,DTO的POCO类的克隆?

您将映射到控制器,查看模型或下面的服务层?

服务
对我来说,服务是与存储库联系以从EF获取POCO对象的对象。这就是我所有的业务逻辑。一旦服务将对象重新发送到存储库以保留到EF,则它们被认为是有效的对象。它是否正确?

存储库
它们中没有业务逻辑,它们仅用于在服务和EF之间传输对象。它是否正确?我正在使用通用存储库实现Interfaces。那么你可以扩展通用的存储库来满足特殊需求吗?

关于术语的问题
1)业务对象是否等于域对象?域对象包含多少逻辑?

2)域模型是EF模型吗?我使用Model-First方法

3)依赖注入 – 我应该用吗?我明白它是如何工作的,只是没有得到真正的好处。我在玩Ninject。

我认为社区将受益于某种wiki,其中包含所有代码示例的最佳做法。那里有什么东西吗?很多样本都很简单,而且很多微软的示例即使在声明时也不会使用这种模式。

提前感谢所有有和将会帮助我的人。

BTW – 我认为StackOverflow需要一点点,“Buy Me A Beer”按钮旁边的“Accept Answer”复选框:)

解决方法

Are view models DTO’s?

可以被认为是控制器和视图之间的一种数据传输对象。

Should they contain just simple properties like Name,AddressLine1,Address City,etc,or should they contain complex properties,multiple objects,etc.

理想的简单属性,但也可以聚合其他视图模型,但没有模型(例如:EF模型)。

Is the validation in the view model.

有两种类型的验证逻辑:进入服务层的业务验证(例如用户名已经存在),进入视图模型的UI验证(例如:username)。

Can the view models just contain the POCO classes returned from EF,or should I be using the AutoMapper?

没有EF在视图模型。视图模型是具有简单属性和其他复杂属性的POCO类,指向其他视图模型。他们还可以包含方法,以便正确格式化将在特定视图中呈现的数据,这些模型将用于此类数据。

If using AutoMapper and DTO,are DTO’s clones of the POCO classes?

不知道我明白这个问题。

Would you map in the controller,view model,or in the service layer below?

控制器。

To me,the service(s) are objects that contact the repository(s) to get POCO objects back from the EF. This is where all of my business logic is. Once the service hands an object back to a repository to be persisted to the EF,they are considered valid objects. Is this correct?

是。

Is the domain model the EF model?

如果您使用EF代码第一种方法,那么是,否则否(如果EF用EF特定属性和类别污染域名)。

There is no business logic in them,they are just used to transport objects between the service(s) and the EF. Is this correct?

是。

I am implementing Interfaces here with generic repository. Then you Could extend the generic repository for special needs?

是的,但不要太过分。通常存储库用于CRUD操作。它应该包含业务逻辑的服务。

Is a business object equal to a domain object?

是。

How much logic should a domain object contain?

这将取决于您对您正在工作的特定项目的领域逻辑的数量以及您可能从旧的项目或其他人工作过的任何现有域逻辑。

Dependency Injection – Should I be using this?

是的,一点没错。

I understand how it works,just don’t get the real benefit

它在应用程序的不同层次之间提供较弱的耦合,从而使其更容易在其他项目中进行单元测试和重用。

I think the community would benefit from some sort of wiki that contained all the best practices with code samples.

我同意。

Is there something like that out there?

我对此表示怀疑。

BTW – I think StackOverflow needs a little,“Buy Me A Beer” button next to the “Accept Answer” checkBox

不能同意更多。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“WPF...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这...
Some samples are below for ASP.Net web form controls:(from http://www.visualize.uk.com/resources/asp
问题描述: 对于未定义为 System.String 的列,唯一有效的值是(引发异常)。 For columns not defined as System.String, the only vali
最近用到了CalendarExtender,结果不知道为什么发生了错位,如图在Google和百度上找了很久,中文的文章里面似乎只提到了如何本地化(就是显示中文的月份)以及怎么解决被下拉框挡住的问题,谈
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence changed a lot since 1.1. Here is the order: App
静态声明: ' Style="position: relative" AppendDataBoundItems="True"> (无 或 空 或
以下内容是从网络上搜集资料,然后整理而来的。不当之处,请不吝指教。(The following were from network, and edited by myself. Thanks in a
Imports System Imports System.Reflection Namespace DotNetNuke '*********************************
Ok so you have all seen them: “8 million tools for web development”, “5 gagillion tools that if you
以下内容来源于: http://blog.csdn.net/cuike519/archive/2005/09/27/490316.aspx 问:为什么Session在有些机器上偶尔会丢失? 答:可能和
以下文章提到可以用“http://localhost/MyWebApp/WebAdmin.axd”管理站点: ---------------------------------------------
Visual Studio 2005 IDE相关的11个提高开发效率的技巧 英文原创来源于: http://www.chinhdo.com/chinh/blog/20070920/top-11-vis
C#日期格式化 from: http://51xingfu.blog.51cto.com/219185/46222 日期转化一 为了达到不同的显示效果有时,我们需要对时间进行转化,默认格式为:2007
from: http://www.nikhilk.net/UpdateControls.aspx Two controls that go along with the UpdatePanel and
Open the report in the Designer. In the ToolBox, select/expand the "Report Items" section.
from: http://drupal.org/node/75844 Do this: find which TinyMCE theme you are using. For the sake of
asp.net中给用户控件添加自定义事件 用户控件中定义好代理和事件: public delegate void ItemSavedDelegate(object sender, EventArgs
在Windows版本的Safari中浏览以下的页面。 http://www.asp.net/AJAX/Control-Toolkit/Live/Calendar/Calendar.aspx Calen
http://aspnet.4guysfromrolla.com/articles/021506-1.aspx By Scott Mitchell Introduction When creating