Click here to Skip to main content
15,906,816 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
AnswerRe: Creating sequential numbering in a semi-connected environment Pin
User 58385228-Aug-07 20:27
User 58385228-Aug-07 20:27 
QuestionODP.Net Pin
miniThomas17-Aug-07 0:54
miniThomas17-Aug-07 0:54 
AnswerRe: ODP.Net Pin
Paul Conrad4-Nov-07 7:07
professionalPaul Conrad4-Nov-07 7:07 
GeneralUsing RDL to Define Flat File Exports Pin
Brady Kelly16-Aug-07 4:05
Brady Kelly16-Aug-07 4:05 
QuestionPassing data between layers Pin
vagl16-Aug-07 3:40
vagl16-Aug-07 3:40 
AnswerRe: Passing data between layers Pin
John-ph26-Aug-07 3:00
John-ph26-Aug-07 3:00 
GeneralRe: Passing data between layers Pin
Mark Churchill26-Aug-07 18:12
Mark Churchill26-Aug-07 18:12 
QuestionPopulating Objects from Db Pin
Burnsys215-Aug-07 17:47
Burnsys215-Aug-07 17:47 
Hi
What do you recomend to populate custom objects from the database?
Let's say i have a "Product" object, and i have a Products table, each product has a "name", "Id","Stock","Type" etc.

Some times i need to get a couple of products while other times i need a large list of them,

So i build a Singleton "Products" class that has a "GetProduct(id)" method and a private Dictionary of products, each time a product is requested it first check if the product is in the dictionary, and if its not it calls a New Product(id) and add it to the dictionary.

Class Products
Public Shared Function GetProduct(id as integer) as product
if Cache.containskey(id) then return Cache(id)
return new product(id)
end function
End class


Then i have a constructor in the "product" Class that looks like this:


Sub New(id as integer)
Dim Comm as new sqlcommand("Select * from products where id=@id",ConnString)
comm.parameters.addwithvalue("@id",id)
Dim Reader as sqldatareader=comm.executereader

reader.read
idvalue=reader("Id")
Namevalue=reader("Name")

end sub

The problem i think is that when i need to get a large number of products i am making a lot of querys to the database, so i thougt of doing somenthing like this:

Class Product
Private Sub PopulateFromReader(r as datareader)
idvalue=reader("Id")
Namevalue=reader("Name")
end sub
Sub New(r as datareader)
me.PopulateFromReader(r)
end sub
Sub New(id as integer)
Dim Comm as new sqlcommand("Select * from products where id=@id",ConnString)
comm.parameters.addwithvalue("@id",id)
Dim Reader as sqldatareader=comm.executereader

me.PopulateFromReader(r)

end sub
End class

And then in the private constructor of the Singleton "Products" class i do somenthing like this:

Class Products
Private Cache as dictionary(of Integer,Product)
Private Sub New
Dim Comm as new sqlcommand("Select * from products",ConnString)
Dim Reader as sqldatareader=comm.executereader
do while reader.read
Cache.add(reader("id"),new product(reader)
loop
end sub
end class

So, what do you think i should do? is it ok? is there a "Correct" way to do it?

Thanks in advance

PS: Sorry my english!
AnswerRe: Populating Objects from Db Pin
Mark Churchill16-Aug-07 22:02
Mark Churchill16-Aug-07 22:02 
GeneralRe: Populating Objects from Db Pin
Ed.Poore17-Aug-07 3:37
Ed.Poore17-Aug-07 3:37 
QuestionDesign Pattern Seminar in Europe Pin
Spithas8-Aug-07 2:59
Spithas8-Aug-07 2:59 
AnswerRe: Design Pattern Seminar in Europe Pin
Colin Angus Mackay14-Aug-07 5:01
Colin Angus Mackay14-Aug-07 5:01 
GeneralRe: Design Pattern Seminar in Europe Pin
Pete O'Hanlon14-Aug-07 9:43
mvePete O'Hanlon14-Aug-07 9:43 
GeneralRe: Design Pattern Seminar in Europe Pin
Spithas17-Aug-07 21:47
Spithas17-Aug-07 21:47 
GeneralRe: Design Pattern Seminar in Europe Pin
martin_hughes20-Aug-07 5:50
martin_hughes20-Aug-07 5:50 
AnswerRe: Design Pattern Seminar in Europe Pin
t!bast30-Aug-07 3:22
t!bast30-Aug-07 3:22 
GeneralKeyboard Wedge Barcode Scanner Pin
Brady Kelly7-Aug-07 23:32
Brady Kelly7-Aug-07 23:32 
QuestionChange CSS according to Browser? Pin
chand107-Aug-07 21:30
chand107-Aug-07 21:30 
AnswerRe: Change CSS according to Browser? Pin
Pete O'Hanlon7-Aug-07 21:54
mvePete O'Hanlon7-Aug-07 21:54 
QuestionRefactoring Properties into Classes Pin
Leslie Sanford7-Aug-07 6:52
Leslie Sanford7-Aug-07 6:52 
AnswerRe: Refactoring Properties into Classes Pin
Luc Pattyn7-Aug-07 7:43
sitebuilderLuc Pattyn7-Aug-07 7:43 
GeneralRe: Refactoring Properties into Classes Pin
Leslie Sanford7-Aug-07 8:00
Leslie Sanford7-Aug-07 8:00 
GeneralRe: Refactoring Properties into Classes Pin
Luc Pattyn7-Aug-07 14:34
sitebuilderLuc Pattyn7-Aug-07 14:34 
AnswerRe: Refactoring Properties into Classes Pin
Pete O'Hanlon7-Aug-07 22:00
mvePete O'Hanlon7-Aug-07 22:00 
GeneralRe: Refactoring Properties into Classes Pin
Heavy Storm15-Aug-07 8:11
Heavy Storm15-Aug-07 8:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.