为EF 4.1 + MVC + JSON循环引用异常最好的解决办法? [英] Best solution for EF 4.1 + MVC + JSON circular reference exception?

查看:237
本文介绍了为EF 4.1 + MVC + JSON循环引用异常最好的解决办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是EF 4.1数据库的第一种方法,用T4模板生成我的POCO类中单独的程序。我有存储库提取数据,并且其用于与用户界面通信服务层

I'm using EF 4.1 Database First approach, with T4 template generating my POCO classes in separate assembly. I have repositories for fetching data, and service layer which is used for communication with UI.

我试图让级联下拉菜单。 我是新的MVC和EF 4.1,所以我搜索计算器可能的解决方案。

I was trying to make cascading dropdowns. I'm new in MVC and EF 4.1, so I searched stackoverflow for possible solutions.

这是一个示例视图模型类:

This is sample viewmodel class:

public class MyViewModel
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public IEnumerable<Phone> Phones { get; set; }
}

我已经阅读到目前为止,解决方案是:

What I have read so far, solutions are:

  1. 使用 ScriptIgnoreAttribute System.Web.Script.Serialization 上 引用属性 - 我不知道 真正要做到这一点,因为我 不想添加引用 的System.Web在我的POCO项目

  1. Use ScriptIgnoreAttribute in System.Web.Script.Serialization on referencing properties - I don't really want to do this, because I don't want to add reference to System.Web in my POCO project

禁用延迟加载在EF 4.1的DbContext - 我不知道我想和使用包括制定深入我的项目

Disable Lazy Loading in EF 4.1 DbContext - I'm not sure do I want to use Include thorough my project

返回匿名类型 - ?我将有问题,这种方法的时候,我的项目得到大

Return anonymous types - will I have problems with this approach when my project gets big?

使用一个视图模型 - 假设我有一个客户,可以有1个或更多的手机。在第一个下拉列表中,您可以选择客户,并在第二个下拉列表你展示他所有的手机。
但是,这是不是产生在我的手机圆形异常对象?或者,我会做一类特殊的我的电话的对象?这似乎是一个很大的unnecesarry code。

Use a ViewModel - suppose I have a Customer which can have 1 or more phones. In first dropdown list you can select Customer, and in second dropdown you display all of his phones.
But wouldn't this generate a circular exception on my Phones object? Or i would make a special class for my Phone object? That seems like a lot of unnecesarry code.

使用AutoMapper - 没有与AutoMapper的经验,所以我不知道它是多么复杂的

Use AutoMapper - don't have experience with AutoMapper, so I don't know how complicated it is.

有关哪一个,你会选,为什么?

For which one would you vote and why?

推荐答案

使用视图模型和 AutoMapper 以之间进行映射您的域模型,您将被发送到视图视图模型。这样你可以完全控制哪些属性将被发送到哪个视图作为结果减少了服务器和客户端之间发送的数据量。此外,因为您现在使用视图模型的code是更适应您的域中的实体的修改。如果你修改它们,只有映射层会受到影响,因此你永远不会需要触摸到你的控制器或视图。

Use a view model and AutoMapper to map between your domain models and the view model which you will be sending to the view. This way you have total control over what properties are sent to the view which as a consequence reduces the amount of data sent between the server and the client. Also because now you are using view models your code is more resilient to modifications in your domain entities. If you modify them, only the mapping layer will be affected and thus you will not ever need to touch to your controllers or views.

所以我的建议是下载AutoMapper,阅读文档并开始使用它。这是一个生命换,相信我。

So my advice is to download AutoMapper, read the documentation and start using it. It's a life changer, believe me.

这篇关于为EF 4.1 + MVC + JSON循环引用异常最好的解决办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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