Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1461
  • Last Modified:

Dynamic connection string guidance

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
oxyoo
Asked:
oxyoo
  • 3
  • 2
1 Solution
 
Rikus_TrentCommented:
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
 
joesthebighmoeCommented:
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
 
oxyooAuthor Commented:
@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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
oxyooAuthor Commented:
@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
 
joesthebighmoeCommented:
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
 
oxyooAuthor Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now