neeebzz
neeebzz

Reputation: 11538

How can I share Configuration Settings across multiple projects in Visual Studio?

I have a Visual Studio web application solution. I have three projects as UserInterface, BusinessLogic and DataAccess.

I had to store some user defined settings and I created configSections in the config file.

I access these configSections through classes which inherit from .NET's ConfigurationSection base class.

So in short for every project I had a separate configSection and for that corresponding configSection I had a class in that project inheriting from ConfigurationSection to access the config section settings.

This works all sweet. But the problem arises if there is any setting which I need to use across multiple projects. So If I need to use a setting defined in UserInterface project configSection in, let say, BusinessLogic project I have to actually make a copy of that setting in the BusinessLogic's configSection. This ends up having the same setting copied across multiple configSections.

Isn't this a bit too redundant?

Upvotes: 10

Views: 9019

Answers (3)

Andy Braham
Andy Braham

Reputation: 10163

There is a much better way of doing this using "Shared Projects" see my Answer on a very similar question here.

Upvotes: 1

user1228
user1228

Reputation:

Never actually done this, but in theory it might work...

When you define your custom configuration section, set its configSource to an external file (whatever.config). This external file should be added to the SOLUTION and not the project. It will appear under "Solution Items". In each project, Add an Existing File, browse to whatever.config, click the dropdown on the Add button and select "Add as Link."

Whatever.config will be a single file you can edit under Solution Items, and it gets copied into each application at compile time.

Upvotes: 2

ChrisF
ChrisF

Reputation: 137118

Using your example:

Just create the setting in the Business Logic project and then expose a Getter to the User Interface project.

Then the UI can query the BL for the value. Your configuration setting is only in one place - the lowest level it can be.

However, if you replace a lower level project with a new one you'll have to make sure that the setting is replicated too. This is only likely to be an issue if the setting is in the Data Access level as that's the one most likely to get changed (different database provider for example).

Upvotes: 0

Related Questions