Lazy loading and static/no proxy

9 views
Skip to first unread message

dnagir

unread,
May 28, 2009, 2:40:27 AM5/28/09
to nhusers
Hi,

Is it possible to have lazy loading without generating Proxy classes
at runtime?

Also, guys, maybe you know anything about static proxy?
Ayende did implement some bits a long time ago (poor Man, he was doing
it late in the night) but since than I cannot see anybody using it.

I don't even mind to write additional code in getter/setter to enable
lazy loading (like LightSpeed requires), but not sure if NH can pick
that.

Thanks,
Dmitriy.

Dario Quintana

unread,
May 28, 2009, 10:38:21 AM5/28/09
to nhu...@googlegroups.com

Dmitiry Nagirnyak

unread,
May 28, 2009, 8:33:46 PM5/28/09
to nhu...@googlegroups.com
Thanks. It's a good option.
But what I really don't like with proxies is that I cannot rely on casting operations with them.
 
So I would rather prefer to have classes in my code to support lazy loading.
I suppose I can achive this by writing some additional code in setters/getters.
 
Has anybody done this?
Cheers.
 
2009/5/29 Dario Quintana <cont...@darioquintana.com.ar>

Shane C

unread,
May 28, 2009, 9:25:00 PM5/28/09
to nhusers
Just out of curiosity why would you want this? Is it a concern over
performance or something else?

Fabio Maulo

unread,
May 29, 2009, 1:36:22 AM5/29/09
to nhu...@googlegroups.com
Please, don't follow bad practice even when they come from Microsoft.
Program to interface not to implementation.

Try to explain when you need a cast to a concrete implementation instead an interface.

2009/5/28 Dmitiry Nagirnyak <dna...@gmail.com>



--
Fabio Maulo

Dmitiry Nagirnyak

unread,
May 29, 2009, 5:03:57 AM5/29/09
to nhu...@googlegroups.com
Hi,

(Guys, thanks for the replies. Please excuse me if I'm not correct in
some parts. Just started to learn NH...)

Assumiing following model:
http://nagir.bravehost.com/public/NH/NH-ProxyHierarchy.png
(The gray colored classes are not designed and the consumer of
assemblies should not know about it.)
Please correct me if I didn't draw proxies correctly.

Relying on that model (except the gray parts) the consumer might
assume it is safe to rely on the actual type, but instead he/she can
get totally different results from expected.

(NOTE: I'm not advocating usage of cast operations instead of
interfaces/abstraction, but many do)

For example, I can think about this:
Payment payment = GetOneBillionPayment();
if (payment is CreditCard) {
CreditCard cc = (CreditCard) payment;
// do something with cc, which will never be executed even if the
payment actually IS CreditCard, because of it's "proxied"
}

So we have to do this:
Payment payment = GetOneBillionPayment();
CreditCard cc = session.Load(typeof(CreditCard), payment.Id);


This is not very clear.
But I assume I miss something around proxies...

Cheers,
Dmitriy.

2009/5/29 Fabio Maulo <fabio...@gmail.com>

Dmitiry Nagirnyak

unread,
May 29, 2009, 5:23:39 AM5/29/09
to nhu...@googlegroups.com
This is what I ideally would like to achieve:
"Use the a code-injection mechanism to inject code in assemblies
containing persistent classes to support lazzy loading"

http://www.telerik.com/community/forums/orm/general-discussions/lazy-loading-change-tracking-with-no-reflection-stateful-navigation.aspx#837819

Probably possible with PostSharp, but I'm not too sure.

Fabio Maulo

unread,
May 29, 2009, 8:46:09 AM5/29/09
to nhu...@googlegroups.com
with PostSharp and Cecil.
But a lot of work to do and everybody should use Postsharp.
payment = loadedPayment as ICreditCard;
if (payment != null)
{
.....
}

In the mapping you can use the interface as proxy.
Another option is disable lazy loading (lazy="false") for that specific class.

Another is
var cc = session.Get<CreditCard>(payment.Id);

Obviously I prefer
Program to interface not to implementation

2009/5/29 Dmitiry Nagirnyak <dna...@gmail.com>



--
Fabio Maulo

Dario Quintana

unread,
May 29, 2009, 9:35:41 AM5/29/09
to nhu...@googlegroups.com
Nowadays, I was looking the possibility to use Linfu2.2, and also I was talking close to Philip Laureano about the new stuff in 2.2 version.

The think is, LinFu 2.2 is using Cecil to modify assemblies at runtime, then, modifying (weaving) types in runtime. The problem we are facing is this:

Foo instance = (Foo)Activator.CreateInstance(modifiedType);

We are not being able to cast the modified type into our entity (the original one). The problem relies that in .Net there isn't exists the concept of ClassLoader from Java, so we are not able to instrument the types in runtime. But, now we found this link, so maybe it can help to do the job http://stackoverflow.com/questions/185836/equivalent-of-class-loaders-in-net

Cheers


On Fri, May 29, 2009 at 9:46 AM, Fabio Maulo <fabio...@gmail.com> wrote:
with PostSharp and Cecil.
But a lot of work to do and everybody should use Postsharp.
payment = loadedPayment as ICreditCard;
if (payment != null)
{
.....
}

In the mapping you can use the interface as proxy.
Another option is disable lazy loading (lazy="false") for that specific class.

Another is
var cc = session.Get<CreditCard>(payment.Id);

Obviously I prefer
Program to interface not to implementation


 

Dmitiry Nagirnyak

unread,
May 29, 2009, 12:10:27 PM5/29/09
to nhu...@googlegroups.com
Hi Fabio,

> But a lot of work to do and everybody should use Postsharp.

Well. I would say only those who wants to. Almost the same way as they
should ByteCode.LinFu, Spring or any other.

> payment = loadedPayment as ICreditCard;

How this casting is different from "laodedPayment as CreditCard" or
"laodedPayment is CreditCard"in terms of working with proxy?
Are you saying that we have to write interfaces for all the business
objects (at least those we need lazy loading to be enabled for)?

Cheers.

Fabio Maulo

unread,
May 29, 2009, 12:12:09 PM5/29/09
to nhu...@googlegroups.com
A proxy can be casted to an interface because it implement the interface.

2009/5/29 Dmitiry Nagirnyak <dna...@gmail.com>



--
Fabio Maulo
Reply all
Reply to author
Forward
0 new messages