Re: One to many relationship in OOP
- From: "Chris Dunaway" <dunawayc@xxxxxxxxx>
- Date: 14 Nov 2006 07:47:16 -0800
On Nov 14, 5:34 am, "Nemisis" <darrens2...@xxxxxxxxxxx> wrote:
If i make an object, called Object1, i have a property called
Object1.ChildId, and another property called Object1.HasChild, which
checks the Id value and says whether it is linked or not.
Should i have a property called Object1.ChildObject, which would load
the data of the child object? I dont need to object all the time, and
thus i am wondering whether i should just load this child object into a
variable when i need it rather then everytime i load the object??
We generally design our objects like your second method above. We have
the "parent" object and as a property we have the child object. Then
when retrieving the data from the database, he have a boolean parameter
that indicates whether we should populate all child object or not.
For example, we might have a class called Parent which has a Child
object. Our data layer might have a method called GetParentObject and
we pass in a bool to indicate if we want the child object retrieved
from the db as well, depending on the circumstance. This code is
pseudo-code and can probably be refined, but it should illustrate the
point.
class Parent
{
public Parent()
{
//Create an empty object here
_child = New Child();
}
private Child _child;
public Child Child
{
get;
set;
}
}
public class DataLayer
{
//This method takes the parentid and a bool to indicate if we get
the child object.
public Parent GetParentObject(int parentid, bool deep)
{
//Code to get parent object here
_child.Id = ....; //assign child id here.
if (deep)
FillChildObject();
}
public void FillChildObject()
{
//get child data here using child id in the _child instance.
}
}
.
- Follow-Ups:
- Re: One to many relationship in OOP
- From: Nemisis
- Re: One to many relationship in OOP
- References:
- One to many relationship in OOP
- From: Nemisis
- One to many relationship in OOP
- Prev by Date: Re: vb.net v. vsto
- Next by Date: Re: One to many relationship in OOP
- Previous by thread: One to many relationship in OOP
- Next by thread: Re: One to many relationship in OOP
- Index(es):
Relevant Pages
|