Monday, 5 November 2007

Keeping all your connection strings in the Web.Config when using library classes (aka simplified deployment and configuration model)

One of the problems with using multiple projects e.g. DataAccess, BusinessLogic, etc classes is that the Data Access Classes need to be able to read from the Web.Config in the Web.UI layer. However, we sometimes cannot Reference the WebUI layer directly because (apart from being a bad design), it would create a circular reference.

There are a few possible solutions to this. One is to use the VS 2008 Client Application services as per http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/06/03/9463.aspx
However, this is dependent upon the service name being consistent. This is a problem on a local Cassini development machine which can change port numbers. If you want a low maintenance approach to reading connection strings from your associated web project:
  1. Add A Reference to System.Web in your library project. This will give you access to the WebConfigurationManager class.
  2. Go to your data access project e.g. "Data", and go to the settings section.
  3. Create your connection string in the settings section.
  4. Add the following code so that your values are set when the settings are initialised.




   1:  using System.Web.Configuration;

   2:   

   3:  namespace Pfizer.CreditReturns.Data.Properties {

   4:      

   5:      

   6:      // This class allows you to handle specific events on the settings class:

   7:      //  The SettingChanging event is raised before a setting's value is changed.

   8:      //  The PropertyChanged event is raised after a setting's value is changed.

   9:      //  The SettingsLoaded event is raised after the setting values are loaded.

  10:      //  The SettingsSaving event is raised before the setting values are saved.

  11:      public sealed partial class Settings {

  12:          

  13:          public Settings() {

  14:   

  15:              //

  16:              this["CreditReturnsConnectionString"] = WebConfigurationManager.ConnectionStrings["PfizerCreditReturnsConnectionString"].ConnectionString;

  17:              this["DWHS_CreditReturnsConnectionString"] = WebConfigurationManager.ConnectionStrings["PfizerCreditReturnsDataWarehouseConnectionString"].ConnectionString;

  18:   

  19:              //System.Console.WriteLine(this.CreditReturnsConnectionString);

  20:              // // To add event handlers for saving and changing settings, uncomment the lines below:

  21:              //

  22:              // this.SettingChanging += this.SettingChangingEventHandler;

  23:              //

  24:              // this.SettingsSaving += this.SettingsSavingEventHandler;

  25:              //

  26:                          

  27:   

  28:          }

  29:          

  30:          private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) {

  31:              // Add code to handle the SettingChangingEvent event here.

  32:          }

  33:          

  34:          private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) {

  35:              // Add code to handle the SettingsSaving event here.

  36:          }


This example will load your configuration settings from the Web.Config at runtime.

No comments: