Library tutorials & articles

Encapsulating ODBC in ASP .NET

The Solution

The obvious answer to what is essentially a code re-use problem is to wrap the required functionality into an object. This basic Object-Oriented Programming (OOP) technique, called encapsulation, has the following benefits:

  • It conceals the complexity underlying the code's functionality, presenting just as much interface as required, and no more. This simplifies code re-use, since the next user down the line only needs to be familiar with the object interface rather than the whole package.
  • The object only needs to be changed once, no matter how many times it's used in the source code. This is particularly important in the ODBC application, since any tinkering with the database will likely necessitate some alteration of the Data Adapter's field mappings.

Once we encapsulated the ODBC initialization code and some basic operations—searches, inserts, and the like—a number of other possibilities suggested themselves. The key concept is to visualize the new object as a proxy for the database on the far end and imagine what might be useful to know about it. For example, if you'd like to know how many records are in the database, then you can attach a read-only Count property to an aggregate query and, presto! xxxDB.Count is now at your disposal.

The following example builds on the framework set up in our previous ODBC article. If you detect minor differences in the source code, rest assured that there are no subtle reasons—our code has simply evolved to meet current requirements, and our clients don't pay us to go back and update past articles to reflect these changes.

As with the previous example, the purpose of this code is to support the e-mail registration functionality on HotQuant's News & Updates page. The new object must meet the following requirements:

  • It must encapsulate the entire ODBC initialization process.
  • It must expose simple data aggregate queries as read-only object properties.
  • It must expose useful data operations as public methods.
  • It must allow direct access to its Dataset.
  • It must encapsulate the entire ODBC clean-up process.

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Encapsulating ODBC in ASP .NET.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related podcasts

  • CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly Media

    CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly MediaHosts Ken Levy and Markus Egger discuss the new State of .NET events, IE8, ASP.NET MVC, followed by an interview from PDC with two editors from O'Reilly Media. More on ASP.NET MVC can be found at http://asp.net/mvc. Interview...

Related jobs

Events coming up

  • Mar 23

    DevWeek 2009

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET Framework 4.0, Silverlight 2, WCF 4.0, Visual Studio 2010, RESTful services, Windows Workflow, ASP.NET AJAX 4.0, SQL Server 2008, LINQ, C# 3, .NET Patterns, Ruby, and more.