blog.mha.dk
The on-line blog of Michael Holm Andersen

Setting DataContext Connection String at runtime

Tuesday, 17 November 2009 14:04 by mha

I’m working on a portal/e-shop solution where I’ll be using multiple databases and needs to change the Connection used by my DataContext object at runtime.

I use the HttpContext.Current.Items collection to store which connection is currently “active” - this is setup in the Application_AcquireRequestState event in Global.asax which fires for every request – and using the OnCreated method that is called as part of the DataContext’s constructors, I can easily change which connection to use at runtime.

The good thing about putting the “connection switch” in the OnCreated method is that no matter how I use the DataContext this code is always executed (I have a LinqUtil class which implement a request-scoped DataContext pattern for LINQ to SQL), and using the below approach I don’t have to worry about which Connection is used as this is transparent to any helper / DAL classes.

The code for the partial class which implements the OnCreated method for my DataContext looks (something!) like this:

   1: public partial class GWportalDataContext : System.Data.Linq.DataContext
   2:     {
   3:         // In order to use multiple database connections we use the HttpContext.Current.Items collection to store which database we're currently using (per request)
   4:         partial void OnCreated()
   5:         {
   6:             switch (HttpContext.Current.Items[ConfigurationManager.AppSettings["WebsiteHttpContextItemKey"]].ToString())
   7:             {
   8:                 case "GWDK":
   9:                     this.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["GWportalConnectionDK"].ConnectionString;
  10:                     break;
  11:                 case "GWNO":
  12:                     this.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["GWportalConnectionNO"].ConnectionString;
  13:                     break;
  14:                 default:
  15:                     this.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["GWportalConnectionDK"].ConnectionString;
  16:                     break;
  17:             }
  18:         }
  19:     }

Use cases - a brief introduction

Friday, 4 July 2008 07:46 by mha

The Unified Software Development Process, Analysis and design is driven by use cases. Tons of books have been written about the subject (just do a quick search on Amazon), however many authors seems to "over complicate" the matter.

I've found two great articles about Use cases, which I think is worth reading if you're new to Use cases..

1) Use cases - An Introduction: usecases_intro.pdf (315.54 kb)

2) Driving Development with Use Cases: usecases.pdf (349.23 kb)

UML is, of course, also a big issue if you're beginning your quest into use case-driven software development projects. If you're new to UML take a look at What is UML?