ASP.NET MVC + EF 4.1分层,模型,的ViewModels [英] ASP.NET MVC + EF 4.1 layering, models, viewmodels

查看:119
本文介绍了ASP.NET MVC + EF 4.1分层,模型,的ViewModels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了几篇文章,并在ASP.NET有关分层架构的问题,但是我太阅读后变得有点混乱。用户界面层在ASP.NET MVC和我在项目中使用EF数据访问开发的。

我想通过一个例子来说明我的问题。比方说,我有一个简单的博客引擎,具有以下实体:帖子,评论,分类,用户。我用EF数据库的方法,并生成POCO-S在我的模型层到数据模型类库,生成的datacontext和EDMX转到数据访问库。

在此之上我有一个业务层。这是负责例如返回与评论的博客条目。在我的UI层我使用视图模型类,因为显示的条目,我需要既邮政实体和评论与同一观点的用户名列表。

而现在我的问题:我的观点并不需要以显示某条信息的用户的实体,只是名字的每一个细节。现在的问题是我应该做我的ViewModels和模型类之间的映射?如果业务层做到这一点?或者我应该用每一个细节返回实体,让UI处理映射?如果业务层包含的ViewModels作为一个类库?

什么是最好的办法了吗?


解决方案

  

问题是我应该做我的ViewModels和模型类之间的映射?


理想地在一个单独的映射层。如果您使用 AutoMapper 映射可以在ASP.NET MVC项目单独的文件中声明。


  

如果业务层做到这一点?


绝对不是。业务层没有任何视图模型的知识。


  

如果业务层包含的ViewModels作为一个类库?


没有。 UI层(ASP.NET MVC应用程序)是具有视图模型的知识的唯一层。他们当然可以是一个单独的类库,如果你愿意的话,但它只是应该引用它的UI层。查看模型紧密耦合的意见。和观点是UI的一部分。

I read a couple of articles and questions related to layered architecture in ASP.NET, however I got a bit confused after too much reading. The UI layer is developed in ASP.NET MVC and for the data access I use EF in my project.

I'd like to describe my problem through an example. Let's say I have a simple blog engine, with the following entities: Post, Comment, Category, User. I use EF database approach and generate POCO-s in my model layer to a data model class library, the generated datacontext and the EDMX goes to the data access library.

Above this I have a business layer. This is responsible for example to return a blog entry with comments. In my UI layer I'm using ViewModel classes because for displaying an entry I need both a Post entity and a list of Comments with usernames on the same view.

And now my problem: My view doesn't need every details of a User entity, just the name in order to display a post. The question is where should I do the mapping between my ViewModels and Model classes? Should the business layer do this? Or I should return the entities with every details and let the UI handle the mapping? Should the business layer contain ViewModels as a class library?

What is the best approach for this?

解决方案

The question is where should I do the mapping between my ViewModels and Model classes?

Ideally in a separate mapping layer. If you use AutoMapper mappings could be declared in separate files in the ASP.NET MVC project.

Should the business layer do this?

Absolutely not. The business layer has no knowledge of any view models.

Should the business layer contain ViewModels as a class library?

No. The UI layer (ASP.NET MVC application) is the only layer that has knowledge of view models. They could of course be in a separate class library if you wish, but it is only the UI layer that should reference it. View models are tightly coupled to views. And views are part of the UI.

这篇关于ASP.NET MVC + EF 4.1分层,模型,的ViewModels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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