Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Global Objects
Message
De
04/09/2007 10:27:29
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
 
À
03/09/2007 15:05:33
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Divers
Thread ID:
01251812
Message ID:
01252191
Vues:
15
This message has been marked as a message which has helped to the initial question of the thread.
John,

>>Another part of your question mentioned Not wanting to instanitate the class every time you needed to access the settings data. There is a design pattern called the Singleton Design Pattern that addresses this issue also. Essentially the class itself can check to see if has already been instantiated before calling the constructor. I use a variation of what Rick mentioned where I have a settings class that uses the singelton design pattern. I instantiate the settings class from my main application object but can get a reference to it from anyother class as needed without creating another instance. Within this settings class all the settings properties are static and read from xml file or database or ?.
>>Tim
>
>More cool data.
>
>Taking me a bit to research the difference between when to use singleton and when to use static classes, but I'm getting it slowly.
>
>I have a number of cases where singleton may make sense for me, I will have to play with it some more.
>

I would be happy to provide you with a simple settings class that uses the singleton design pattern. I have later versions that use other storage medium and also keep the actually settings seperated out. This is the simple version. My Settings stuff is alll in its own library called SettingsLib. To use this you just do the following:
use the library

Settings.Load(); // Static method

And to access a setting
string listenIp = Settings.Instance.ListenerIp;
or
Settings settings = Settings.Instance;
string listenIp = settings.ListenerIp;


using System;
using System.IO;
using System.Xml;
using System.Drawing;
using System.Xml.Serialization;
using System.Diagnostics;
using EventLoggerLib;

namespace SettingsLib
{
public class Settings
{
#region Singleton

private static Settings instance = null;
public static Settings Instance
{
get
{
if(instance == null)
instance = new Settings();

return instance;
}
}

public Settings()
{
}

#endregion Singleton

public static void RestoreAllDefaults()
{
instance = new Settings();
}

//System
public bool EnableDataUpload = true;
public bool AutoStart = true;
public string ListenerIp = "192.168.101.53";
public string ServerIp = "192.168.101.10";
public bool ArchiveData = true;
public bool EnableDiagnostics = true;

// Specific Info
public string CompanyID = "WMT";
public string TruckID = "2416";

// Rfid
public bool RfidSerialConnect = true;
public int RfidComPort = 3;
public string RfidBaudRate = "57600";
public double RfidReadDelay = 0.1;
public string RfidIpAddress = "192.168.100.95";
public string RfidPort = "2101";

// Gps
public int GpsPort = 4;
public int GpsBaud = 4800;

// Scale
public bool UseReadTimeout = true;
public bool UseWriteTimeout = true;
public short ScaleReadTimeout = 500;
public short ScaleWriteTimeout = 500;

// Scale Port Settings
public int CurbScalePort = 1;
public int StreetScalePort = 2;
public string ScaleBaudRate = "9600";
public string ScaleDataBits = "8";
public string ScaleParity = "None";
public string ScaleStopBits = "1";
public string ScaleReadCommand = "(NL)";

#region Save / Load

public static void Save()
{
try
{
Settings.Serialize(Settings.Instance, "Settings.xml");
Debug.WriteLine("Saved", "Settings");
EventLogger.LogEvent("Saved", "Settings");
}
catch(Exception ex)
{
Debug.WriteLine(ex);
EventLogger.LogException(ex);
}
}

public static bool Load()
{
bool loaded = false;

try
{
if(File.Exists("Settings.xml"))
{
Settings.instance = (Settings)Settings.Deserialize("Settings.xml", typeof(Settings));
loaded = true;
Debug.WriteLine("Loaded", "Settings");
EventLogger.LogEvent("Loaded", "Settings");
}
}
catch(Exception ex)
{
Debug.WriteLine(ex);
EventLogger.LogException(ex);
}

return loaded;
}

private static void Serialize(Object obj, String fileName)
{
try
{
FileStream file = File.Create(fileName);
XmlSerializer xml = new XmlSerializer(obj.GetType());
xml.Serialize(file, obj);
file.Close();
}
catch(Exception ex)
{
Debug.WriteLine(ex);
EventLogger.LogException(ex);
}
}

private static object Deserialize(String fileName, Type objType)
{
object stuff = null;

try //to get data from XML file
{
FileStream file = File.Open(fileName, System.IO.FileMode.Open);
XmlSerializer xml = new XmlSerializer(objType);
stuff = xml.Deserialize(file);
file.Close();
}
catch(Exception ex)
{
Debug.WriteLine(ex);
EventLogger.LogException(ex);
}

return stuff;
}
#endregion Save / Load
}
}



Timothy Bryan
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform