CodeSmith Customization - Dynamic ConnectionString Login user based

Well finally I got through a way to modify the CodeSmith generated layers to support Dynamic Switching of ConnectionString based on the user logged in.
That means, if we want to allow few users to access a particular database, and other users to access some other database, here is what I did.....  


In Services Layer there is a Class "ConnectionScopeBase" which has this public property DataProvider.
I noticed this property was called whenever there was a database call. So I placed my logic into the "get" method of this property:


I already initialized a session variable when the user tends to login, and kept this user name in a separate text file, which is read when user clicks on Login button.


As soon as the username is matched with the one saved in textfile, the session object is initialized with the alternate connectionstring which I place again in text file....  :-)


so now, which ever database call comes in it verifies the session object, if exist then the Provider connectionstring is forced to pick from text file.....


Above all this did not affected the application in terms of performace.


Enjoy Coding.....

Comments

Popular posts from this blog

cshtml binding js function with boolean parameter using model

RDLC Error The report definition for report 'xxxxxx' has not been specified

Visual Studio vulnarable behaviour