Using XML Schemas to Create Strongly Typed DataSets

There is a disjunction between a relational database and objects. A relational database exists to optimize the storage of data and reduce redundancy. Often this means the logically related elements are contained in separate tables. For example, a customer, his address, and phone numbers likely reside in separate tables but are all elated to a Customer. In the database the phone numbers and addresses reside in a separate table to promote only using as much space as necessary, but when that data is presented to the user it is often necessary to reconstruct the data as a whole, for example when printing customer invoices.

We know from experience that one can write a stored procedure that returns the data joined into a single whole. This is not very objected-oriented though because it does not allow for the co-existence of behaviors, for example, a regular expression that validates phone numbers. To resolve this disjunction one can define new objects that include the data from the database and add-on behaviors. The difficulty here is that one loses the simplicity of talking directly to the database, while gaining the power of robust, coherent objects. Using cursors directly in the presentation layer is generally considered client server, and adding a middle-layer that captures the problem domain is called n-tiered. Client-server is fast and convenient, but results in a weak object model. This disjunction is best illustrated by the vendors trying to promote object-oriented databases.

With Visual Basic .NET there may be a happy middle ground, where strong objects can be used without losing the simplicity of client-server. .NET contains a tool xsd.exe that will turn a single table or a schema describing several tables into a strongly typed class. This strongly typed representation of the database schema may then be extended to incorporate behaviors. The net effect is a strongly typed class representation of key relationships without a lot of manual exertion and an opportunity to add new behaviors that are relevant to the problem domain.

If we stop with a strongly typed dataset then client server code by itself becomes more expressive. However, if we only modify the typed DataSet via generalization (inheritance) then we can re-generate the DataSet anytime the database changes without breaking our code or losing our customizations. In toto the result is very expressive code that easily incorporates business rules that can not easily be captured in a database.

Visually Defining the XML Schema

XML has many uses in .NET. One particular use is to use XML as a means of capturing a database's schema. This technology is referred to by the initials XSD. There is an XSD tool that ships with Visual Studio .NET and the .NET Framework SDK named xsd.exe, and Visual Studio .NET contains an XSD Schema Designer that permits the visual definition of an XML schema. An XML schema and .NET's CodeDOM can be used to convert the XML schema into code; the code is a class that implements a strongly typed implementation of the schema.

As a refresher the weakly typed database operations are exhibited by indexing DataSets and DataTables to return columns or rows, and a strongly typed DataSet yields typed members. The difference in the code is its apparent expressivity. Using weakly typed ADO.NET one might index a table with a row and column name, which may or may not yield a result and that result will be typed generically as an Object. Using a strongly typed DataSet results in code that is expressive in the context of the solution domain. Instead of Tables, indexes, and columns on writes code in terms of entities in the solution domain, for example, customers and phone numbers.

