Click here to Skip to main content
15,850,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I would like to know which is the best way of generating List of entity objects?
I am using following layers to fetch the data
presentation Layer->Service Layer->DAL (& Entity layer)

Way1:

get dataset from dqladpter.Fill()
apply linq to convert each dataTable row into a entity object
and convert into a List<entityclass>

Way2:

C#
while(sqldataReader.Read())
{
EntityClass ec = new EntityClass();
ec.Name = sqldataReader["Name"];
.
.
.
ListClass.Add(ec);
}

both way return the same results but i need which is the best way and why?
My thought is, Way2 might be best because it reduce the dataset memory size
Please clarify...
Regards,
--SJ
Posted
Updated 22-May-13 20:03pm
v3
Comments
Balimusi 22-May-13 13:54pm    
This is entirely depending on your need and situation. If your purpose is solely returning data to your application, DataReader will be a good choice. DataReader is a forward-only read; it's fast.

However, there is situation where you need to use DataSet; for instance, if you need to transform the result-set into XML and to be sent over the wire via HTTP, where a DataReader cannot be serialized and cannot be passed between physical-tier boundaries.

1 solution

You can try similar to this
First of all add similar method in inside Entity Class
C#
public EntityClass Fill(IDataReader row)
{
   return new EntityClass(row);
}


//you can write the helper method in business layer to convert to generic list
private GenericList<t> ConvertTo<t>(IDataReader reader)
                        where T : EntityClass, new()
        {
            GenericList<t> temp = new GenericList<t>();
            try
            {
                while (reader.Read())
                {
                    temp.Add((new T().Fill(reader)) as T);
                }
                return temp;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                reader.Close();
            }
        }
</t></t></t></t>


then you can invoke the business layer method from UI inturn invoke data access layer. DAL will return the data reader instance. This instance you can pass to ConverTo method to get the generic list. Hope this will give an hint
Thanks
Nilesh
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900