Solved

Dynamic connection string guidance

Posted on 2008-10-01
6
1,450 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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