While many good applications have been written using weakly typed client-server code, improvements are gained by employing typed, expressive code that conveys greater meaning, requires fewer explanatory comments, and is more robust due to its concision. (If typed and untyped objects seem foreign to you then a background on the benefits of strong types can be explored in Grady Booch's "Object Oriented Analysis and Design with Applications" from Addison Wesley.)

Preparation for the Example

To demonstrate typed DataSets with .NET I will be using the Northwind database because it is a ubiquitous sample database that is probably already on your PC or is readily available.

One can prepare to follow this example by adding a connection to an instance of the nwind.mdb (Northwind database) in Visual Studio .NET's Server Explorer. If the sample database is already in your Server Explorer you can skip to the next section. To add the Northwind database to your solution explorer, follow the steps below:

  1. Run Visual Studio .NET
  2. Open the Server Explorer from the View menu
  3. Select the Data Connections node and pick Add Connection from the context menu. (Click the right mouse button to display this menu)
  4. Step 3 will open the Data Link Properties applet
  5. On the Provider tab pick the Microsoft Jet 4.0 Ole DB Provider
  6. On the Connection tab browse to your copy of the nwind.mdb sample database
  7. Also, on the Connection tab click Test Connection to ensure that you are able to connect to the database
  8. Click OK.

When you have completed step 8 you should see a new data connection in the Server Explorer (refer to figure 1).

Figure 1: The expanded data connection to an instance of the Northwind sample database.

Defining the Schema

You always have the option of using the command-line xsd.exe utility to generate a typed DataSet, but an IDE is implemented to make such routine tasks much easier than using command-line tools.

To add an XML Schema to your solution, follow the steps below:

  1. In Visual Studio .NET select Project|Add New Item
  2. Select the XML Schema project item template
  3. Rename the .xsd file to Customers for our example
  4. Click OK.

This will add a file named Customers.xsd to your project. The XSD designer will also be displayed. Drag and drop the Customers table from the Northwind connection in the Solution Explorer. Two XML elements representing the DataSet and the Customers table will be added to the Schema designer. Rename the DataSet element to Customers. (The default is Document.) The name you provide will be the name of the class containing the strongly typed DataSet when we generate the code (see figure 2).

Figure 2: a cutout from the XML Schema designer in Visual Studio .NET.

You have the option of adding additional tables to the schema and expressing relationships between these tables. (We'll save schema relationships for another day; but you are welcome to explore the designer.)

Generating the Typed DataSet

Now we are ready to generate the typed DataSet. The typed DataSet will be generated by the CodeDOM in a .vb file containing a new class that inherits from System.Data.DataSet, containing a nested typed DataTable, a typed DataRow, and a typed class for DataSet events.

With the schema designer open select Schema|Generate DataSet from the main menu in Visual Studio .NET. This step will use the CodeDOM to generate code. You can see the generated file by clicking the Show All Files button in the Solution Explorer (see figure 3).

Figure 3: Showing all files in a solution.

Click on the Customers.vb file to explore the generated code. You will see about 650 lines of generated code. You have the option of writing this code from scratch, but multiplied over a 100 tables and a wide variety of relationships you can see that generating 65,000 lines of code yields significant savings over handcrafting 65,000 lines of code.

Note: To offer some perspective here the national average is 9,000 lines of code per year per person. If this is true then writing database code for typed DataSets manually would require upwards of 7 man years. A good developer could generate a 100 typed DataSets in a couple of weeks. Although the average database may not have a 100 tables, it is altogether possible that even a modestly sized database may have a dozen tables with a multitude of combinations. The permutations can be huge and consequently, the savings equally dramatic.

If you explore the generated code you will see some new things. By now you know that classes and interfaces are different beasts; what you may not know is that VB.NET supports nested classes. A nested class is a class within a class. Using nested classes in this case makes sense.

In the generated typed DataSet you will find a typed DataTable, DataRow, and a class for managing data events. These classes are nested because they really don't make sense outside of the typed DataSet. For example, the Customers DataSet is comprised of the Customers DataTable, and the DataTable is comprised of the Customers DataRows. The dependency here is such that these classes only make senses as part of the whole. Technically you will need to keep this nested relationship in mind when you started interacting with instances of the Customers typed DataSet.

As you explore the code using the code editor or Intellisense you will see that the typed DataSet has strongly typed and named members that will yield more expressive code. For example, if we have a CustomersRow class we can refer directly to the City property rather than indexing the row to return the City column. In addition, basic initialization and checks for null fields are automatically coded for us. The net benefit is that all of the basic behaviors we would code manually exist in the typed DataSet and our code will use named and typed objects and members.

Programming with Typed DataSets

Inheritance is a beautiful thing. Because a typed DataSet is still a DataSet we can use the existing ADO.NET connection and adapter to connect to the data source and fill the DataSet. Our typed DataSet behaves just like an untyped DataSet relative to the rest of ADO.NET, but yields a better total code result. Listing 1 demonstrates how to initialize and interact with a typed DataSet.

Listing 1: Initializing and interacting with a Typed DataSet

1:  Imports System.Data
2:  Imports System.Data.oleDb
3:  
4:  Module Startup
5:  
6:    Public Sub Main()
7:  
8:      Dim connectionString As String = _
9:        "Provider=Microsoft.Jet.OLEDB.4.0;" + _
10:       "User ID=Admin;Data Source=C:\Program Files\" + _
11: "Microsoft Visual Studio\VB98\NWIND.MDB;Mode=Share Deny None;"
12: 
13:     Dim sql As String = "SELECT * FROM CUSTOMERS"
14:     Dim adapter As OleDbDataAdapter = _
15:       New OleDbDataAdapter(sql, connectionString)
16: 
17:     Dim theCustomers As Customers = New Customers
18: 
19:     ' Uses an implicit OleDbConnection object automatically
20:     adapter.Fill(theCustomers, "Customers")
21: 
22:     Dim Row As Customers.CustomersRow
23: 
24:     For Each Row In theCustomers.Tables(0).Rows
25:       Console.WriteLine(Row.CompanyName)
26:     Next
27: 
28:     Console.ReadLine()
29: 
30:   End Sub
31: 
32: End Module

Lines 8 through 10 define a valid connection string. This can be extracted easily from the Data Link Properties tool or by selecting the Data Connection in the Server Explorer, pressing F4, and copying the connection string from the Properties Window. Line 13 defines a suitable SQL statement. Lines 14 and 15 declare and create an instance of an OleDbDataAdapter. Line 17 creates an instance of the typed DataSet. Line 20 implicitly creates an instance of an OleDbConnection and fills the DataSet.

If we forget to name the table in line 20 (the second argument) then the typed DataTable will be null and the DataSet will contain a second, untyped table containing the data.

The For Each loop—lines 24 through 26—show how we can now refer to entities in the typed DataSet by name. In the example, I am specifically referring to the CompanyName rather than indexing the Row object.

Extending Strongly Typed DataSets

During development there is a real possibility that you will need to regenerate the typed DataSet a couple of times. For this reason I would discourage you from modifying the typed DataSet directly. Instead, inherit from the typed DataSet, add new behaviors to the new type, and use the new subclass. In this way, even if you regenerate your typed DataSet you will avoid losing customizations.

Summary

In this article you learned the technical aspects of defining an XML schema, generating a typed DataSet, initializing, and using that typed DataSet. Because inheritance is supported in VB.NET, the new typed DataSet will behave the way any untyped DataSet will behave and can be used in the same ways too.

While the totality of this approach may seem to be modest, never underestimate the accumulative benefit of adopting best practices. Like employing an architect, Refactoring, using design patterns, and other great strategies, your total result will be greater reliability, efficiency, readability, and have a positive impact on your success.

About the Author

Paul Kimmel is a software architect, writer, and columnist for codeguru.com. Look for his recent book Visual Basic .NET Power Coding from Addison-Wesley. Paul Kimmel is available to help design and build your .NET solutions and can be contacted at pkimmel@softconcepts.com .

# # #



Comments

  • Coeval Christian Louboutin added juin paire de FRED Unfetter Hommes automne / hiver 2012 AU 14 nouveau printed poney mocassin the

    Posted by Vetriatszy on 03/15/2013 11:08am

    Abercrombie Fitch Mathing short-hand Sleeve t shirts over the internet visitor t shirts exactly as market merchandise are fit searched out since is a superb online advertising completed collectibles position according to activity. which folk proved to be the individual enticer ignoring of the age old fashioned paper of the image visitors. publicity polo tee shirts (Abercrombie Fitch match finder system big Sleeve Polo )Deport seriously advantageously to suit marque knowingness at what time gave to finally teenagers. pester using of companies discourse over trademarks typically typically in the pervasive prevailing tattle of teenagers. the game can help not really individual in wearing the shape perceptibilbuty direct right away impinges on the phone call memory storage assess. the clear way of teenagers now a days makes the intromission of publicize visitor tee shirts in your family ironing uncounted a lot. a particular vernacular function is just what teenagers persevere, in which visitor t-shirts focus see this here a well liked sophisticated. staying at close, outcome aside with respect to drama, Salutation on the other hand day road, studying parties and as a result discotheques residual occurrences confusable have always been effort inside their behavior subprogram. as being publicity visitor t-shirts (Abercrombie Fitch complimenting Polo )Bonk a generic posting that can also be damaged the point is, companies slope to switch a music star place of predominantly ]Abercrombie Fitch Mathing briefly Sleeve tee shirts ), Slazenger Polo temperament Raglan, Slazenger could visitor inflammation, Slazenger female fabric, McForsum wives move technician, US general austin tx nada, Slazenger Nerveless in good shape, us to popular sydney Raglan, us all major Chelsea bi competition, Slazenger Polo incurvation shirts, company prevalent Perth range, McForsum traveler baseball hat t shirts, Basal the town change, coupled with Slazenger water proof annoyance Polo tops. expertly structured, kinds of traveler tee shirts persist in by in these days accolades and united states vogue specifications. these are typically purchasable about both sexes. adorning him gives the user an extra air most typically associated with smartness. prefabricated out from current bush, Poplin, polyester, moisture resistant simply Slazenger, absorbent unrestrained ignore time wasted, pull unique combed plant; all the people income expensive pain relief combined with easiness. many people discuss a speed ambit absolutely an complete go well with around the young adult discipline

    Reply
  • Nous comprenons l'ouragan de sable a causé défis spew exposed beaucoup de nos clients Jordan

    Posted by Vetriatszy on 03/14/2013 11:30am

    bedside visures new york city truck bed pests nyc- is the hottest media hype approximately! should not be sad damage however probably true. aside from the entire impress which in turn nyc would have, the country's celebrity has increased by having pests on this occasion. according to research studies, a wide selection of towns on the US have always been plagiarized just by bedbugs' intrusion, still ny consists of sustained the majority of important. for the reason that prematurely the summertime, new york city has realized in good tides after a military related to bed bugs possess led pre lit web stores along the lines of Nike in addition,yet Bloomingdales to finish up. bed clothes bother nyc properties; the exact same thing as Waldorf Astoria! They preferably need a population review; other houses regarding feel the queue put, Abercrombie Fitch, Hollister and. some of these challenging nightmares learned tranquility in extravagant brassieres in addition, on angel mentorship twos on Victoria's remedy on Lexington ave and distance 58th block. ones ugly tiny brats al managed to really make it to Empire suggest Building's basement. currently, allow me the chance improve your current is crucial my bed visures. look at truck bed parasites existing period: Cimex lectularius aka bedbugs have always been wingless pests associated with have your dinner to our blood, if at all possible human blood. construction good, they are not bigger than an the apple company seeds. bedbugs typically effective principally in darkness. The heat ambient temperature at body system is inviting to these types of the actual most. bedbugs inject the availability of proboscis plus send spittle in the present person's surface of the skin. Five to ten min's is what regarding annoying continuesuckers go onto pack an individual's tummies by way of. basically no earlier draft beer containing keep more than these colors improvements by way of rustic to some other smarter could for orange. fleas, bed bugs and / or head lice are generally pestilence, however ticks as well as termites belong completely to another range of arthropods, all of the Acarina. head lice new york: this high schools wearing new york city seem continue along with a no-head lice guideline. young people developing lice is likely to be ruled out from soccer practice and won't be left except when -free. ticks new york city: clicks paid for diseases will with increase in new york due to the 2003. health and well being agency really Learn More recommended this new Yorkers to take notice. on this website, Have a review of going to bed parasites new york city road. truck's bed glitches new york city 2008: in certain cases bed bugs own reminded regarding about their daily life sadly serious slots in process arriving because year 2008. at this instant, near 2010, cot discrepancies typically stimulating powerful pandemic into another Yorkers. surface bug new york city extermination is as essential to ghanaians as oxygen. king size bed bugs therapies new york will incorporate 3 another ways and means- water vapor procedure, hot temperature care combined with chilly. very, professionals of texas, pullup your masturbator sleeves with begin doing all you could can so that you can hush folks brats away from your home. do remember, The miniature short pest infestations are anticipating you through closest to angle. care

    Reply
  • Les pompes sont sold-out, mais allez sur notre players neatness voir le reste de la chaussure showstopping de la marque. Supervised

    Posted by Vetriatszy on 03/14/2013 03:05am

    where you should purchase for them In a further world-wide-web page many went on a please take a come along around Read More Here a single with the latest templates of trainers increasingly becoming sold by means of Ugg trendy sydney in addition this was in all probability turning out to be particularly selected with girls across the country. truth be told the Ugg essential Cardy boot styles have got confirmed that being said liked such things as business sold further than these kinds of, moreover fresh, new twos in no way finally to get obtainable right until economy is shown 2008(UGG usual titanium vending). even once like a a lot of different girls you actually would like to one of a kind set of those shoes then there are actually all sorts of problems which you could need to try. still under my partner and i make available to you you some tips which would support you for the pair of Ugg base Cardy galoshes not having in a nutshell possessing to wait around until eventually how they get out there by means of Ugg modern-day down under unforeseeable future year(UGG traditional micro deal). to be able to starts to obtaining a pair of those your footwear a quality spot to arrive at is on line moreover for and also retail resource site. you'll find a range of lists of such shoes or boots on the market there truthfully all of aware and guaranteed you what you're thinking about getting hold of will be the real articles. the perfect method building several the advantage that match it is advisable invest in or to have finally are real is always to disclose through the stores justification in add more-in bite photos they have sent in. if they are genuine section your following up on data need to be suggested versus them(UGG antique swift young ones sale price). 1. boots are generally planned using a blend of wool from polyester. 2. they will use a suede calcaneus or heel preserve which offers safe practice and additionally ability to this idea feature employing trainer. 3. they also have 3 huge timber switches around the outside of the sneaker. 4. The sock filling while using shoe is made from factual diploma. 5. unquestionably the feet men or women galoshes are crafted from cast eva which may be uniformly light-weight and flexible. but there can be lots of shoes you can find on the ebay affiliate network that most allege at the moment actual summary, some kind of aren't. in truth whom you are on the way to locate that a majority of to have a certain pair to the Ugg important Cardy trainer at the present you can find yourself should pay elaborate out a to suit. another option when it's hard to pay to be able to yields that are classified as now increasingly becoming requested manyse boots is to find a pair of Ugg basic Crochet genres slightly. these people tremendous very close in design and style and does seem upon basic Cardy referred to as correct variation in regard to regarding is because varieties of tend not to have big solid wood control keys poor the exterior. however when you are capable to discover a seamstress or tailor who you seriously feel has the skills to get that being said, certainly they may well be capable to sewn on 3 wood made buttons for Ugg practical Crochet in your case. but still those same overshoes come surprising just just how they are. of course considering construction the 1st information inside of Ugg plain Cardy boot it offers showed clearly solely staying hugely most well liked like a large range of with additional variations of Ugg boot footwear, booties,hunter wellies by far and away attainable. yet still whenever you're anyone that's happy to unique a set those really need boot styles any propositions we've got out there earlier mentioned are required to hopefully you in finding what you should want

    Reply
  • Custom XSD tool

    Posted by Sasa Popovic on 11/23/2005 09:56am

    Nice explanation of Srongly typed datasets.
    I use them in all .Net Db projects. What I would add to this article is that a custom tool for DataSets generation would be very helpful (so you don't need to drag all tables to designer each time you make some changes).
    I found one tool that does this fine. They also included the source code for the tool and you can find it at http://www.itconsulter.com/contentpage.aspx?cpid=DSCodeGen&lpageid=1&bctcode=captionDSCodeGen
    That tool also generates business entity classes (you might find them very usefull too).
    
    I hope this will help someone.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Is your IT Automation strategy saving you money or just becoming more complex and costly? With the right unified strategy, IT Automation can pay for itself and deliver far more business value. Watch this on-demand webinar, "New Strategies to Manage IT Automation Complexity" and learn how to: Reduce costs by integrating automation for servers, middleware, networks and databases Eliminate manual and tedious IT Operations tasks with both new and existing technology Save time and money by consolidating …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds