Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 19th, 2006, 02:10 AM
Registered User
 
Join Date: Sep 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Generic collection population from database

Just wondering whether I am missing something.
In the book "Professional .NET 2.0 Generics" by
Tod Golding , the author asserts for the example below :

"In this example, the goal is to create a general GetItems() method that could retrieve a collection of
objects from a database. This method might return Person, Customer, Employee, or Order objects.
And, with generics, you expect the list returned to be a type-safe collection. The following generic
method achieves these goals:"

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel
using System.Data.OleDb;
public class GetDatabaseItems {
OleDbConnection dbConn = new OleDbConnection("");
public Collection<T> GetItems<T>(String sql) {
OleDbCommand selectCmd = new OleDbCommand(sql, dbConn);
Collection<T> retVal = new Collection<T>();
OleDbDataReader dataReader = selectCmd.ExecuteReader();
while (dataReader.Read() == true)
retVal.Add((T)dataReader[0]);
return retVal;
}
}

As far as I can see it just picks up a value of the single column ( which may be primitive type in Java parlance ) and tries to cast to a class, which doesn't seem to be sensible.

 
Old September 19th, 2006, 02:20 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I don't have the book right here so I can't check, but is it possible that this example is using the new SQL Server 2005 features where actual .NET types are stored in the database? That way, dataReader[0] could truly be a T, like a Customer and the cast would succeed.

From which chapter and page is this example?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old September 19th, 2006, 02:35 AM
Registered User
 
Join Date: Sep 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You might be right , I was not aware of such a feature. Anyway, it is not stressed in a book. As for the place in a book : Chapter 5 "Generic Methods" / page 98
I guess, even for standard types we can pass as parameters a number of columns and types ( to speed it up ) and iterate and populate ArrayList and then somehow instantiate generic class ( Person, Customer ... ) ( e.g. there is a constructor with ArrayList ) and then add to Collection. But how to keep it generic :) don't know.

 
Old September 19th, 2006, 03:26 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I guess if you start passing around collections of columns / properties, things are no longer generic.

You could use reflection to retrieve all the properties of T and then use each property name to access the correct column in the data reader. But, since reflection is slow, you loose some of the benefits that Generics were supposed to bring....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old September 19th, 2006, 04:07 AM
Registered User
 
Join Date: Sep 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ignoring speed issues , I have no problem to understand how reflection may be used if T className is given as another parameter.

public Collection<T> GetItems<T>(String sql , String className)

Type theType = Type.GetType(className);

...

Could you give a hint how syntactically use T itself for reflection purposes.

Type theType = Type.GetType( T );

gives a compile error.

 
Old September 19th, 2006, 04:18 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I think this should work:

Type myType = typeof(T);

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old September 19th, 2006, 05:47 AM
Registered User
 
Join Date: Sep 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes it does. Thanks.
Also inside method instead of
T t = new T()

should be used
T t = default(T)

But reflections still unavoidable since methods are not known to the compiler

t.Fill(arrayList)
doesn't compile

 
Old September 19th, 2006, 05:53 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, I agree.

You could use reflection to get each property of the object and then get its value from the reader, with (pseudo) code like this:

foreach (propertyName in T.Properties)
{
  T.PropertyName = myReader[propertyName];
}

Look at stuff like GetProperty and GetProperies on the Type class.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
database design issue - generic vs performance elysian Oracle 0 March 27th, 2007 04:34 AM
Databinding a Generic Collection to a Datagrid rolandatem ASP.NET 2.0 Professional 0 October 7th, 2006 11:08 PM
Insert database values into a Hashmap collection xaitienle Struts 1 June 19th, 2006 06:04 AM
Datagrid Population jayabhatia .NET Web Services 3 November 6th, 2003 11:10 AM
Datagrid population KennethMungwira ADO.NET 1 November 6th, 2003 11:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.