基于 Fluent Nhibernate 模型的 ASP.NET MVC JSON

发布于 2024-11-19 19:10:49 字数 875 浏览 3 评论 0原文

我试图使用 MVC 控制器标准 Json(object) 方法返回 JsonResult 。我的 Model1 类型的对象是由 Fluent NHibernate 构建的。

Model1 具有 Model2 类型的属性。在调试模式下,我看到环境创建了一个名为 Castle.Proxies.Model2ProxyModel2 代理后代类。我相信 Fluent Nhibernate 在内部使用它来满足我的映射。在运行时,实际 model1.Model2 的类型为 Castle.Proxies.Model2Proxy

问题是,当我的 Model1 被序列化时,Model2 也在被序列化。并且序列化器似乎尝试序列化该对象的所有属性,包括那些由 Castle 生成且我不需要的属性。如果它没有引起异常,我会同意的。也就是说,该对象内部的某处存在循环引用,并且异常是由它引起的。以下是异常文本:

System.InvalidOperationException: 在序列化类型为“System.Reflection.RuntimeModule”的对象时检测到循环引用

我仔细检查了我的域,发现那里没有循环引用,所以我责怪城堡。我说得对吗? Castle真的应该为此负责吗?如果是这样,我有什么选择?如何告诉序列化程序忽略 Castle 属性?特别是,我如何告诉它序列化定义的类型,而不是实际类型?

我倾向于用 ViewModel 覆盖我的域模型来解决这个问题,这是推荐的方法,但我真的很想知道另一种解决方法(如果存在)。

I am trying to return JsonResult using MVC controller standard Json(object) method. My object of type Model1 is built by Fluent NHibernate.

Model1 has property of type Model2. In debug mode I see that the environment creates a proxy descendant class of Model2 called Castle.Proxies.Model2Proxy. This is used internally by Fluent Nhibernate, I believe, to satisfy my mappings. And in run time, actual model1.Model2 is of type Castle.Proxies.Model2Proxy.

The problem is that when my Model1 is serialized, Model2 is being serialized too. And the serializer seems to try to serialize all the properties of this object, including those generated by Castle and not needed by me. I would be OK with it if it did not cause an exception. Namely, somewhere inside this object a circular reference presents and the exception is caused by it. Here is the exception text:

System.InvalidOperationException: A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'

I double checked my domain and found no circular references there, so I am blaming Castle. Am I correct? Is Castle really to blame for that? If so, what are my options? How do I tell serializer to ignore Castle properties? Particularly, how do I tell it to serialize the defined type, not the actual one?

I tend to covering my domain models with ViewModels to fight this issue, which is a recommended approach, but I would really love to know another cure, if it exists.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆° 2024-11-26 19:10:49

一般来说,序列化模型实体不是一个好习惯。
这是因为您希望完全控制序列化并发送到客户端的内容。
当您序列化模型实体时,您可能会序列化与它们关联的整个对象图,而您不一定需要/想要。
(例如,如果您希望用户仅查看 Model1 实体,您可能还会发送 Model2 实体及其 Model3 > 集合等)

处理它的标准方法是使用某种 DTO,它适合精确显示您想要显示的内容。例如:

public class Model1DTO
{
   public int Id;
   public string Name;
   public string Model2Name;
   //whatever other properties you need to display
}

in general, it's not good practice to serialize your model entities.
this is because you want to have complete control over what you serialize and send to the client.
when you serialize your model entities, you might be serializing the whole object graph associated with them, which you don't necessarily need / want.
(for example- if you want the user to view just a Model1 entity, you might be sending along also a Model2 entity, along with its Model3 collection etc.)

the standard way to deal with it is using some sort of DTOs, which are adapted to display precisely what you want to display. for example:

public class Model1DTO
{
   public int Id;
   public string Name;
   public string Model2Name;
   //whatever other properties you need to display
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文