EF 4.1 的最佳解决方案MVC + JSON循环引用异常?
我使用 EF 4.1 Database First 方法,使用 T4 模板在单独的程序集中生成 POCO 类。我有用于获取数据的存储库,以及用于与 UI 通信的服务层。
我试图制作级联下拉菜单。 我是 MVC 和 EF 4.1 的新手,因此我在 stackoverflow 中搜索了可能的解决方案。
这是示例视图模型类:
public class MyViewModel
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public IEnumerable<Phone> Phones { get; set; }
}
到目前为止我读到的解决方案是:
Use
ScriptIgnoreAttribute
inSystem.Web.Script.Serialization
上 引用属性 - 我不 真的很想这样做,因为我 不想添加参考 我的 POCO 项目中的 System.Web在 EF 4.1 DbContext 中禁用延迟加载 - 我不确定是否要使用 Include 彻底我的项目
返回匿名类型 - 当我的项目变大时,这种方法会遇到问题吗?
使用 ViewModel - 假设我有一个可以拥有 1 部或多部手机的客户。在第一个下拉列表中,您可以选择客户,在第二个下拉列表中,您可以显示他的所有电话。
但这不会在我的 Phones 对象上生成循环异常吗?或者我会为我的 Phone 对象创建一个特殊的类?这似乎有很多不必要的代码。使用 AutoMapper - 没有使用 AutoMapper 的经验,所以我不知道它有多复杂。
您会投票给哪一个?为什么?
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.
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:
Use
ScriptIgnoreAttribute
inSystem.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 projectDisable 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?
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.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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用视图模型和 AutoMapper 在域模型和要发送到视图的视图模型之间进行映射。通过这种方式,您可以完全控制发送到视图的属性,从而减少服务器和客户端之间发送的数据量。另外,因为现在您正在使用视图模型,所以您的代码对域实体中的修改更具弹性。如果您修改它们,只有映射层会受到影响,因此您将不需要触摸控制器或视图。
所以我的建议是下载 AutoMapper,阅读文档并开始使用它。这是一个生活的改变,相信我。
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.
So my advice is to download AutoMapper, read the documentation and start using it. It's a life changer, believe me.