Reputation: 1507
For a school project we are trying to get our ASP.net project as modular as we can. We want to split up our Web.config so that we have separate files to store our database queries in. One for each "model" to be exact.
I tried to look on the internet for more information about this but I could not really find any good answer to this question that I understood.
So how can we split up queries into separate config files and access them?
Upvotes: 0
Views: 316
Reputation: 42343
You can't merge multiple web.configs like this at runtime. You could possibly merge some files at build time with a post build action. If you really wanted to go this route, you'd be better using your own config files instead of web.config and load them within your application. That said - why do you need separate files? Can't you just group them up in the web.config?
Anyway, as others have hinted, this is probably the wrong place to be storing SQL queries. I would query this with your tutor. The usual place for SQL is the data access classes or Stored Procedures (personally I dislike these, as testing newer versions of the software side-by-side becomes difficult if you have logic in the database).
Upvotes: 0
Reputation: 745
First of all I have to back up what others have said, you really should not be storing queries in your web.config, they should be hardcoded in the apps if you aren't using an ORM.
If you really want to look into having the queries in files and modular files you could look into using resource files. Now these should be used for localisations and things that are considered "resources" however there is no reason why you couldn't put queries into them.
This way you could have one resource file per module. Which is modular as you want. So given your example you would have a resources file called recipes. Then you would have a key of SelectOwn and the value is then your query. You can then access this in your code behind by saying Recipies.SelectOwn which at least is a nicer API.
I would though double check your home work, is there a reason you can't put queries in your code? Even as constant strings?
Upvotes: 1
Reputation: 119
If you organize your pages into folders you can have a web.config file in each folder. All pages in that folder will be able to use that web.config file.
Upvotes: 0