Click here to Skip to main content
14,302,372 members
   

Database

 
GeneralRe: looping in SQL Server2005 Pin
Tobias Schoenig20-Mar-08 0:26
memberTobias Schoenig20-Mar-08 0:26 
Questionhow to change the date format Pin
mon***z19-Mar-08 19:08
membermon***z19-Mar-08 19:08 
AnswerRe: how to change the date format Pin
N a v a n e e t h19-Mar-08 20:46
memberN a v a n e e t h19-Mar-08 20:46 
GeneralRe: how to change the date format Pin
Vimalsoft(Pty) Ltd19-Mar-08 21:27
professionalVimalsoft(Pty) Ltd19-Mar-08 21:27 
AnswerRe: how to change the date format Pin
Ashfield19-Mar-08 22:57
memberAshfield19-Mar-08 22:57 
Generalmultiplying columns Pin
neoghy19-Mar-08 18:46
memberneoghy19-Mar-08 18:46 
GeneralRe: multiplying columns Pin
N a v a n e e t h19-Mar-08 20:53
memberN a v a n e e t h19-Mar-08 20:53 
GeneralGeneric data fetch with IDataReader Pin
RubensFarias19-Mar-08 16:38
memberRubensFarias19-Mar-08 16:38 
Hi there,

I'm looking for some advise for a generic data fetch pattern for IDataReader I'm working on, as follow:

// main load
using(GenericDataReader reader = new GenericDataReader(cm.ExecuteReader()))
{
while (reader.Read())
{
orders.Add(reader.Get<Order>()); // "GetList" pattern
//return new Order(reader); // "Get" pattern
}
}

// generic reader; interface implementation removed
public class GenericDataReader : IDataReader
{
// ... \\

public T Get<T>(string name)
{
int i = _dataReader.GetOrdinal(name);
if (_dataReader.IsDBNull(i))
return default(T);
else
return (T)_dataReader.GetValue(i);
}

public T Get<T>() where T : ILoadable, new()
{
T entity = new T();
entity.Load(this, false);
return entity;
}
}

// All business objects must implements this interface
public interface ILoadable
{
void Load(GenericDataReader reader, bool deep);
}

// Sample business object
class Order : ILoadable
{
int id;
Customer customer; // Note
DateTime orderDate;
DateTime requiredDate;
DateTime? shippedDate;
List<OrderDetail> orderDetails;

public Order(){ }

public Order(GenericDataReader reader)
{
Load(reader, true);
}

#region ILoadable Members

public void Load(GenericDataReader reader, bool deep)
{
id = reader.Get<int>("OrderID");
customer = reader.Get<Customer>(); // Note
orderDate = reader.Get<DateTime>("OrderDate");
requiredDate = reader.Get<DateTime>("OrderRequiredDate");
shippedDate = reader.Get<DateTime?>("OrderShippedDate");
if (deep && reader.NextResult())
{
orderDetails = new List<OrderDetail>();
while (reader.Read())
{
orderDetails.Add(reader.Get<OrderDetail>());
}
}
}

#endregion
}

What do you think? Are there any major drawback with this model I'm missing? Are there any more clever way to do that?

I really appreciate your time reading this. Thanks in advance,

Rubens
GeneralRe: Generic data fetch with IDataReader Pin
Syed Mehroz Alam20-Mar-08 20:17
memberSyed Mehroz Alam20-Mar-08 20:17 
GeneralRe: Generic data fetch with IDataReader Pin
RubensFarias23-Mar-08 12:54
memberRubensFarias23-Mar-08 12:54 
GeneralAdd new columns in the middle of a table Pin
brhnz19-Mar-08 15:22
memberbrhnz19-Mar-08 15:22 
GeneralSelect statement Using the like keyword Pin
Mr Oizo19-Mar-08 4:33
memberMr Oizo19-Mar-08 4:33 
GeneralRe: Select statement Using the like keyword Pin
A Wong19-Mar-08 5:27
memberA Wong19-Mar-08 5:27 
QuestionHow to combine the following Pin
tonyong19-Mar-08 2:37
membertonyong19-Mar-08 2:37 
GeneralA domain error occurred in SQL Pin
Eunice (VB junior)18-Mar-08 23:16
memberEunice (VB junior)18-Mar-08 23:16 
GeneralRe: A domain error occurred in SQL Pin
Krish - KP19-Mar-08 1:49
memberKrish - KP19-Mar-08 1:49 
GeneralRe: A domain error occurred in SQL Pin
Eunice (VB junior)19-Mar-08 16:10
memberEunice (VB junior)19-Mar-08 16:10 
Questionhow to store date in ddmmyy format in sql server 2005 Pin
vishal parekh18-Mar-08 22:49
membervishal parekh18-Mar-08 22:49 
AnswerRe: how to store date in ddmmyy format in sql server 2005 Pin
N a v a n e e t h19-Mar-08 0:01
memberN a v a n e e t h19-Mar-08 0:01 
AnswerRe: how to store date in ddmmyy format in sql server 2005 Pin
Ashfield19-Mar-08 2:49
memberAshfield19-Mar-08 2:49 
Generalretrieve current week's data Pin
SeeSharp18-Mar-08 20:07
memberSeeSharp18-Mar-08 20:07 
GeneralRe: retrieve current week's data Pin
Krish - KP19-Mar-08 0:19
memberKrish - KP19-Mar-08 0:19 
GeneralRe: retrieve current week's data Pin
SeeSharp20-Mar-08 0:09
memberSeeSharp20-Mar-08 0:09 
GeneralDouble Click options in SQL 2005 editor Pin
pmpdesign18-Mar-08 12:43
memberpmpdesign18-Mar-08 12:43 
GeneralReporting services--Problem with the subreport Pin
Payal_e_m18-Mar-08 10:50
memberPayal_e_m18-Mar-08 10:50 

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.