2016-08-11 12:33 GMT+01:00 mwpowellhtx <mwpowell...@gmail.com>:

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

It is in fact completely different. It is applying the cast to an entirely
different object.



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

It is possible to provide both the As<T>() and "Unproxy()" methods as
extension methods instead of putting them in each class. The difference is
that the body of the method then need to explicitly check if the object is
a proxy and ask NHibernate for the real object before applying the cast.

Untested, but something like this:

public static T As<T>(this object obj)
{
    if (obj is INHibernateProxy)
        obj =
((INHibernateProxy)obj).HibernateLazyInitializer.GetImplementation();
    return (T)obj;
}

You could even do:
    public interface IUnwrappable { /* empty marker interface */ }
    public static T As<T>(this IUnwrappable obj) { ... }
if you want to avoid exposing the extension method except for classes that
you explicitly allow it for.


/Oskar

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