Solved

Dynamic connection string guidance

Posted on 2008-10-01
6
1,453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

630 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