开发者

Class or Variables?

If I need to get info from a database, items from an invoice, for instance... should I just get the data into a data table, and store them into variables, or should I create a class?

What's better?

MyClass.ItemNumber;
MyClass.Price;
MyClass.InvoiceNumber;

or...

myItemNumber = (int)dt["itemnumber"];
myPrice = (double)dt["price"];
myInvNum = dt["InvNum"].ToString();

Why? If it's something I will never have to use anywhere else, are variables fine, or is it beneficial to create a "Invoice Class"? If I get data, store it in a class instance, is it cached for form posts in .Net? Or is each page recreating the class instance/db call, etc.? How about performance? Please explain concepts. The concepts are the hardest thing for me to grasp... coding--easy. Concepts of开发者_StackOverflow .Net--not so easy.


The two main reasons for using custom objects (classes) for storing data are type safety, and ease of understanding/maintainability etc in the future. It's a lot easier to understand what MyInvoice.Customer is than dt[0][3].ToString() or (string)dt["sCust"] or some other horrid magic string approach.

This article from Imar Spaanjaars explains how to use custom objects in ASP.NET development. It's based on ASP.NET 2.0, but is probably the best of its type in terms of communicating the concepts.


If its for immediate use only, class is definitely an unneeded overhead.

Your datatable dt is already an entity in the memory with the data. Variables are enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