.Net Code Monkey RSS 2.0
 Saturday, April 12, 2008

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...

Saturday, April 12, 2008 7:03:24 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.Net | Asp.Net | C# | Config Files | XML
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
Archive
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
Blogroll
 Clemens Vasters
 Harry Pierson
Passion * Technology * Ruthless Competence
 Joshua Flanagan
A .NET Software Developer
 Michael Schwarz's Blog
Developing applications on the Microsoft platform since Windows 3.1!
 Omar Shahine
Yet another Microsoft blogger
 Scot GU
Scott Guthrie lives in Seattle and builds a few products for Microsoft
 Scott Hanselman
Programming Life and the Zen of Computers
 Tom Mertens
Tom's corner
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Duane Wingett
Sign In
Statistics
Total Posts: 17
This Year: 17
This Month: 2
This Week: 0
Comments: 2
Themes
Pick a theme:
All Content © 2008, Duane Wingett
DasBlog theme 'Business' created by Christoph De Baene (delarou)