Skip to content

Instantly share code, notes, and snippets.

@bahusoid
Last active September 23, 2020 11:12
Show Gist options
  • Save bahusoid/55d8dfc3d02abfb270e4228fe50ab480 to your computer and use it in GitHub Desktop.
Save bahusoid/55d8dfc3d02abfb270e4228fe50ab480 to your computer and use it in GitHub Desktop.
Test case for GH2563
using System;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
namespace NHibernate.Test.NHSpecificTest.GH2563
{
[Serializable]
public class Entity
{
public virtual int Id { get; set; }
public virtual Entity Parent { get; set; }
public virtual Entity Child { get; set; }
public virtual string Name { get; set; }
}
[TestFixture]
public class ByCodeFixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<Entity>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Assigned));
rc.Property(x => x.Name, m => m.Lazy(true));
rc.ManyToOne(x => x.Child);
rc.ManyToOne(x => x.Parent);
});
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity
{
Id = 1,
Name = "Middle",
};
e1.Parent = new Entity
{
Id = 2,
Name = "Parent",
Child = e1
};
session.Save(e1);
session.Save(e1.Parent);
transaction.Commit();
}
}
protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from System.Object").ExecuteUpdate();
transaction.Commit();
}
}
[Test]
public void SerializationOfConnectedEntitiesWithLazyProperties()
{
using (var session = OpenSession())
{
var result = session.Query<Entity>().ToList();
var ds = SpoofSerialization(session);
}
}
private T SpoofSerialization<T>(T session)
{
var formatter = new BinaryFormatter();
var stream = new MemoryStream();
formatter.Serialize(stream, session);
stream.Position = 0;
return (T) new BinaryFormatter().Deserialize(stream);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment