Is List<> better than DataSet for UI Layer in A

2019-02-23 07:08发布

I want to get data from my data access layer into my business layer, then prepare it for use in my UI.

So i wonder: is it better to read my data by DataReader and use it to fill a List<BLClasses> or to fill a DataSet and send the DataSet to UI Layer ??.

I'm interested in good performance and scalability.

8条回答
女痞
2楼-- · 2019-02-23 07:27

I don't know about performance, but in terms of maintenance and readabilty the use of a list<> is far better.

One large problem with using datasets with data tables is that the next guy to work on the code will have a big problem figuring out what tables are in a given dataset - especially when you consider the habit a lot of people seem to have of creating new tables and adding them to the set for use later.

With a list of objects it is strongly typed and explicit, moreover the structure of the object is well known and will references to properties will change with the object. Often with datatables people will resort to referencing columns by index rather than name which is a timebomb.

If performance is an issue then look at other generic collections, things like dictionary have very very nice key retrieval properties (almost n(1) apparently) which can make a huge impact in the right spots.

Obviously this sits on top of a discussion about interfaces and am assuming that you know how you want to expose your objects functionality.

查看更多
萌系小妹纸
3楼-- · 2019-02-23 07:27

List<T> is type-safe - You always know what you're getting and you never (rarely) have to worry about casting. You don't have that luxury with DataSet.

List<T> also has all the nice Linq helper methods built in, assuming you're on 3.0

查看更多
SAY GOODBYE
4楼-- · 2019-02-23 07:29

Check out Performance Comparison: Data Access Techniques. It is quite old but has some good information regarding the performance between DataSet, DataRader, and XmlReader.

查看更多
做自己的国王
5楼-- · 2019-02-23 07:33

I would suggest a List<> if you are just reading from the db and presenting on the UI. The DataSet is a pretty complex and heavy class, so I would use it only of you needs its special features, like deferred updates, data relationships, etc.

查看更多
再贱就再见
6楼-- · 2019-02-23 07:34

Using a List<T>, IList<T> or even better an IQueryable<T> give you the advantage of hiding the implementation details behind an interface boundary. This frees you up to change how you handling the retrieval of that at a later time.

查看更多
迷人小祖宗
7楼-- · 2019-02-23 07:42

i would say it would be better to use your custom objects instead of a dataset. it is best practice to have your ui/business layers be blind to how you are implementing your data access. Any way you actually talk to your data source (db, xml, ect.) should be contained in your data access layer and your business layer should just be expecting your custom objects.

查看更多
登录 后发表回答