简体   繁体   中英

C# Populating object from database

Currently our DAL (Data Access Layer) queries the database and we loop through the resultant data to populate an object (one object per row) in which the final result is a List.

What is the most efficient (and quickest) way to populate the objects?

Which is the quickest way to get data out of the database and into a specific object designed for that result set?

This is C#

-- EDIT

By quickest, we mean the fastest for the computer to process, not speed of development.
This is also a .NET 2.0 application

--

There are a lot of ways to do this, depends on which version of C# you use.
You can use the following:

1. DataSet (.net 2.0 above I think?)
2. Linq2SQL (.net 3.5 above)
3. EntityFramework (.net 3.5 above)

For example of using Linq2SQL:

var result = (from t in YourTable
              select t).ToList();

Most efficient is how you are doing using a forward-only SqlDataReader http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

Requires plenty of "boiler plate" code however so not the quickest to write. I personally use NHiberate for that but that requires some learning http://nhforge.org/Default.aspx

I believe the quickest (as in quickest to develop) is LINQ to SQL .

It does exactly that. Create classes that represent a list of attributes from each table. When accessing them, it retrieves data automatically from the database and populates these objects with data, which you can use then as a list of objects. It's all automated. You just write one single row of code for this, after you've made the mapping (dragged tables from the database to the Linq to SQL mapping)

The code can look as simple as this:

List<Something> yourList = YourAutomaticallyGeneratedDataContext.Something.ToList();

For Firebird, of course, LINQ to SQL won't work. Try Entity Framework instead. NHibernate is too much of a fuss, in my opinion.

But since you want the , I recommend you check out this site: http://ormbattle.net/ Choose your weapon, then fire. ,我建议你看看这个网站: http//ormbattle.net/选择你的武器,然后开火。 I am not considering the option with DataReader. It's a pain, and can lead to bugs. It's hard to maintain too.

Or, check this question, Best free ORM tools to use with .NET 2.0/3.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM