views:

25

answers:

1

Hi, I have a ASP.NET MVC application which is build up as an assembly that queries the database and a asp.net frontend that references this assembly and this assembly abstracts the underlying database. This means that my Assembly contains a app.config file that contains the connectionstring to the database (Linq to Sql data model). How do I go about making this more flexible? Should i make a "initialize()" method somewhere in my assembly which takes the connection string from the asp.net mvc application and then that controls which database to use? or how is this done?

A: 

You can create a setting for that assembly in the project properties. However, you really shouldn't have things configured statically like that. It should be possible to create two systems of objects from your assembly that each point toward a different database without either affecting the other.

The only reason that its configured statically is that thats what linq to sql does. Now i move from a development environment to a production environment and want to make a more suitable and sustainable way of doing this. Pls note that i want to deploy it multiple times (sell it to different customers) so it should be relatively easy to reconfigure.
H4mm3rHead
If that's really the way that LINQ to SQL works, then it is an argument against using it. However, since you are already, this link (http://goneale.com/2009/03/26/untie-linq-to-sql-connection-string-from-application-settings/) implies it's already configurable.
As far as I can read its the same rubbish... its still stored in the app.config and that is compiled into the assembly - right? thus i need to recompile every time before deploying to a new customer? Doesnt seem very efficient!
H4mm3rHead
When creating a context, call the constructor that takes a connection string. The no-arg constructor pulls a connection string from the app/web.config and is not compiled into the assembly, you can change that without recompiling though.
nos
@nos: When i compile my application i get no app.config to change. In my bin folder i get my assembly.dll file that connects to my db. But no app.config to read change my connection string in? Could i just add one? how about the "old" one compiled into the assembly.dll?
H4mm3rHead
For a normal app, you'd have to use the app.config of the exe file, not the one for the .dll. For a web app, you use the web.config , any assemblies your web app links to will use web.config. The only thing hard coded in the assembly is the name of the particular connection string and a default connection string if one cannot be found in web.config
nos