objectdatasource

G

germ

I was playing with some mutable/immutable stuff and came across something
that is not what I expected.
Given the classes and the ObjectDataSource below, I would have expected an
error due to readonly properties when attempting an update using a databound
control with using this ods
But this isn't the case - the ODS seems to be finding the base class
property definitions rather than those of the actual class that is being
used.
If I change the names of the base class properties or drop the base class
altogether then I get the expected behaviour.

I had expected to have 2 sets of crud methods on the objManager - general
use using immutableObj and ODS specific using mutableObj. It looks like the
mutable versions aren't required.

I'm not sure if this is a good thing or not.

Is there anyway to get the ODS to respect the class definition as specified
by DataObjectTypeName ?

I read an article stating that objects that are completely readonly are
handled more efficiently but the runtime. Does this apply to the
immutableObj class ? Strictly speaking it is 100% readonly but its base
class isn't and it also isn't directly accessible as all properties are
new'd in the derived class. My guess is that this is not considered a
readonly class.

Gerry


// mutable base - ObjectDataSource finds these properties which are settable
public class mutableObj
{
static int _id = 0;
public int ID { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public mutableObj()
{
ID = ++_id;
Name = string.Format( "Obj {0} Name" , ID );
Title = string.Format( "Obj {0} Title" , ID );
}
}

// in general we want these objects to be immutable
public class immutableObj : mutableObj
{
new public int ID { get { return base.ID; } }
new public string Name { get { return base.Name; } }
new public string Title { get { return base.Title; } }
public immutableObj ()
:base()
{
}
public immutableObj ( int ID , string Name , string Title )
{
base.ID = ID;
base.Name = Name;
base.Title = Title;
}
}


public static class objManager
{
private static immutableObj [] Objects= {
new immutableObj () ,
new immutableObj () ,
new immutableObj () ,
new immutableObj () ,
};
public static immutableObj [] Select() { return Objects; }
public static void Update( immutableObj dat ) {}
}



<asp:ObjectDataSource runat="server"
ID="ObjODS"
TypeName="objManager"
SelectMethod="Select"
UpdateMethod="Update"
DataObjectTypeName="immutableObj "
/>
 
S

Shengqing Yang [MSFT]

Hi Gerry,

Based on my understanding, you did a test on the ObjectDataSource you
provided and was surprise to find it's update worked well as you expected
it may have some problem since the properties is read-only.

Actually, I am not quite understand the meaning of this test as although
you used new key word to hide the base properties, the properties of the
child class still return values from base class by the get statements.

Since you did not provide the update method in your code, I simply write
this code for a further test.

public static void Update(immutableObj dat) { Objects[0] = dat; }

And here is the code of the ASP.NET page.

<asp:GridView ID="GridView1" runat="server" DataSourceID="objODS">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="objODS" runat="server" TypeName="objManager"
SelectMethod="Select"
UpdateMethod="Update"
DataObjectTypeName="immutableObj"></asp:ObjectDataSource>

When we update the first record in the GridView, we can find the ID value
has been changed. This is because parameter dat is set to Objects[0] as a
completed package and it will not use set statement in this process. So it
is no problem to change the value of the class although the properties are
read-only.

I hope the explanation above can solve your puzzle of how the update method
works. And if I have misunderstood you, please feel free to let me know.

--
Sincerely,
Bravo Yang
Microsoft Online Support

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.

The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx

==================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top