C
Charlie Brown
When filling class data from a relational database, what are some good
ways to minimize the data load to only the data you need? I will
provide the following example to illustrate (simplified):
Class Ingredient
Public Property Ingredents As List(Of Ingredient)
Class Recipe
Public Property RecipeName as String
Public Property Ingredients As List(Of Ingredient)
Class MenuItem
Public Property MenuItemName as String
Public Property MenuRecipe As Recipe
In one form, I might be trying to look at a list of all the recipes in
our database so I would only pull the RecipeName out of the database
since recursively gathering the ingredient list would slow it down
quite a bit. In another form I may need to gather all the recipes and
their main ingredients (without the recursive call to get the
Ingredient.Ingredients() ), at other times I want all of the
information.
How can I design my classes, or data classes in such a way that I
achieve this outcome without adding a ton of overloaded methods and
calls to my database?
One of my thoughts was to place the database call in the property of
the classes as such:
Private _ingredientList as List(Of Ingredient)
Public Property IngredientList As List(Of Ingredient)
Get
If _ingredientList Is Nothing
_ingredientList = GetIngredientsFromDatabase()
End If
Return _ingredientList
End Get
.....
The problem I have so far with this method is that it makes
multithread applications a little more complex than they need to be,
and it forces many calls to the database over and over again.
A better method for loading all the recipes and their ingredients
would be to collect all of the recipes and ingredients from the
database, then loop through the data and "fill" the classes, that
would reduce the database calls from a potential of hundreds down to
2, certainly preferable.
A mix of the two options seems to be a way to go, but I wanted to prod
the minds of the good folks on the usenet for ideas (as a solo
developer, i can only bounce ideas off the wall, which is fun, but
gets me nowhere most of the time)
Thanks in advance,
Charlie
ways to minimize the data load to only the data you need? I will
provide the following example to illustrate (simplified):
Class Ingredient
Public Property Ingredents As List(Of Ingredient)
Class Recipe
Public Property RecipeName as String
Public Property Ingredients As List(Of Ingredient)
Class MenuItem
Public Property MenuItemName as String
Public Property MenuRecipe As Recipe
In one form, I might be trying to look at a list of all the recipes in
our database so I would only pull the RecipeName out of the database
since recursively gathering the ingredient list would slow it down
quite a bit. In another form I may need to gather all the recipes and
their main ingredients (without the recursive call to get the
Ingredient.Ingredients() ), at other times I want all of the
information.
How can I design my classes, or data classes in such a way that I
achieve this outcome without adding a ton of overloaded methods and
calls to my database?
One of my thoughts was to place the database call in the property of
the classes as such:
Private _ingredientList as List(Of Ingredient)
Public Property IngredientList As List(Of Ingredient)
Get
If _ingredientList Is Nothing
_ingredientList = GetIngredientsFromDatabase()
End If
Return _ingredientList
End Get
.....
The problem I have so far with this method is that it makes
multithread applications a little more complex than they need to be,
and it forces many calls to the database over and over again.
A better method for loading all the recipes and their ingredients
would be to collect all of the recipes and ingredients from the
database, then loop through the data and "fill" the classes, that
would reduce the database calls from a potential of hundreds down to
2, certainly preferable.
A mix of the two options seems to be a way to go, but I wanted to prod
the minds of the good folks on the usenet for ideas (as a solo
developer, i can only bounce ideas off the wall, which is fun, but
gets me nowhere most of the time)
Thanks in advance,
Charlie