Solved

Dynamic connection string guidance

Posted on 2008-10-01
6
1,440 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now