extra database reads in a business object class

  • Thread starter Thread starter SteveS
  • Start date Start date
S

SteveS

This has been bothering me for a long time, and I'm curious about everyone's
opinion or a "Microsoft Best Practices" method of doing this. I have a
"Member" business object which contains information about an individual
member. For instance, it contains the following properties:

member.name = "SteveS"
member.LoginName = "SteveS"
member.Phone = '555-555-1234'
member.Office = ...object of office information such as (office.address1,
office.name, office.mailtype, etc...)
member.Committees = ....contains a collection of the Committee objects
(Committee.Name, Committee.Type, Committee.MeetingPlace, etc...)

So, every time I instantiate the member object, I populate the .Office and
..Committes objects. Basically, in this example, the database would take 3
hits - one to read the member record, one to read the office record, and
one to read the committee records.

THE PROBLEM: This works fine, except there are 2 extra database hits when
I only need the LoginName property. ***I hate to call the database again if
I don't have to****

THE SOLUTION: ???? What do you think is the best answer???? Once answer
is to read all the data in a sql statement, then use that to populate each
object. That doesn't seem to be the cleanest way to me. (but I could be
wrong). Here's what I did:


Public Class Member

private _name as string
private _MemberNumber as Long
private _LoginName as string
private _Phone as string
private_Office as Business.Office
private _Committees as Business.CommitteCollection()

Public Property Name as String
....
End Property
Public Property LoginName as String
....
End Property
Public ReadOnly Property as Business.Office
Get
If IsNothing(_office) then '<======= populate the office object
if not already
_Office = New Office(_OfficeNumber)
End If
End Get
End Property
Public ReadOnly Committees Property as Business as
Business.CommitteeCollection()
Get
If IsNothing(_Committees) Then '<=======populate the
CommitteCollection if not already
...code to populate the CommitteCollection.....
End If
return _Committees
End Get
End Property
.....
.....
End Class


Thanks for taking the time to think about this :-)

SteveS
 
I've used this approach in the past as well - i.e. load collections as they
are requested.

One variation I have also used on this approach is to include some type of
optional "level" parameter in the constructor. So, if I know I plan on
using various collections, I can pass in a level of, say "1", when I
instantiate the object and it will know to go ahead and load some of the
collections (rather than wait until I access them) since I know they will be
accessed.
 
Back
Top