Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a project which requires connection to SQL servers. The problem is that the library needs to run at 2 different locations and needs to access two different data managers. The method names inside both data managers are the same but I have no idea how to implement this.

Could some one give me an example.

Detailed explanation :

I have a project : MyProject

There are two different data manager classes(in separate dlls) both named "DataManager"(not sure if this makes a difference)

1. DataManager(with methods named getnames(), getdata() ....) -- has code to access SQL servers in London.

2. DataManager(with methods named getnames(), getdata() ....) -- has code to access SQL servers in Paris.

I do not want to produce 2 separate dlls of MyProject (one with Paris DataManager referenced and one with London DataManager referenced). I cannot change the code of either DataManager but I am free to modify MyProject.

PS : The method names to access the data are exactly the same in both classes.
Posted

The obvious solution is to have these DataManager classes pick up different ConnectionStrings from the config file. You can have multiple connection strings in there so it would seem that this would be a simple solution.
 
Share this answer
 
Provided they are in separate DLL's - which they are - it shouldn't be a problem: they will almost certainly have different namespaces already. So all you have to do is use the fully qualified name in each case when you reference them:
C#
var lon = London.DataManager.getnames();
var par = Paris.DataManager.getnames();
Or
C#
London.DataManager lon = new London.DataManager();
Paris.DateManager par = new Paris.DataManager();
var lonnames = lon.getnames();
var parnames = par.getnames();
 
Share this answer
 
yes Sir Pete O'Hanlon you right said. It's possible you can multiple connection hold in the web config file. like that
XML
<connectionstrings>
   <add name="London" connectionstring="(Paste your connection Properties)Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providername="System.Data.SqlClient" />
 </connectionstrings>


another is mention below as per your problem.
XML
<connectionstrings>
   <add name="Paris" connectionstring="(Paste your connection Properties)Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providername="System.Data.SqlClient" />
 </connectionstrings>


I hope your problem has been solved,hit to reply for more information.
Happy Coding ☺
 
Share this answer
 
Comments
rohith naik 4-Feb-14 9:56am    
hi, the problem is that all connection strings are with in the dataManager class. I cannot use them. All I can use in myProject is DataManager.GetNames(). The programs has to automatically use the appropriate dll depending on the location.
CHill60 4-Feb-14 16:19pm    
You could write a "wrapper" class that has an internal object cast to the appropriate type (London.DataManager or Paris.DataManager) at runtime. I don't think you can use inheritance per se (I may be wrong) so your wrapper class would have to "pass through" all of the calls to the instance of the DataManager it's created. The comments about refactoring I definitely agree with ... if you get the chance
Ideally, there would be only one set of code and you would simply instantiate with whichever connection information you need at the time.
Failing that, they could both implement an Interface and you would simply reference the Interface; the DataManagers could then even be plug-ins.
As that doesn't seem to be the case, you could encapsulate them in other classes that implement an interface.

I recommend refactoring to remove duplicated code when convenient.
 
Share this answer
 
Comments
rohith naik 7-Feb-14 8:54am    
Thnaks man - I was thinking of using the same thing. In fact already did it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900