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!
Something like this:
<?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
}
}
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...