D
D A M
Guest
I'm going through a book on NHibernate and have run into a casting issue in a unit test. Here's the issue:
public class Base
{
}
public class Derived : Base
{
public string property1 { get; set; }
}
public class Derived1 : Base
{
public string property1 { get; set; }
public string property2 { get; set; }
}
public class Derived2 : Base
{
public string property1 { get; set; }
public string property2 { get; set; }
public string property3 { get; set; }
}
public void TestDerived( )
{
object id = 0;
using( var transaction = session.BeginTransaction( ) )
{
id = session.Save( new OtherClassWithCollectionOfBase
{
Number = "123456789",
CollectionOfBase = new HashSet<Base>
{
new Derived
{
property1 = "value"
},
new Derived1
{
property1 = "value",
property2 = "value2"
},
new Derived2
{
property1 = "value",
property2 = "value2",
property3 = "value3",
}
}
} );
transaction.Commit( );
}
session.Clear( );
using( var transaction = session.BeginTransaction( ) )
{
var otherclass = session.Get<OtherClassWithCollectionOfBase>( id );
Assert.That( otherclass.CollectionOfBase.Count, Is.EqualTo( 3 ) );
var derived = otherclass.CollectionOfBase.OfType<Derived>( ).FirstOrDefault( );
Assert.That( derived, Is.Not.Null );
transaction.Commit( );
}
This asserts with derived == null. I've also tried
var derived = otherclass as Derived;
and
var derived= otherclass.CollectionOfBase.FirstOrDefault(b => b.GetType() == typeof (Derived));
I've tried a hard cast on each member of the collection with the expected result NullReferenceException. Can this just not be done or have I done something wrong in the code?
Continue reading...
public class Base
{
}
public class Derived : Base
{
public string property1 { get; set; }
}
public class Derived1 : Base
{
public string property1 { get; set; }
public string property2 { get; set; }
}
public class Derived2 : Base
{
public string property1 { get; set; }
public string property2 { get; set; }
public string property3 { get; set; }
}
public void TestDerived( )
{
object id = 0;
using( var transaction = session.BeginTransaction( ) )
{
id = session.Save( new OtherClassWithCollectionOfBase
{
Number = "123456789",
CollectionOfBase = new HashSet<Base>
{
new Derived
{
property1 = "value"
},
new Derived1
{
property1 = "value",
property2 = "value2"
},
new Derived2
{
property1 = "value",
property2 = "value2",
property3 = "value3",
}
}
} );
transaction.Commit( );
}
session.Clear( );
using( var transaction = session.BeginTransaction( ) )
{
var otherclass = session.Get<OtherClassWithCollectionOfBase>( id );
Assert.That( otherclass.CollectionOfBase.Count, Is.EqualTo( 3 ) );
var derived = otherclass.CollectionOfBase.OfType<Derived>( ).FirstOrDefault( );
Assert.That( derived, Is.Not.Null );
transaction.Commit( );
}
This asserts with derived == null. I've also tried
var derived = otherclass as Derived;
and
var derived= otherclass.CollectionOfBase.FirstOrDefault(b => b.GetType() == typeof (Derived));
I've tried a hard cast on each member of the collection with the expected result NullReferenceException. Can this just not be done or have I done something wrong in the code?
Continue reading...