序列化NHibernate的查询JSON [英] Serialize nHibernate query to JSON

查看:233
本文介绍了序列化NHibernate的查询JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在钻研功能NHibernate,我发现了一个潜在的断路器使用它......



考虑下面的代码POCO



 公共类客户
{
公共虚拟INT标识{搞定;组; }
公共虚拟字符串名称{;组; }
公共虚拟详情详情{搞定;组; }
}

公共类详情
{
公共虚拟INT标识{搞定;组; }
公共虚拟的IList<订单>订单{搞定;组; }
}

公共类CustomerMap:ClassMap<客户>
{
//执行映射
}
公共类DetailsMap:ClassMap<详情>
{
//执行映射
}



我装了。ASP.NET MVC和试图使用JSON序列

 使用System.Web.Script.Serialization; 

公共静态部分类JsonExtensions
{
公共静态字符串的toJSON(此对象的项目)
{
返回新的JavaScriptSerializer()。序列化(项目) ;
}
}

和罗,当我通过从我的NHibernate的查询上下文的的toJSON 的方法,我得到了一个错误!




一个循环引用是而序列化类型'System.Reflection.RuntimeModule'的对象检测。




看来我是否拉一个对象要做到这一点,或对象的列表..或者为此事任何东西。我甚至试过打我的班为 [Serializable接口] 具有相同的结果。这不使用Microsoft实体框架代码,唯一的方法完全相同的类发生。



我无法反序列化NHibernate的DTO的成JSON?



添加更多的代码进行检查。



 使用(VAR会话= sessionFactory.OpenSession()) 
{
使用(VAR交易= session.BeginTransaction())
{
VAR的客户= session.CreateCriteria(typeof运算(客户))名单,LT;客户>();

的foreach(VAR的客户在客户)
{
Console.WriteLine(customer.ToJson());
}
到Console.ReadLine();
}
}


解决方案

这仅仅是一种预感,但你可能想调查什么类型它确实在努力serialize- NHibernate的生成在运行时为每个POCO代理(所以它可以做的东西一样外键的实体延迟加载等)。这可能就是为什么你收到此错误。



尝试指定的确切类型进行序列化或者创建一个全新的对象序列化,填充其属性与NHibernate的POCO的。



编辑:
这似乎是更likley回答你的问题:



http://www.west-wind.com/WebLog/posts/147218.aspx



基本上,检查所有的POCO的任何循环引用(例如,一个客户POCO有订单POCO作为而令POCO具有作为一个属性客户列表的属性)


In delving into Fluent nHibernate, I discovered a potential breaker for using it...

Given the following POCO code.

public class Customer
{
   public virtual int Id { get; set; }
   public virtual string Name { get; set; }
   public virtual Details Details { get; set; }
}

public class Details
{
  public virtual int Id { get; set; }
  public virtual IList<Orders> Orders { get; set; }
}

public class CustomerMap : ClassMap<Customer>
{
 // perform mapping
}
public class DetailsMap : ClassMap<Details>
{
 // perform mapping
}

I loaded up ASP.NET MVC and attempted to use Json Serialization.

using System.Web.Script.Serialization;

    public static partial class JsonExtensions
    {
        public static string ToJson(this object item)
        {
            return new JavaScriptSerializer().Serialize(item);
        }
    }

And Lo, when I passed a query from my nHibernate context to the ToJson method, I got an error!

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

It seems to do this whether I pull a single object, or a list of objects ..or anything for that matter. I've even tried marking my classes as [Serializable] with the same result. This doesn't happen with the exact same classes using the Microsoft Entity Framework Code-Only approach.

Can I not deserialize nHibernate DTOs into JSON?

Adding more code for examination.

        using (var session = sessionFactory.OpenSession())
        {
            using (var transaction = session.BeginTransaction())
            {
                var customers= session.CreateCriteria(typeof(Customer)).List<Customer>();

                foreach (var customer in customers)
                {
                    Console.WriteLine(customer.ToJson());
                }
                Console.ReadLine();
            }
        }

解决方案

This is just a hunch but you might like to investigate what type it is really trying to serialize- nHibernate generates proxies for each POCO at runtime (so it can do stuff like lazy loading of foreign key entities, etc.). This may be why you are getting this error.

Try specifying the exact type to be serialized or perhaps create a brand new object to serialize, populating its properties with that of the nHibernate POCO.

EDIT: This seems to be much more likley the answer to your problems:

http://www.west-wind.com/WebLog/posts/147218.aspx

Basically, check all of your POCO's for any circular references (e.g. a Customer POCO that has an Order POCO as a property while the Order POCO has a list of Customer's as a property)

这篇关于序列化NHibernate的查询JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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