Skip to content

Instantly share code, notes, and snippets.

@cammerman
Created March 1, 2011 03:53
Show Gist options
  • Save cammerman/848580 to your computer and use it in GitHub Desktop.
Save cammerman/848580 to your computer and use it in GitHub Desktop.
Configuration data class.
internal interface IConfigLoader
{
IConfigSettings LoadConfig();
}
internal class ConfigLoader : IConfigLoader
{
public ConfigLoader()
{
}
protected String ConfigFilePath()
{
return
Path.Combine(
Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location),
"settings.xml");
}
protected FileStream OpenConfigFile()
{
return
new FileStream(
ConfigFilePath(),
FileMode.Open,
FileAccess.Read,
FileShare.Read);
}
public IConfigSettings LoadConfig ()
{
var settings = null as IConfigSettings;
using (var fs = OpenConfigFile())
{
var xml = new XmlSerializer(typeof(ConfigSettings));
settings = xml.Deserialize(fs) as ConfigSettings;
}
return settings;
}
}
protected virtual Config.IConfigSettings ResolveConfigSettings(IComponentContext context)
{
return
context.Resolve<Config.IConfigLoader>()
.LoadConfig();
}
protected virtual void RegisterConfig(ContainerBuilder builder)
{
builder
.RegisterType<Config.ConfigLoader>()
.As<Config.IConfigLoader>()
.InstancePerLifetimeScope();
builder
.Register(ResolveConfigSettings)
.As<Config.IConfigSettings>()
.InstancePerLifetimeScope();
}
internal interface IConfigSettings
{
String GreetingFileName { get; }
Int32 WorkIntervalSeconds { get; }
}
[XmlRoot("GreeterSettings")]
public class ConfigSettings : IConfigSettings
{
public ConfigSettings ()
{
}
[XmlAttribute]
public String GreetingFileName
{
get;
set;
}
[XmlAttribute]
public Int32 WorkIntervalSeconds
{
get;
set;
}
}
internal class GreetService : ServiceBase
{
protected virtual IGreeter Greeter
{
get;
private set;
}
protected virtual ILogger Logger
{
get;
private set;
}
protected virtual Func<Owned<IGreetServiceWorker>> WorkerFactory
{
get;
private set;
}
public GreetService(IServiceNameProvider serviceNameProvider, ILogger logger, Func<Owned<IGreetServiceWorker>> workerFactory)
{
serviceNameProvider.ThrowIfNull("serviceNameProvider");
ServiceName =
serviceNameProvider.ServiceName
.ThrowIfNullOrEmpty("serviceNameProvider.ServiceName");
Logger = logger.ThrowIfNull("logger");
WorkerFactory = workerFactory.ThrowIfNull("workerFactory");
CanStop = true;
AutoLog = true;
}
protected IGreetServiceWorker CurrentWorker
{
get;
set;
}
protected override void OnStart(String[] args)
{
Logger.Message("Starting service.");
if (CurrentWorker == null)
{
try
{
CurrentWorker = WorkerFactory().Value;
CurrentWorker.Start();
Logger.Message("Service started.");
}
catch (Exception ex)
{
Logger.ExceptionAlone(ex);
}
}
else
{
Logger.Message("Cannot start service. Service is already running.");
}
}
protected override void OnStop()
{
Logger.Message("Stopping service.");
CurrentWorker.Stop();
CurrentWorker = null;
Logger.Message("Service stopped.");
}
}
<GreeterSettings
GreetingFileName="hola.txt"
WorkIntervalSeconds="5" />
internal interface IWorkInterval
{
Int32 IntervalSeconds { get; }
}
internal class ConfigWorkIntervalProvider
{
public Int32 IntervalSeconds
{
get;
private set;
}
public ConfigWorkIntervalProvider(Config.IConfigSettings settings)
{
IntervalSeconds = settings.WorkIntervalSeconds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment