Using Strongly Typed Objects and Collections to replace DataSet’s in your .NET applications.
page 2 of 4
by Phil Winstanley
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 27790/ 67

The Strongly Typed Object

The easiest way to look at building objects is by first thinking about the logical structure of your data. Using the idea of a Foo we shall construct a logical Data Hierarchy of objects.

Start at the very top of the tree, then move down, firstly the Foo class.

using System;

namespace BusinessObjects

{

          public class Foo

          {

                   private int _Id = -1;

                   public string Id

                   {

                             get

                             {

                                      return _Id;

                             }

                             set

                             {

                                      _Id = value;

                             }

                   }

                   public Foo()

                   {

                   }

          }

}

Initially there is just one property on the object, this is enough for us to be able to Identify which Foo the Class is. Also it is handy to be able to pre-populate the properties on the class, this is achieved by using constructors.

namespace BusinessObjects

{

          public class Foo

          {

                   private int _Id = -1;

                   public string Id

                   {

                             get

                             {

                                      return _Id;

                             }

                             set

                             {

                                      _Id = value;

                  

                   public Foo()

                   {

                   }

                   public Foo(int FooId)

                   {

                             _Id = FooId;

                   }

          }

}

We now have some code that is simple to understand and very easy to use, some sample usage is here: -

private void DoSomething()

{

          BusinessObjects.Foo C = new BusinessObjects.Foo(500002112);

          Console.WriteLine(C.Id);

}

The Strongly Typed Collection

Having one of an object is useful, but being able to group objects of the same type together in to a collection is essential. What follows is the code to create an object in which to store the Foo objects.

using System;

using System.Collections;

namespace BusinessObjects

{

          public class FooCollection : CollectionBase

          {

                   public Foo this[int index]

                   {

                             get { return (Foo) this.List[index]; }

                             set { this.List[index] = value; }

                   }

                   public void Add(Foo myFoo)

                   {

                             if (!DoesFooExist(myFoo.Id))

                             {

                                      this.InnerList.Add(myFoo);

                             }

                   }

                   public bool DoesFooExist(int FooId)

                   {

                             bool flag = false;

                             foreach(Foo C in this)

                             {

                                      if (C.Id == FooId)

                                      {

                                                flag = true;

                                                break;

                                      }

                             }

                             return flag;

                   }

                   public FooCollection ()

                   {

                   }

          }

}

What we have above is a Class that inherits from CollectionBase, we’ve added an Indexer, an Add Method and a Method to check if a Foo object is already contained within the collection.

What follows is sample usage of the Collection object.

private void DoSomethingElse()

{

          BusinessObjects.Foo Foo1 = new BusinessObjects.Foo(1);

          BusinessObjects.Foo Foo2 = new BusinessObjects.Foo(2);

          BusinessObjects.FooCollection CC = new BusinessObjects.FooCollection();

          Console.WriteLine("Foo in the Collection? : " + CC.DoesFooExist(Foo1.Id));

          CC.Add(Foo1);

          CC.Add(Foo2);

         

          Console.WriteLine("Foo in the Collection now ? : " + CC.DoesFooExist(Foo1.Id));

          Console.WriteLine("Foo in the Collection using an Index? : " + Convert.ToString(Foo2.Id == CC[0].Id));

          Console.WriteLine("Foo in the Collection using an Index? : " + Convert.ToString(Foo2.Id == CC[1].Id));

}

The result from the above would be: -

Foo in the Collection? : False

Foo in the Collection now ? : True

Foo in the Collection using an Index? : False

Foo in the Collection using an Index? : True


View Entire Article

User Comments

Title: Mr.   
Name: Alan
Date: 2007-12-28 6:00:51 AM
Comment:
Another reason to use strongly typed objects over datasets is the size of a dataset... For instance if you are building a distributed app and want to send object over the wire to an app server then the overhead of using datasets is huge just because of their serialization size when comapred with strongly typed objects.

Travel boy this is why strongly typing datasets is not a great idea most of the type. You want to avoid the dependency on datasets too.
Title: what about dynamic connection ?   
Name: Sam
Date: 2006-07-19 5:55:06 PM
Comment:
Hi,
Nice article. But you don't go through connection strings. One problem I have is how to pass DYNAMICALLY a connection string to the dataAdapters at runtime. Say that I have a different connection string for each user, stored in a Session variable, how would I pass that to the xsd each time I want to use of a dataTable ?
Title: UML   
Name: Magnus Andersson
Date: 2005-06-10 4:18:14 AM
Comment:
WowWowWow !

Now i can design strongly typed object(dataset) in the whole project, without XML Schema.
This is great when i working with UML Design, because
ADO.NET object is a mess in UML design.
It is now time to build a automatic UML to dataset Class generator
Title: travelboy   
Name: travelboy
Date: 2005-06-08 12:15:54 PM
Comment:
Would advise looking into typed datasets as oppossed to spending hours defining your own collections.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-12-11 7:38:32 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search