如何序列化的NHibernate的映射对象的所有属性? [英] How do I serialize all properties of an NHibernate-mapped object?

查看:139
本文介绍了如何序列化的NHibernate的映射对象的所有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些网络方法,返回我的对象​​追溯到序列化的XML。它只是序列化对象......任何人的NHibernate的映射属性有一些见解?这似乎是网络的方法实际上是序列化NHibernate的代理,而不是我的班。我已经使用[XMLInclude]和[XMLELEMENT]试过了,但性能仍然没有序列化。我身边有这样获得的真正可怕的hackish的方式,但我想知道是否有更好的方法!

I have some web methods that return my objects back as serialized XML. It is only serializing the NHibernate-mapped properties of the object... anyone have some insight? It seems to be that the web methods are actually serializing the NHibernate proxies instead of my classes. I've tried using [XMLInclude] and [XMLElement], but the properties are still not serializing. I have a really horrible hackish way of getting around this, but I wondered if there was a better way!

事情是这样的:

<?xml version="1.0" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="StoryManager" assembly="StoryManager">
  <class name="Graphic" table="graphics" lazy="false">
    <id name="Id" column="id" type="int" unsaved-value="0" >
      <generator class="identity"/>
    </id>

    <property name="Assigned" />
    <property name="Due" />
    <property name="Completed" />
    <property name="UglyHack" insert="false" update="false" />


    <many-to-one name="Parent" class="Story" column="story_id"/>

  </class>
</hibernate-mapping>

public class Graphic
{
    private int m_id;
    public virtual int Id
    {
        get { return m_id; }
        set { m_id = value; }
    }

    private DateTime? m_assigned;
    public virtual DateTime? Assigned
    {
        get { return m_assigned; }
        set { m_assigned = value; }
    }

    private DateTime? m_due;
    public virtual DateTime? Due
    {
        get { return m_due; }
        set { m_due = value; }
    }

    private DateTime? m_completed;
    public virtual DateTime? Completed
    {
        get { return m_completed; }
        set { m_completed = value; }
    }

    public bool UglyHack
    {
        get { return m_due < m_completed; } // return something besides a real mapped variable
        set {} // trick NHibernate into thinking it's doing something
    }
}

这显然是没有办法写code。如果我没有在那里(UglyHack属性)假的映射,该属性不会被序列化。现在我在找到使用(数据)传输对象,并且可以使用反射将要发生什么...

This is obviously no way to write code. If I don't have the "fake" mapping in there (UglyHack property), that property won't be serialized. For now I'm looking into using (Data) Transfer Objects, and may be on to something using reflection...

推荐答案

序列化NH最好的方法映射对象是不序列化:)。

The best way to serialize the NH mapped object is to not serialize it :).

如果你跨线发送它你真的应该创建一个DTO它。如果你不希望创建一个对象,你可以设置[XmlIgnore]的属性,你不想要序列化。

If you're sending it across the wire you should really create a DTO for it. If you don't want to create that object you can set [XmlIgnore] on properties you don't want serialized.

如果你想要的所有属性,你必须从数据库中的所有加载它们 - 对于一些热切的负荷将足以为他人(其中太多的加入将开始复制数据),你必须访问任何方式财产要触发负载。

If you want all properties, you have to load them ALL from the database - for some an eager load will be enough for others(where too many joins will start duplicating data) you'll have to access that property in any way you want to trigger the load.

编辑:

和我想补充另一件事情 - 通过网络发送您的域的实体是始终一个坏主意。在我的情况下,我学会了硬盘的方式 - 我揭露一些实体在一个WebService - 现在几乎任何改变(重命名属性,删除属性..等),以我的域名杀死使用WS应用程序 - 一大堆加属性有[XmlIgnore]他们(不要忘了循环依赖)。

And I'd like to add another thing - sending your domain entities over the wire is always a bad idea. In my case I learned it the hard way - I expose some entities over a WebService - and now almost any change(rename a property, remove a property ..etc ) to my domain kills the app using the WS - plus a whole bunch of properties have [XmlIgnore] on them ( don't forget about circular dependencies).

我们会做一个重写很快 - 但放心,不是我要的曾经再这样做。 :)

We'll do a rewrite soon enough - but rest assure that's not something I'll ever do again. :)

编辑2

您可以使用 AutoMapper ,在数据从实体到DTO传输。他们在网站上的一些例子。

You could use AutoMapper for transferring the data from your entity to the DTO. They have some examples on the site.

这篇关于如何序列化的NHibernate的映射对象的所有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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