On The Way To Mastering ASP.NET: Introducing Custom Entity Classes

xiaoxiao2021-03-06  20

On The Way To Mastering ASP.NET: Introducing Custom Entity Classes

Karl SeguInmicrosoft Corporation

March 2005

Summary: There are situations for which untyped DataSets may not be the best solution for data manipulation The goal of this guide is to explore an alternative to DataSets:.. Custom entities and collections (32 printed pages)

Contents

IntroductionPROBLEMS with DATASTSCUSTOM Entity ClasseSObject-Relational MappingCustom CollectionsManaging Relationshipshipsbeyond The BasicsConclusion

Introduction

The days of ADODB.RecordSet and the oft-forgotten MoveNext are gone, replaced by the powerful and flexible capabilities of Microsoft ADO.NET. Our new arsenal is the System.Data namespace, featuring lightning-fast DataReaders, feature-rich DataSets, and packaged in a capable object-oriented model. It's no surprise that we have such tools at our disposal. Any 3-tier architecture relies on a solid Data Access Layer (DAL) to elegantly connect the data layer to the business layer. A quality DAL Helps Promote Code Reuse, IS Key To Good Performance, And is Totally Transparent.

As our tools have evolved, so too have our development patterns Saying goodbye to MoveNext was more than ridding ourselves of cumbersome syntax;. It opened our minds to disconnected data, which in turn had a profound impact on how we built applications.

While DataReaders might have been familiar (they behave a lot like RecordSets), it did not take long for us to venture forward and explore DataAdapters, DataSets, DataTables, and DataViews. It was our growing skills at exploiting these new objects that changed how we developed. Disconnected data allowed us to utilize new caching techniques, and thus greatly improve the performance of our applications. The capability of these classes allowed us to write smarter and more powerful functions, while at the same time reducing, sometimes considerably, the amount of code required for common activities.There are a number of situations for which DataSets are particularly well suited, such as prototyping, small systems, and support utilities. However, using them in enterprise systems, where ease of maintenance is more important than time to Market, May Not Be The Best Solution. The goal of this guide is to explore an alternative to DataSets That IS Geared Towards Type of Work: Custom Entities and collections. Other alternatives exist, but none provide the same capability or have more backing. Our first task will be to look at the shortcomings of DataSets in order to understand the problem we are trying to solve.

Keep in mind that every solution has its own advantages and disadvantages, so it's possible that the drawbacks of DataSets are going to be more palatable to you than those of custom entities (which we'll also discuss). You and your team must decide which solution is better suited to your project. Remember to consider the total cost of a solution, including the nature of requirements to change and the likelihood that you'll spend more time in post-production than actually developing code. Finally, note that when I Refer to DataSets, I don't mean type some of the shortcomings associated with untyped-datas.problems with datasets

LACK OF ABSTRACTION

The first and most obvious reason to consider alternatives is the DataSet's inability to decouple your code from the database structure. DataAdapters do a great job of making your code blind to the underlying database vendor (Microsoft, Oracle, IBM, ...) but fail to abstract away the core component of a database:... tables, columns, and relationships These core database components are also the core components of the DataSet DataSets and databases share more than just common components; unfortunately, they also share their schema Given the following select STATEMENT:

SELECT Userid, Firstname, Lastname

From user

WE KNOW THAT VALUES WILL BE AVAILABLE From The Userid, FirstName, And Lastname Datacolumns WITHIN OUR Dataset.

Why is this so bad? Let's look at a basic everyday example. First WE HAVE A Simple Dal Function:

'Visual Basic .NET

Public function getAllUsers () AS Dataset

DIM Connection As New SqlConnection (connection_string)

Dim command as sqlcommand = new sqlcommand ("getusers", connection)

Command.commandType = CommandType.StoredProcedIm Da As SqldataAdapter = New SqlDataAdapter (Command)

Try

DIM DS AS Dataset = New Dataset

Da.fill (DS)

Return DS

Finally

Connection.dispose ()

Command.dispose ()

Da.Dispose ()

END TRY

END FUNCTION

// C #

Public dataset getAllUsers () {

SqlConnection Connection = New SQLCONNECTION (Connection_String);

SQLCommand Command = New SqlCommand ("Getusers", Connection;

Command.commandtype = commandtype.storedProcedure;

SqlDataAdapter Da = New SqlDataAdapter (Command);

Try {

DataSet DS = New DataSet ();

Da.fill (DS);

Return DS;

} finally {

Connection.dispose ();

Command.dispose ();

Da.Dispose ();

}

}

Next We Have a page with a repeater displaying all the users:

<% # DataBinder.eval (Container.DataItem, "firstname")%>