0

I have different database with same schema. I need to connect to one of them according to the client that has been connected. For example if ClientA is connected i must connect to DatabaseA; If ClientB is connected i must connect to DatabaseB; Now I have a multi-layered application: a Presentation Layer, a Service Layer and a repository Layer. With some logic I obtain the correct connection string!

So.. What I need now.

I don't know where to store the connection string for that client! I was thinking in Session. But if I store it in the session I have to pass it in each call I do to the repository layer, because Session is not visible at repository. I think this is the worst solution I could take.

I would like to have the selected connection string just a repository layer in a global variable for that client.

Suggestion? Thank you

Simone
  • 2,304
  • 6
  • 30
  • 79

4 Answers4

1

If you want to keep global data for web site - you are able to store any data in Application class, more details you can check by the following link: http://www.asp.net/web-forms/overview/data-access/caching-data/caching-data-at-application-startup-cs In case when you have any separate parts of application , you are able to keep shared data in the xml file and Create ConfigManager in each application domain or you can use database table.

Vladyslav Furdak
  • 1,765
  • 2
  • 22
  • 46
  • mm.. In the application class I put every property that is common at everyone in the application... My value change foreach client... I am thinking it's better something like to claims.... but claims are lost at every request... – Simone Mar 17 '15 at 12:31
  • hm.. you are able to create request-related repository that will be created from IoC-container for each request with session data or cookie data as a parameter, please, correct me if i misunderstood your question. – Vladyslav Furdak Mar 17 '15 at 12:48
  • it's not request-related but client (or tenant)-related.. I would like to avoid session, but until now it seem it's the only solution... – Simone Mar 17 '15 at 12:57
  • So, why do not use the database approach ? – Vladyslav Furdak Mar 17 '15 at 13:01
  • What you mean? I have different tenant. Has been decided to have different database for each tenant. So, when user log-in I go in a common database to see which tenant he belongs to.. I read the correct connection string. Now I should use this connection string. If I pass it as parameter to methods, everything work. But I would like to avoid to pass the connection string in every method. – Simone Mar 17 '15 at 13:08
  • So you need to have some class in each application domain that is able to provide you with connectionString information. For example singleton that will visible in entire domain and it will be used when we need to create a repo class or take connectionString in each method, correct me if i'm wrong. – Vladyslav Furdak Mar 17 '15 at 13:19
  • Yes.. It would be perfect... but in this moment I do not have the concept of "application domain" in my application.. There is just one domain for each tenant.. so If I use a singleton, the connectionString is the same for each tenant – Simone Mar 17 '15 at 13:30
  • i mean .net app domain that is isolated scope for static classes, if you have asp.net web site that is app.domain, and if you have another console project that is another app.domain, so your static class will be visible in that scope, you can declare it in some VS project and referenced to it from another projects. – Vladyslav Furdak Mar 17 '15 at 13:38
  • So you can use singleton with methods that retrive connectionStrings and use it in entire application. – Vladyslav Furdak Mar 17 '15 at 14:44
  • Ok.. you are telling me that if I want more database I must have more domain... I cant have 1 domain -> N database... correct? – Simone Mar 17 '15 at 15:29
  • 1
    oh sorry, but i mean the following : http://msdn.microsoft.com/en-us/library/yb506139.aspx – Vladyslav Furdak Mar 17 '15 at 17:06
1

With so many discussions already done, I would like to share my thoughts based on my experience.

It is always advisable to get the connection string resolved at the repository layer for the client that is connecting to. This can be however cached within the application.

You will be having your business layer that contains all of your business entities that reside in the application and it will be accessible throughout all the layers of the application.

If the connection string resolution mechanism reside in this layer [of course cached], it will be a elegant design and easy to use.

Saravanan
  • 7,637
  • 5
  • 41
  • 72
0

Probably the best way for you is to have a single database where user-related tables have foreign keys to Users table.

You can learn more about multi-tenant architectures from this Microsoft article. Here is a whole topic about this subject: asp.net mvc multiple connection strings

Community
  • 1
  • 1
Bruniasty
  • 418
  • 5
  • 18
0

Why aren't you creating a ConnectionString repository in your app, and based on the logic you get the correct CS. Something like a lookup table, static. In case you'd like to modify the ConnectionStrings, you could use your app.config and get them from there anytime you need them.

brainfood
  • 219
  • 1
  • 9
  • I created a repository with all connectionStrings. The repository is inside a common database and everytime a client connect to the application I read all information about that client. But then I don't know where I have to store those information. For example the connection string. – Simone Mar 17 '15 at 11:13
  • Maybe use a singleton? – brainfood Mar 17 '15 at 11:27
  • I should instanciate my DbContext once, at the session start... mmm... I don't know.. I'm thinking.. – Simone Mar 17 '15 at 11:32
  • or a dbContext/client, or a lookup table stored in a singleton, any option is a viable solution – brainfood Mar 17 '15 at 12:39
  • Ok.. I have a lookup table, but how can I access the right row? With a Id for example.. Where do I take this Id? I have to pass this id at each method that call the database? So I can read the right row.. It's what I am trying to avoid... pass a value that is not strictly necessary to my business component just because I need to understand in which db I have to operate... – Simone Mar 17 '15 at 12:55