populating a CSLA object structure in stages

populating a CSLA object structure in stages

Old forum URL: forums.lhotka.net/forums/t/6226.aspx


rjn123 posted on Wednesday, January 21, 2009

I have a CSLA object structure (a root Business object with a hierarchy of children, grandchildren etc.) where some of the required data to populate it is non-changing metadata and some of it is editable data. I have written a separate CSLA readonly object structure that loads the metadata, and this is cached so that it is not retrieved from the database each time. The problem I have is that the main object structure needs to be initialised from the metadata structure, and then the database needs to be called to fill in the editable data. This doesn't seem to fit with the CSLA DataPortal_Fetch call, which expects to load the whole object structure in one go. Ideally I need to be able to create my objects clientside, partially filling them in with the metadata, and only after that call the data portal to obtain the editable data and use it to finish off the population process.

Can anyone suggest a way of getting this to work? (I'm using CSLA 3.0.5)

Thanks,

Richard

rsbaker0 replied on Wednesday, January 21, 2009

Why not cache the metadata "server side", and that way you could fetch the objects in the normal CSLA method via the data portal and completely populate them on the server side?

You could certainly force your fetch method to run on the client, and during it's execution retrieve additional data over the data portal, but it might be less efficient (especially for building collections of objects if you are doing a round-trip per item in the collection).

rjn123 replied on Wednesday, January 21, 2009

> You could certainly force your fetch method to run on the client, and during it's execution retrieve additional data over the data portal

Thanks for your reply. Could I ask you how you see this working? I know I can use RunLocal on the DataPortal_Fetch method, forcing it to run on the client, but having done that, and written the code inside it that loads the metadata, how could I write code at the end of the locally running DataPortal_Fetch that then retrieves additional data over the data portal?

Thanks for any additional help

Richard

RockfordLhotka replied on Wednesday, January 21, 2009

You'd use a second, private read-only object to go get the data. So your object's DP_F would be like this:

[RunLocal]
private void DataPortal_Fetch()
{
  var cache = CachedData.GetData();
  // load fields from cache
  var server = ServerData.GetData(...);
  // load fields from server
}

The ServerData class would be a standard read-only object, but it would be private to your actual business object (so you don't clutter the namespace).

rjn123 replied on Thursday, January 22, 2009

Thanks for the replies. The actual scenario is a bit more complicated than my initial post, where I was trying to pick out the essentials in order to ask a coherent question; but the answers have given me a good steer as to the direction I should be heading in. Thanks again!

Richard

RockfordLhotka replied on Wednesday, January 21, 2009

There are a few possible solutions, depending on your requirements.

  1. You could keep the read-only metadata in memory on the server (simple static field cache) and use that to load the object in its DataPortal_Fetch() method. This would make sense if you always run to the server to load the object before it is used.
  2. You could keep the read-only metadata in memory on the client (which you are doing) and use it to load the object on the client by marking DataPortal_Fetch() as RunLocal. This would make sense if you allow the user or other client code to interact with the object before you (optionally) run to the server to load other data.
    1. You can use a private read-only object to retrieve the server-side data for the object instance. When you are read to retrieve the rest of the object's data, your object would just use this read-only object to go grab the data, and then you'd load it into your object's fields - at which point you have a fully populated object
    2. You might consider NOT merging the metadata directly into your object's fields. Instead, you might consider exposing properties that just delegate to the cached read-only metadata object. This way you don't transport the read-only values to the server when you do a Save()

 

 

Jack replied on Wednesday, January 21, 2009

Why don't you just wrap the read-only object inside the root object and either pass in the rootobject as part of the criteria or just link it after the fact. 

ParentBO pseudo code
{

   StaticDataBO  localStaticBO

   Other properties

   GetParentBO(int ID, StaticDataBO staticdata){}

   DataPortal_Fetch(int ID, StaticDataBO staticData)
  {
       localStaticBO = staticData;
       DoInitStuff ( )
       retrieve data and set properties
   }
}

or use a commandObject to get StaticDataBO when you need it.

any help?

jack

Copyright (c) Marimer LLC