Should class properties be values or references to objects?
Should class properties be values or references to objects?
Here's my situation: Reseller Has Customers Has Services
In my app, the relationship between objects rarely changes. So once a customer is setup for a given reseller, he will pretty much always have that association.
I frequently do relational type operations such as getting the name of a reseller, when given only the customer or service.
I would like to keep database hits to a minimum.
#1. customer class should contain a reference object for the reseller
VB Code:
Public Class Customer
public CustID as integer '(readonly)
public ResellerID as integer
public Services as ServiceCollection
End Class
'To get the reseller's name from a Customer object
dim theCustomer as new Customer(1)
dim theReseller as new Reseller(theCustomer.ResellerID)
strResellerName = theReseller.Name
or #2. the resellers' key so that it can be retrieved if needed.
VB Code:
Public Class Customer
public CustID as integer '(readonly)
public Reseller as Reseller
public Services as ServiceCollection
End Class
'To get the reseller's name from a Customer object
dim theCustomer as new Customer(1)
strResellerName = theCustomer.Reseller.Name
Right now I'm doing #1. It has benefits such as being able to the the reseller's name with statements like this: strName = theCustomer.Reseller.CompanyName. Unfortunately this is really starting to be a pain in the butt, since I have to set those references all the time. But at the same time, I'm only instantiating the Reseller object one time so there is only one database hit.
Thanks for any opinions,
Jamison
Re: Should class properties be values or references to objects?
Bah, writing this pretty much answered my question. Maintaining all the object references is just not elegant at all.
I started down this road because originally I was using an object manager that would keep track of what had been instantiated in the given application. I dropped the manager for other reasons, but somehow idea #2 stuck around.
J
Re: Should class properties be values or references to objects?
Just out of curiosity, what are you going with then?
Because I don't like option 2.
The Customer should not contain the Reseller object. The Reseller is the Parent object. Using option 2 would make it possible to do this:
theCustomer.Reseller.Customers(3).Reseller.Customers(1).Reseller.Customers(5)
But if you are doing that, you should have a proper variable theReseller to begin with anyways.
Just my two centavos.
-tg
Re: Should class properties be values or references to objects?
Hey TG,
You are right on both accounts. Circular refernces can easily happen. And I generally have the proper parent object available.
The main appeal of #2 was being able to get reseller properties with only a service object.
I've pretty much talked myself out of #2. I'm finding only a couple objects where it makes sense.
Jamie