views:

451

answers:

2

I have a DTO which can be fully loaded or lazy loaded using Lazy Load Pattern. How it is loaded depends on what the Flex Application needs. However, this DTO will be sent to a Flex application (swf). Normally, a collection for instance, will only be loaded when called. In my case however, the collection will only be called in Flex, so my implementation on the .NET side will obviously not work in this case (except if Flex would do a server call... something I would like to avoid).

In the getter of the collection, the data is retrieved from the database. If I would be working with ASP.NET pages, it would work, but not if the DTO is sent to Flex.

How would you deal with this? I could call the getter before sending the DTO to Flex, but that seems awful... + calling the getter can only be done if it is assigned to something (and the local variable that will hold the collection will never be used...).

+1  A: 

I would probably introduce a Finalize method for the class and perhaps a FinalizeAll extension method for various collections of the class. This method would simply go through and reference all the getters on the public properties of the class to ensure that they are loaded. You would invoke Finalize (or FinalizeAll) before sending the object(s) to your Flex app. You might even want to make this an interface so that you can test for the need for finalization before transfering your objects and invoke the method based on a test for the interface rather than checking for each class individually.

NOTE: Finalize is just the first name that popped into mind. There may be (probably is) a better name for this.

tvanfosson
EagerLoad or Load was my idea of the name of the function. We came to the same conclusion here that a method would do well.
Lieven Cardoen
Is there a way to call the getter in this Finalize method without having to asign it to a variable?
Lieven Cardoen
I don't think so without using reflection. You could, however, refactor to have the getter call a method that sets the property then have the Finalize method call this same method. I'd probably refactor this to use reflection as well so that it could be used by all of your properties.
tvanfosson
Will have to check out reflection in .NET. Have always programmed ActionScript in Flash and Flex and used Reflection there with spring.actionscript. Do you have links, resources to get me started with reflection. How would you refactor this to use reflection?
Lieven Cardoen
That's a big question, especially without code. The basic idea is that you use names (strings) to tell the method what to set and where and how to the value to use. You use reflection on the objects to get the appropriate fields, then get/set their values.
tvanfosson
Relection namespace: http://msdn.microsoft.com/en-us/library/system.reflection.aspx. You're probably most interested in FieldInfo, MethodInfo, and PropertyInfo, depending on your code.
tvanfosson
+2  A: 

You can introduce a method to load dependents - loadDependencies - that should take of all lazy loading for your DTO object before being sent over the wire (to Flex). You can abstract this method to an interface to streamline such process across different DTOs. There is nothing against using getters the way you described it inside this method.

grigory