Solved

Dynamic connection string guidance

Posted on 2008-10-01
6
1,451 Views
Last Modified: 2012-05-05
Hi,

I'm developing a hosted asp.net web app (n-tier layer: ui[mvp] -> service-> business-> data[NHibernate] ) in which each customer have their own database. This means that when the customer logs in I need to create a unique connection string for each customer (given the unique customer id).


The simples solution I could think of for this is to store the customer id in a Session and pass that along to the data access layer (which uses NHibernate) which will create a connection string specific for that user.
The downside of course is that there will be a lot of overhead, in that I need to add the customer id within each method call.

Is there a more robust and manageable solution (perhaps some sort of pattern)? Maybe some way to store the connectionstring in the data access layer so I do not need to pass it along each time the customer needs to fetch/store something in the database.

Any help is appreatiated.
Thanks!

0
Comment
Question by:oxyoo
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:Rikus_Trent
ID: 22616501
In your data access class give it a static readonly string varibale to hold our connection string, then pass the customer id to its constructor to assemble the string. You can then refer to it from all your data access methods.
 public class MyDataAccessor
    {
        private static readonly string _connectionString = String.Empty;
        
        static MyDataAccessor(string CustId)
        {
            _connectionString = "yadda yadda yadda" + CustId + "blah blah blah";
        }

Open in new window

0
 
LVL 7

Accepted Solution

by:
joesthebighmoe earned 500 total points
ID: 22616529
Joe's 2 cents...
Overhead might be the wrong term.
I don't think it is uncommon at all for applications that can use different databases (sometimes just development versus production) to pass something around for DB related calls that contains information related to the database connectivity.

At our firm we refer to this class as Activity Context, this object contains database connectivity information along with other information. It is passed all over the place. Adding it as another parameter to routines that require database connectivity has never really been a pain. Often a service object is created and an Activity Context is passed in to the constructor and then all calls to the service get the connection information from there.

Sometimes the simplest solution is the best solution and easiest to understand for "the next guy."

In the past I used a solution that required a shared database that contained the connection information for each client. So all calls required a call to the common database first and then to the database specified in the connection information.  The information could have been stored somewhere after it was called, but as you can imagine the call to get the information was so fast it did not matter.

0
 
LVL 10

Author Comment

by:oxyoo
ID: 22616793
@Rikus_Trent
Wouldn't the use of static mean that it would be the same for all users of the web app? Or is a static variable in asp.net only static within the current user session and can't be accessed from other users?



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:oxyoo
ID: 22616804
@joesthebighmoe
You have a point in that it is a more simple way, and that usually tends to be less error prone and easier to understand.
0
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22616857
I was jsut reminded of another time where we used windows security on the website. The data access layer would then just check to see what user was logged in and then accessed the appropriate database. The database information was hard-coded in the Dal. This worked because new clients were not added that often, so when we got a new client, waiting for the next release was not a big deal at all.
0
 
LVL 10

Author Closing Comment

by:oxyoo
ID: 31502044
Thanks for your input, I went with the approach of storing the id in a session and passing it along via Service layer.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Input parameteres to DragOver 2 36
Unlocking a column in excel using C# 17 27
How can I do Unit Testing with Session ? 1 23
YouTube API get the Video Tags - vb.net 7 30
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question