Design: why using id in place of a reference to an object

Design: why using id in place of a reference to an object

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


alef posted on Tuesday, February 26, 2008

In the ProjectResource class you have a property Role. This is the property Id of the object Role.

Why you don’t use a reference to the object Role.

Suppose the GUI is changing the role with the combobox. Now the class ProjectResource knows only about the id of role, and not for example the name of the role.

alef replied on Wednesday, February 27, 2008

Has somebody any idea?

The purpose I'm asking this, is for implementing the ErrorTreeView control from codeplex. I do not want to see id's, I want to override ToString and show something meaningfull to the user and not showing id's.

alef replied on Thursday, February 28, 2008

To explain the need in my case. I've the following objects:

Model: Editable Root Parent with a property Name(e.g. Shoe123)

ModelColors: EditableChildList

ModelColor: EditableChild with properties Id, ColorId, Quantity

Colors: EditableRoot List

Color: EditableSwitchable

 

So in the GUI we can create/update a model, we can change the name of the model,

and in a grid we can add all the possible colors in which this model is manufactured.

 

Now I was playing with the ErrorTreeView control from CSLAContrib to show all the business roles which are broken.

So I want to see something like:

   Model : Shoe12

      -Colors

            -Color: Brown

-         Quantity can not be negative

            -Color: Yellow

-         Quantity can not be more then 100

 

And not something with an id for the color

   Model : Shoe12

      -Colors

            -Color: 45

-         Quantity can not be negative

            -Color: 67

-         Quantity can not be more then 100

 

So in the Business Object ModelColor I need to override the ToString method to show the name of the color. But in ModelColor I do not have the name of the Color, I only have ColorId.

So how can I solve this? Not using ColorId in the business object ModelColor but storing a reference to the object Color? I don’t want to use code like Kleur.GetKleur(_colorId) because this invokes a roundtrip to the database.

Rocky, can you help me with this?

 

malloc1024 replied on Friday, February 29, 2008

I don’t see anything wrong with storing a color object in ModelColor. Another option would be to add a property called ColorName inside ModelColor. The ColorName property would send the ColorID to the Colors object and retrieve the corresponding color name. You would want to cache the Colors collections so you can avoid calling the db every time you look up the ColorName. Turing the Colors class into a singleton would accomplish this.

Copyright (c) Marimer LLC