Archive for October 28th, 2009
nHibernate and Remoting/Shallow Serialization
by Alec Horn on Oct.28, 2009, under .NET/C#, Coding, nHibernate
So, recently had an issue with Weborb and nHibernate serializing our entire object model due to deep serialization. After much research and my lack of deep knowledge of nHibernate I came across http://trentacular.com/tag/nhibernate/. Shamelessly stolen, of course this solved my problems immediately allowing me to specify n levels of serialization to be kept down the line. I made a few alterations to allow for collections to be serialized:
if (nhType.IsCollectionType)
{
object propertyValue = propertyInfo.GetValue(unproxiedObject, null);
if (propertyValue == null)
continue;
if (propertyValue is PersistentBag)
{
PersistentBag holder = (PersistentBag)propertyValue;
for (int j = 0; j < holder.Count; j++)
{
holder[j] = holder[j].UnproxyObjectTree(sessionFactory, maxDepth - 1);
}
propertyInfo.SetValue(
unproxiedObject,
holder,
null);
}
else if (propertyValue is ISet)
{
ISet holder = (ISet)propertyValue;
ArrayList tempcoll = new ArrayList();
foreach (object o in (IEnumerable)propertyValue)
{
tempcoll.Add(o);
}
for (int j = 0; j < tempcoll.Count; j++)
{
tempcoll[j] = tempcoll[j].UnproxyObjectTree(sessionFactory, maxDepth - 1);
}
holder.Clear();
holder.AddAll(tempcoll);
propertyInfo.SetValue(
unproxiedObject,
holder,
null);
}
continue;
}