On Wed, Aug 10, 2016 at 11: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.

In my joining table mapping, in this case the reference from Cup to
Juice, I do something like this, which seems to work, but will want to
test it heavily.

References(x => x.Juice, "JuiceId")
    .LazyLoad(Laziness.NoProxy)
    .Not.Nullable();

And this seems to do the trick; at least there is literally no proxy
involved and I can do the casting that I want to do. I might lose some
proxiness about its usage, though, so need to test it more thoroughly.

> 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?
>
> 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.

Reply via email to