I'm sure many of you are aware that you can override the connection string settings in your DataAccess layer of your web application with connection string settings in your Web.Config. We found this out recently and found it very useful in an intranet application at work recently. However this week one of the other developers required that he could override a setting the BusinessEntity layer of his application from the App.Config in the console layer.
A quick chat with Mr Google didn't turn up a solution for this, so with this in mind all three of us quickly tried to return a working solution. I'm going to try to give an example of what we did, incase it something others may find of use. First we create a solution with two class library projects within it. The first will simulate the DataAccess layer and the second will simulate the BusinessEntity layer in an application. In the BusinessEntity project we will create a reference to the DataAccess project.
Starting with the DataAccess project we will add an App.Config file by creating a setting called "MySetting" in the Settings tab of the project Properties. The setting scope will be set to Application.

This creates a new sectionGroup called applicationSettings and a new section in the App.Config file and places the new setting in their too.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Playground.ConfigurationOverride.DataAccess.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Playground.ConfigurationOverride.DataAccess.Properties.Settings>
<setting name="MySetting" serializeAs="String">
<value>Setting in DataAccess</value>
</setting>
</Playground.ConfigurationOverride.DataAccess.Properties.Settings>
</applicationSettings>
</configuration>
Next we will add a simple class to the DataAccess project with a single static method that will return the value of the setting we have just created.
using System;
namespace Playground.ConfigurationOverride.DataAccess
{
public static class SettingsHelper
{
public static string GetMySetting()
{
return Properties.Settings.Default.MySetting;
}
}
}
Next we will create anothe simple class, SettingsGetter, in the but in the BusinessEntity layer this time. It will just return the value returned from the SettingsHelper class in the DataAccess layer.
using System;
namespace Playground.ConfigurationOverride.BusinessEntity
{
public static class SettingsGetter
{
public static string GetDataAccessSettingValue()
{
return DataAccess.SettingsHelper.GetMySetting();
}
}
}Next we need to create a presentation layer. So first off we will create a small web application and add it to the solution, and grab a reference to the
BusinessEntity layer. Set the web site as the startup project. We will also add a
Label to the default webform and a
Button. In the
Page_Load handler method populate the label text from the business entity method.
protected void Page_Load(object sender, EventArgs e)
{
uxSettingsLabel.Text =
Playground.ConfigurationOverride.BusinessEntity.SettingsGetter.GetDataAccessSettingValue();
}
...and run the application, and allow the Web.Config file to be created. The value of the setting in the DataAccess App.Config file should be displayed.
More to follow soon...