﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>atx commented on How NHibernate forced me to OO design</title><description>I have a similar problem with base classes and proxies, but mine also involves sublassing with discriminators. 
  
  
The setup is following:
  
- base class A, has Lazy=true, subclasses A1, A2 &amp; A3 (table-per-class pattern - one table with columns for all class tree), discriminator field named e.g. "ASubClass"
  
- class B has some property of type A (e.g. "myA")
  
  
Now, I am trying to do the following:
  
B myB = B.FindOne(.....);
  
A myA = B.myA;
  
if (myA.ASubClass == "1")   //1 is discriminator value in subclass A1
  
{
  
    //discriminator says it is an A1!
  
    A1 myA1 = (A1)myA;  //Error!
  
}
  
  
Now, the line marked as Error, yields an "cannot cast NhibernateProxy to A1" exception - similarly to what you wrote in this post, the actual "myA" is an NHibernateProxy subclassing class A. However, it should rather be an proxy subclass of A1... Frankly speaking, I don't know how to go around this... How to cast the lazy initialized object to its subclass? I do not fully understand the solution that you gave here, Ayende :(
</description><link>http://ayende.com/3691/how-nhibernate-forced-me-to-oo-design#comment2</link><guid>http://ayende.com/3691/how-nhibernate-forced-me-to-oo-design#comment2</guid><pubDate>Fri, 21 Nov 2008 15:50:11 GMT</pubDate></item><item><title>Victor Kornov commented on How NHibernate forced me to OO design</title><description>Heh, that should read "Tell, don't ask" :)
  
  
I wonder, what DisplayInformation looks like. Do you use it like:
  
if(info.IsImage) {
  
...
  
} else {
  
...
  
}
</description><link>http://ayende.com/3691/how-nhibernate-forced-me-to-oo-design#comment1</link><guid>http://ayende.com/3691/how-nhibernate-forced-me-to-oo-design#comment1</guid><pubDate>Wed, 12 Nov 2008 18:48:08 GMT</pubDate></item></channel></rss>