CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

Using data components on an ASP.NET page

In my apps I organize all interaction with my databases in components. On my component is a connection to the database and a couple of data-adapters to read and write data.

These components are housed in a separate assembly. From there I can use them straight to fill the datasets on my web– or win-forms, or write a webservice (or other) remoting layer around them (see this 2005 example). How many adapters a component should have is a point of discussion. In my first 2003 applications they were gigantic. Nowadays I make them a lot smaller. Based on experimenting with datasets 2005–style (and all the SOA stories) I beleive a component should work with just a couple of related tables, like invoices and invoicelines. In this article on 2005 datasets that idea is explored a little further.

Inside the component the constructor opens the connection to the database.

public OnderwijsComponent()
{
   InitializeComponent();
   sqlConnection1.Open();
}

The component publishes a couple of public methods which expose the data as XML datasets.

public StudieOnderdeel StudieOnderdeel(int id)
{
   StudieOnderdeel ds = new StudieOnderdeel();
   StudieOnderdeelCRUD.SelectCommand.Parameters["@id"].Value = id;
   StudieOnderdeelCRUD.Fill(ds.STUDOND);
   return ds;
}

The connection to the database is closed in the componenet’s dispose method.

protected override void Dispose( bool disposing )
{
   if( disposing )
   {
      if(components != null)
      {
         sqlConnection1.Close();
         components.Dispose();
      }
   }
   base.Dispose( disposing );
}

Using the using statement my webpages can interact with the database and will be sure that the connection to the database is allways closed.

private void Page_Load(object sender, System.EventArgs e)
{
   using (StudieOnderdeel onderwijsData = new StudieOnderdeel())
   {
      studieOnderdeel1.Merge(onderWijsData.StudieOnderdeel(id));
   }
}

Using will always fire the dispose method of the (Idisposable) object it is using, also when an exception is hit.

Working this way does have one drawback. Every time I need a componenet it is created, the db-connection is opened, the db is fiddled with and the component is disposed. When I need to do something with the data on several points in the page lifecycle, say in the pagerender (not the pageload) and in a button click, the component is created (and the db-connection is opened) twice on one roundtrip. Ado.net connection pooling is great but there is a more efficient way. To create my components only once on a roundtrip and only when really needed I add the componenets as “lazy” properties of the webform.

private OnderwijsComponent _onderWijsData;

private OnderwijsComponent onderWijsData
{
   get
   {
      if (_onderWijsData == null)
         _onderWijsData = new OnderwijsComponent();
      return _onderWijsData;
   }
}

The componenets are now created on demand. But I’ve lost the invocation of their Dispose method. Where to do that ? On a web form the unload event is always fired, also when the page throws an exception. The handler of the unload events checks if the component was created. If so it will invoke its dispose method.

private void StudieOnderdeel_Unload(object sender, System.EventArgs e)
{
   if (_onderWijsData != null)
      _onderWijsData.Dispose();
}

Following this pattern has the following advnatages

  • A component (and a connection to the database) is only created when really needed
  • A component is never created more than once on a roundtrip
  • The component is always disposed. (And the connection to the database is always closed)

Works like a charm.

 


Published May 18 2005, 06:56 AM by pvanooijen
Filed under: ,

Comments

Udi Dahan - The Software Simplist said:

On using data components in ASP.net pages, or WinForms for that matter - don't.
# May 24, 2005 6:58 AM

pvanooijen said:

That's to much simplism. A why would be nice.
# May 24, 2005 11:57 AM

Udi Dahan - The Software Simplist said:

Do you have any way of wrapping the data of an Order with its logic?
# June 24, 2005 4:16 AM

pvanooijen said:

Wrapping logic and data in one object, that's entering the field of O/R mappers.

What I do is stuff the logic in the componenet as well. The componenet exposes the data in its public methods and properties. These methods are my place to code my logic. The methods to retreive data perform calculations and group the data. The methods which update the database validate my bussiness rules.

This is no real bussiness object yet, where data and logic are in one and the same object. I have a component object which serves and accepets dumb data in datasets. But is one central place to handle bussiness logic in a middle tier.
# June 26, 2005 11:57 AM

Peter's Gekko said:

Often I work for people with a Delphi background to introduce them to C#. No problem until we reach the...
# July 15, 2005 7:08 AM

Peter's Gekko said:

VS 2003, VS 2005, the datagrid and controlstate
I have built a lot of web application with Visual Studio...
# August 3, 2005 3:16 PM

Peter's Gekko said:

VS 2003, VS 2005, the datagrid and controlstate
I have built a lot of web application with Visual Studio...
# August 3, 2005 3:24 PM

Peter's Gekko said:

VS 2003, VS 2005, the datagrid and controlstate
The DataGrid with Visual Studio 2003 is a very nice...
# August 15, 2005 10:43 AM

Peter's Gekko said:

Author: <a href="/blogs/peter.van.ooijen">Peter Van Ooijen</a><br />
The DataGrid with Visual Studio 2003 is a very nice control, however, when it comes to the viewstate and "pushing your app through the wire" it does have some serious drawbacks. Visual Studio 2005 does go a long way towards fixing these issues, but a better general approach to optimizing DataGrid performance is fighting viewstate size.
# August 22, 2005 11:16 AM

Peter's Gekko said:

Author: <a href="/blogs/peter.van.ooijen">Peter Van Ooijen</a><br />
The DataGrid with Visual Studio 2003 is a very nice control, however, when it comes to the viewstate and "pushing your app through the wire" it does have some serious drawbacks. Visual Studio 2005 does go a long way towards fixing these issues, but a better general approach to optimizing DataGrid performance is fighting viewstate size.
# August 22, 2005 11:16 AM

Peter's Gekko said:

Yesterday David had an interesting post on opening and closing sql connections to the database where...
# November 4, 2005 6:31 AM

Peter's Gekko said:

Yesterday David had an interesting post on opening and closing sql connections to the database where...
# November 4, 2005 6:45 AM

Peter's Gekko said:

<Update> : As you will read in the comments the solution presented is not recommended. But the...
# November 7, 2005 3:58 AM

Peter's Gekko said:

<Update> : As you will read in the comments the solution presented is not recommended. But the...
# November 7, 2005 3:59 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

This Blog

Syndication

News