Skip to content

Instantly share code, notes, and snippets.

@cammerman
Created November 16, 2010 04:46
Show Gist options
  • Save cammerman/701436 to your computer and use it in GitHub Desktop.
Save cammerman/701436 to your computer and use it in GitHub Desktop.
Setting provider types for injecting simple setting values
namespace Bootstrap
{
using Autofac;
using Autofac.Builder;
using System.Reflection;
public static class AutofacBootstrap
{
public static IContainer BootstrapIoC()
{
var builder = new ContainerBuilder();
// Register providers.
builder
.RegisterAssemblyTypes(Assembly.GetCallingAssembly())
.InNamespace("MyApp.SettingProviders")
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
// Register caching services.
builder
.RegisterType<WorkingSetCache>()
.InstancePerLifetimeScope();
builder
.RegisterType<AcquisitionCache>()
.InstancePerLifetimeScope();
return builder.Build();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Caching
{
class AcquisitionCache
{
protected virtual String StoragePath
{
get;
private set;
}
protected virtual Int32 MaxInMemory
{
get;
private set;
}
public AcquisitionCache(AcquisitionCacheStoragePath basePath, AcquisitionCacheMaxInMemory sizeLimit)
{
StoragePath =
basePath.ThrowIfArgumentIsNullOrEmpty("basePath")
.StoragePath;
MaxInMemory =
sizeLimit.ThrowIfArgumentIsNegative("sizeLimit")
.MaxSize;
}
// Cache implementation ...
}
class WorkingSetCache
{
protected virtual String StoragePath
{
get;
private set;
}
protected virtual Int32 MaxInMemory
{
get;
private set;
}
public WorkingSetCache(WorkingSetCacheStoragePath basePath, WorkingSetCacheMaxInMemory sizeLimit)
{
StoragePath =
basePath.ThrowIfArgumentIsNullOrEmpty("basePath")
.StoragePath;
MaxInMemory =
sizeLimit.ThrowIfArgumentIsNegative("sizeLimit")
.MaxSize;
}
// Cache implementation ...
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SettingProviders
{
class AcquisitionCacheStoragePath
{
protected virtual IAppSettings Settings
{
get;
private set;
}
public AcquisitionCacheStoragePath(IAppSettings settings)
{
Settings = settings.ThrowIfArgumentIsNull("settings");
}
public virtual String StoragePath
{
get { Settings.AcquisitionCacheDiskPath }
}
}
class AcquisitionCacheMaxInMemory
{
protected virtual IAppSettings Settings
{
get;
private set;
}
public AcquisitionCacheMaxInMemory(IAppSettings settings)
{
Settings = settings.ThrowIfArgumentIsNull("settings");
}
public virtual Int32 MaxSize
{
get { Settings.AcquisitionCacheMaxSize; }
}
}
class WorkingSetCacheStoragePath
{
protected virtual IAppSettings Settings
{
get;
private set;
}
public WorkingSetCacheStoragePath(IAppSettings settings)
{
Settings = settings.ThrowIfArgumentIsNull("settings");
}
public virtual String StoragePath
{
get { Settings.WorkingSetCacheDiskPath }
}
}
class WorkingSetCacheMaxInMemory
{
protected virtual IAppSettings Settings
{
get;
private set;
}
public WorkingSetCacheMaxInMemory(IAppSettings settings)
{
Settings = settings.ThrowIfArgumentIsNull("settings");
}
public virtual Int32 MaxSize
{
get { Settings.WorkingSetCacheMaxSize; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment