English 中文(简体)
Is List<> better than DataSet for UI Layer in ASP.Net?
原标题:

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.

最佳回答

Your UI layer should be far-removed from your data access strategy - it shouldn t be dealing with raw data. Thus, your UI should communicate solely with your domain objects (directly or through a service layer).

You can then have the UI layer create the data structures that it needs in order to render. Any performance concerns could be mitigated through a caching strategy.

This affects the readability, testability and maintainability of the code which should always be long- and short-term goals of any design.

I think ideally you d want to see something along these lines:

 UI
 /|
  |
SERVICE (? depending on the design)
 /|
  |
DOMAIN
 /|
  |
 DCO
 /|
  |
 DAL

EDIT:

On the flip-side, if you re building a very straight-forward WebForms application, then you may be able to forgo some of this complexity and just use the designers provided by Visual Studio. I ve found, however, that applications often evolve beyond the capabilities of the drag-and-drop interface and you end up having to refactor into a more separated design.

You always need to know what you stand to gain/lose by choosing an architectural strategy. If you can live (on the edge) with no testing, reduced readability and tighter coupling of the UI to the data ****shudder****, then perhaps the more straight-forward approach is for you.

Some links for further reading

问题回答

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.

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.

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.

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

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

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.

If you are worried about performance, take a look at the code-behind of a strongly typed dataset and you can make up your mind yourself ;)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签