views:

1134

answers:

2

I'm in the process of adding a Data Access Layer for our ASP.Net 2.0 web application that was written almost exclusively using in-line SQL calls and copious use of copy/paste. Due to business concerns I am only able to re-factor small portions of the application at a time (can't settle in and do a huge re-design), so I have to live with certain design decisions until things are loosely coupled enough to make big changes and test them properly.

I've decided to use the Enterprise Library Data Access Application Block as the plumbing for the Data Access Layer going forward, and one such previous design decision is causing me problems. Currently we get the "main" connection string for our application from an admin database based on the account id provided by the user so that we can allow a single installation of the application to access multiple databases. My problem deals with trying to figure out the best practices way of getting that connection string (or the account id) to the DAL.

The previous implementation was storing the encrypted connection string in a cookie, so my current hack-approach is to get the connection string from there and then use DAAB in the following manner:

Public Shared Function GetResultsByKeywords(ByVal key1 As String, ByVal key2 As String, ByVal key3 As String, ByVal key4 As String) As DataTable
    Dim db As SqlDatabase = New SqlDatabase(Connection.GetConnectString())
    Dim dt As DataTable = New DataTable

    Using dr As IDataReader = db.ExecuteReader("sel_eHELP_ITEMS", key1, key2, key3, key4)
        dt.Load(dr)
    End Using

    Return dt
End Function

Where Connection.GetConnectString() is fetching the connection string from the cookie.

I know this is nowhere near the best way to do this and I need to fix it, but I'm struggling with the "right" way to get it done. Maybe create a base class that I can initialize with the connection string and inherit all other classes from, but I'm not sure how this would look and where I would first initialize an instance of that class. On every page before accessing any DAL functions?

Any guidance would be appreciated. Thank you.

Update

Let me clarify. At most there will be 3 or 4 different connection strings for an installation. The "account id" is not a user id and is used by multiple users to essentially specify which database to connect to.

+1  A: 

Edit:

With your latest update, I still recommend looking into the Configuration using the DAAB http://davidhayden.com/blog/dave/archive/2006/01/23/2744.aspx

I'm having difficulty finding/opening the Ent Lib docs, but I would see if it has the equivalent of ChangeDatabase method of the SqlConnection object. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.changedatabase.aspx

Here's one way to do it .. Since I don't think your DAL should know about your switching DB requirement I would setup your DAL with a ConnectionString as a parameter of the constructor. Use a wrapper method/property to hide/encapsulate that requirement and get the instance of the DAL you would like to use in your app.

(please excuse the c#)

public class MyDAL
{
    private string _connectionString;

    public MyDAL(string connectionString)
    {
        _connectionString = connectionString;
    }

    public int MyDatabaseCall()
    {
        using (SqlConnection conn = new SqlConnection(_connectionString))
        {
            using (SqlCommand cmd = new SqlCommand("my sql", conn))
            {
                conn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    //...data access
                    return 0;
                }
            }
        }
    }
}

public class MyApp
{
    public static Dictionary<string, MyDAL> myDatabases = new Dictionary<string, MyDAL>();

    public string GetConnectionString(string database)
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings[database].ConnectionString;
    }

    public void StartUp() // Global.asax Application_Start ?
    {
        myDatabases.Add("Database1", new MyDAL(GetConnectionString("Database1")));
        myDatabases.Add("Database2", new MyDAL(GetConnectionString("Database2")));
        myDatabases.Add("Database3", new MyDAL(GetConnectionString("Database3")));
    }

    public MyDAL DataAcccessLayer
    {
        get
        {
            string usersDB = FigureOutUsersDatabase();
            return myDatabases[usersDB];
        }
    }

    public void UseSomeData()
    {
        var myData = DataAcccessLayer.MyDatabaseCall();
        //Do Stuff
    }
}

Hope that helps, good luck! And be very careful what you put in cookies. ;)

Chad Grant
Thank you. This wasn't exactly how I ended up solving the problem, but it still illustrates the basic concept that I used. No more connection strings stored in cookies!
classicmfk
A: 

Have a look at the following link:

link text

This might give a solution for you.

Cheers