Re: Instance vs Static + Option
Davy J <[email protected]> Fri, 9 May 2008 12:06:38 +0200
| Newsgroups | gmane.comp.windows.devel.dotnet.clr |
|---|---|
| Message-ID | <[email protected]> |
I've been using a proxy pattern for stuff like this, I tried using
Spring.Net to do it, but what a nightmare to configure for just one or
two objects.
here's a quick and dirty example
hth. Davy J
public interface ISettings
{
object ReadValue(string name);
void WriteValue(string name, object value);
}
/// <summary>
/// Public class that is used to read the settings from, not there
is no real implementation in this class
/// it serves as a proxy class.
/// </summary>
public class Settings : ISettings
{
private ISettings proxy;
#region ISettings Members
private void CheckProxy()
{
if (proxy == null)
{
switch (Properties.Settings.Default.CodeVersion)
{
case "DEBUG":
this.proxy = new TestSettings();
return;
default:
this.proxy = new LiveSettings();
return;
}
}
}
public object ReadValue(string name)
{
CheckProxy();
return proxy.ReadValue(name);
}
public void WriteValue(string name, object value)
{
CheckProxy();
proxy.WriteValue(name,object);
}
#endregion
}
/// <summary>
/// Application class.
/// </summary>
public class DemoClass
{
//We create a new class and don't worry about what we will be attacking.
private Settings settings = new Settings();
private string connectionString = string.Empty;
public DemoClass()
{
//We get a connection string from the class.
connectionString = (string)settings.ReadValue("ConnectionString");
}
}
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com