Comply to ObjectDataSource--Object creation

Published 27 April 06 04:01 PM | despos

This should not be something new to great ASP.NET devs; however, just in case :)

ObjectDataSource is designed to work with classes in the business layer of the application. An instance of the business class is created (via reflection) for each and every operation performed and destroyed shortly after the operation is complete.
In case of business objects particularly expensive to initialize, you can resort to static methods or you can implement a custom caching or pooling mechanism.
Instances of the business object are not automatically cached or pooled. Both options, though, can be manually implemented by properly handling the ObjectCreating and ObjectDisposing events on an ObjectDataSource control. The ObjectCreating event fires when the data source control needs to get an instance of the business class. You can write the handler to retrieve an existing instance of the class and return that to the data source control.

public void OnObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
     BusinessObject bo = RetrieveBusinessObjectFromPool();
     if (bo == null)
        bo = new BusinessObject();
     e.ObjectInstance = bo;
}

Likewise, in ObjectDisposing you store the instance again and cancel the disposing operation being executed.

public void OnObjectDisposing(object sender, ObjectDataSourceDisposingEventArgs e)
{
     ReturnBusinessObjectToPool(e.ObjectInstance);
     e.Cancel = true;
}

From the ObjectDataSource perspective, it makes no difference. Given the internal implementation, you perhaps save a few IL operations if you have static methods. However, having static methods on the BLL/DAL may pose issues at another level.

What if you test a business class that calls the DAL internally and the DAL fails. Can you figure out what really happened? Does the business class work or not? To effectively test a business layer that calls into a DAL you need to focus on the object under test. Mock objects come to the rescue. Mock objects are programmable, polymorphic objects that present themselves as others and can wrap DAL anomalies and signal them out clearly making the test succeed if nothing else happens. The point is that some mocking toolkits typically don’t like static methods and don't offer proper support.

Comments

# Marc Brooks said on April 27, 2006 11:10 AM:

How about something slightly more realistic, like needing the key values to lookup the entry?

# Marc Brooks said on April 27, 2006 12:08 PM:

Just to note, if your access methods (Select,Update,Delete,Insert) are declared in the business object as static, the OnObjectCreating and OnObjectDisposing methods are never called.

# DinoE said on April 27, 2006 12:22 PM:

Hi Marc
yes you are totally correct regarding the static methods. As for the slightly more realistic example, I'm not sure about what you mean precisely. However, my guess is that you're saying to create a sort of personal pool of objects and asking about ways to identify objects?

I think that a relatively simple approach to use is storing live ObjectDataSource objects in the Cache each associated with a GUID and keep the list of GUIDs public somewhere. You might want to build a sort of pooler class for this. When ObjectCreating fires, you run code that scans all the known GUIDs in the Cache to find a valid object and waits until it gets one.

There are quite a few more details to discuss, but this seems to be an idea

# robertro said on April 29, 2006 07:59 PM:

I would have really liked the ObjectDataSource (ODS) to have implemented OnCreating slightly differently. Specifically, I'd like to declare an ODS without specifying a typename property. Then within the OnCreating event assign it (I know I could do this within the page load event etc). However, I'd like to stick all related code within the scope of the control (ODS).

If you look at how MS implemented the whole creation process (using Reflector) you'll notice that before the OnCreating is fired the ODS has already retrieved the Type of the object defined within the typename property. Therefore you can't return a completely different object type within OnCreating (if you auto generate columns the typename would have been meaningless as it would calculate them based on the returned instance)

So here's how I would have liked the OnCreating and Creation process to occur. The OnCreating is fired - If I don't return an object instance use the typename property value then retrieve the type of that object. Secondly, if I do return an object ignore the typename property and retrieve the Type of the returned instance.

I sure know it would make my life a whole lot easier. If the above happens for a reason I'd love to know. The DataObjectTypeName creation could have also been greatly improved but that's another discussion.

# Xanax said on May 15, 2006 02:37 PM:

Nice site. Further, if there is movement, there is also something moved,and everything is moved out of something and into something; itfollows that that that which is moved must first be in that out ofwhich it is to be moved, and then not be in it, and move into theother and come to be in it, and that the contradictory statementsare not true at the same time, as these thinkers assert they are.

# xanax said on May 19, 2006 12:43 PM:

Hi. For it assumes thathe is false who can deceive (i.e.

Leave a Comment

(required) 
(required) 
(optional)
(required)