Why is GetIdValue() Protected instead of Public?

Why is GetIdValue() Protected instead of Public?

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


david.wendelken posted on Thursday, October 11, 2007

I'm confused about this!  Why is GetIdValue() defined as protected instead of public?

Isn't its purpose to allow the object to identify itself? 

It seems like a catch-22 sort of thing:

"My job is to identify myself, but you aren't allowed to ask me."

JonStonecash replied on Thursday, October 11, 2007

My understanding is that this method overrides a method in the base class.  The overridened logic returns a value that allows the base class to properly handle the equals and get hash code methods.  Otherwise you would have to write overrides for those two methods. 

My guess is that the scope of protected is not all that critical.  Also, the caller of a specific class probably already knows which property is the primay key.


Jon Stonecash

ajj3085 replied on Thursday, October 11, 2007

I think it might be important actually.  The value used by GetIdValue is only there to ensure the object compares correctly, and to ensure uniquenes if its in a collection.  Its not something that necessarly represents a database id; it could be a temporary construct. 

If GetIdValue were public, you would have a harder time changing exactly how you determine if an object is unique or not down the line.  With the protected approach, you could do that without having to worry about breaking any clients.

david.wendelken replied on Thursday, October 11, 2007

ajj3085:
I think it might be important actually.  The value used by GetIdValue is only there to ensure the object compares correctly, and to ensure uniquenes if its in a collection.  Its not something that necessarly represents a database id; it could be a temporary construct. 

If GetIdValue were public, you would have a harder time changing exactly how you determine if an object is unique or not down the line.  With the protected approach, you could do that without having to worry about breaking any clients.

Are you sure about that?  It seems exactly backwards to me!  :)

GetIdValue returns an object that uniquely identifies the business object.

If GetIdValue were public, one could take a given instantiated business object and ask if it were equal to another instantiated business object - and the code would always work.

You could argue that if you had some field values and wanted to compare, you would have to know how to cast the returned object.  But you need to know what fields to use anyway, so how does that make it better?

Mind you, I'm not saying you are wrong!  Just that I don't understand why you are right! :)

ajj3085 replied on Thursday, October 11, 2007

david.wendelken:
Are you sure about that?  It seems exactly backwards to me!  :)


Yes, I am! Wink [;)]

david.wendelken:
GetIdValue returns an object that uniquely identifies the business object.

If GetIdValue were public, one could take a given instantiated business object and ask if it were equal to another instantiated business object - and the code would always work.

But we already can do that.  Use the Equals method of the business object.  Rocky provides that functionality in Csla already for us.

david.wendelken:
You could argue that if you had some field values and wanted to compare, you would have to know how to cast the returned object.  But you need to know what fields to use anyway, so how does that make it better?

So you're talking about two BOs which are of different types then?  Usually different types means right away they won't be equal.  In the other cases, I would think in your business layer you would create an object which implements IEqualityComparer, and that would be used to compare your differing objects.  If two BOs are equal sounds like a business rule.. so it should be there.

That's my 2 cents, anyway Smile [:)]

Andy

david.wendelken replied on Thursday, October 11, 2007

Thanks!  I think I got it!

Copyright (c) Marimer LLC