> On Aug 10, 2016, at 10:55 AM, Michael Powell <mwpowell...@gmail.com> wrote:
> 
> Hello,
> 
> I have a use case where I'd like to cast an abstract base class to a
> child class in order to verify one of the child class properties.
> 
> Consider, an albeit somewhat contrived example:
> 
> abstract class Juice { }
> class AppleJuice : Juice { public virtual double Acidity { get; set; } }
> class OrangeJuice : Juice { public virtual double Tanginess { get; set; } }
> 
> class Cup { public virtual Juice Juice { get; set; } }
> 
> I would like to test the Juice in the Cup in this manner:
> 
> Cup cup = ...;
> if (((ApplyJuice) cup.Juice).Acidity > 1.5)
> {
>    // Respond to acidity...
> }
> 
> However, what I am finding is that the cast doesn't work "properly",
> due to the proxies involved, I get InvalidCastException:
> 
> "Unable to cast object of type 'JuiceProxy' to type 'AppleJuice'"
> 
> Which I suppose is not surprising, but I wondered with the support for
> class hierarchies, is there a recommended workaround? Do I need to do
> some other form(s) of casting, or have some event handlers connecting
> with ISession bits?

I solved this by using a core non-persistent class that implements the 
properties of Cup, then a persistent class that inherits from Cup that 
implements my Hibernate and persistent layer (created by, created on, etc).

Then I can cast the object to the non-persistent class when I need to ensure 
the proxies aren’t getting in the way. Unfortunately that was an early design 
decision and isn’t easy to implement on a larger/older DAL.

Just my 2c.

> 
> Thanks!
> 
> Regards,
> 
> Michael Powell
> 
> --
> You received this message because you are subscribed to the Google Groups 
> "Fluent NHibernate" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to fluent-nhibernate+unsubscr...@googlegroups.com.
> To post to this group, send email to fluent-nhibernate@googlegroups.com.
> Visit this group at https://groups.google.com/group/fluent-nhibernate.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to fluent-nhibernate+unsubscr...@googlegroups.com.
To post to this group, send email to fluent-nhibernate@googlegroups.com.
Visit this group at https://groups.google.com/group/fluent-nhibernate.
For more options, visit https://groups.google.com/d/optout.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to