On Thursday, August 11, 2016 at 7:43:27 AM UTC-4, Rasmoo wrote: > > You can't cast the proxy. You can cast the proxied object. Therein lies > the difference. > You haven't told me anything I don't already know, and/or learned about the nature of NHibernate and the proxied domain objects.
I try to avoid casting anyways. I'd rather use a visitor pattern as it > maintains completeness. > Thank you. On Thu, Aug 11, 2016 at 1:33 PM, mwpowellhtx <mwpow...@gmail.com > <javascript:>> wrote: > >> >> >> On Thursday, August 11, 2016 at 7:23:35 AM UTC-4, Rasmoo wrote: >>> >>> That's exactly Oskar's trick: You call the proxy, into the proxied >>> object and get it to cast itself. >>> >> >> No, that's not what Oskar wrote, one the one hand; see below: >> >> public TCastTarget As<TCastTarget>() >> { >> return (TCastTarget)this; >> } >> >> This is not any different than what I attempted in the LINQ statement. >> >> On the other hand, introducing an "UnproxiedObject" virtual property is >> too much. I want to keep the model itself as clean as possible without >> inverting proxy dependency inversions more than is necessary. >> >> Appreciate the constructive input, though. >> >> cup.Juice.As<AppleJuice>() >>> >>> Try it. :-) >>> >>> Alternative, you can return the inner, unproxied object, by adding a >>> method like this to the base class: >>> >>> public virtual object UnproxiedObject { get { return this; } } >>> >>> Then it's >>> >>> cup.Juice.UnproxiedObject as AppleJuice >>> >>> >>> On Thu, Aug 11, 2016 at 12:47 PM, mwpowellhtx <mwpow...@gmail.com> >>> wrote: >>> >>>> >>>> >>>> On Wednesday, August 10, 2016 at 1:20:15 PM UTC-4, Oskar Berggren wrote: >>>>> >>>>> You can put something like this in relevant base class: >>>>> >>>>> public TCastTarget As<TCastTarget>() >>>>> { >>>>> return (TCastTarget)this; >>>>> } >>>>> >>>> >>>> Do you understand what's going on with Proxying? You don't see it in >>>> your code, but what you end up with is something like this: >>>> >>>> class JuiceProxy : Juice { ... } >>>> class AppleJuiceProxy : AppleJuice { ... } >>>> class OrangeJuiceProxy : OrangeJuice { ... } >>>> >>>> Something like that, if memory serves, but I haven't verified the >>>> actual inheritance tree. >>>> >>>> And since Cup wants a Juice, JuiceProxy fits the bill, but there is no >>>> reasonable way for JuiceProxy to be side-cast into the expected >>>> AppleJuice, >>>> much less AppleJuiceProxy, without losing necessary hierarchical details. >>>> >>>> Then you don't have to turn off proxying. >>>>> >>>> >>>>> /Oskar >>>>> >>>>> >>>>> >>>>> 2016-08-10 18:02 GMT+01:00 Michael Powell <mwpow...@gmail.com>: >>>>> >>>>>> On Wed, Aug 10, 2016 at 11:55 AM, Michael Powell <mwpow...@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-nhibern...@googlegroups.com. >>>>>> To post to this group, send email to fluent-n...@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-nhibern...@googlegroups.com. >>>> To post to this group, send email to fluent-n...@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-nhibern...@googlegroups.com <javascript:>. >> To post to this group, send email to fluent-n...@googlegroups.com >> <javascript:>. >> 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.