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.

You might also like...

Comments

About the author

Jason G. Williscroft

Jason G. Williscroft United States

Jason Williscroft is a former Marine and naval officer, and a graduate of the United States Naval Academy, where he studied Systems Engineering. He is currently General Manager of HotQuant, LLC,...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Java is to JavaScript what Car is to Carpet.” - Chris Heilmann