Skip to content

Instantly share code, notes, and snippets.

Created November 22, 2014 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/aa104db19eb10cd7541a to your computer and use it in GitHub Desktop.
Save anonymous/aa104db19eb10cd7541a to your computer and use it in GitHub Desktop.
Structuremap Settings/App.config
using System;
using FubuCore;
using FubuCore.Binding;
using FubuCore.Binding.InMemory;
using FubuCore.Configuration;
using FubuCore.Conversion;
using FubuCore.Reflection;
using StructureMap;
using StructureMap.Configuration.DSL;
using StructureMap.Graph;
using StructureMap.Pipeline;
namespace ConsoleApplication1
{
/*
The App.config file;
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="TestSettings.CanDoX" value="true"/>
<add key="TestSettings.ValueY" value="100"/>
<add key="TestSettings.ValueZ" value="101.1"/>
<!-- Todo; write a typeconverter to read of the form; 1y3M1d10h15m10s -->
<add key="TestSettings.ValueA" value="10.13h"/>
<add key="TestSettings.ValueB" value="2014/10/12T15:23:45"/>
</appSettings>
</configuration>
*/
public class Program
{
public static void Main(string[] args)
{
var container = new Container(new TestRegistry());
Console.WriteLine("Settings;");
container.GetInstance<TestObject>();
Console.ReadKey();
}
}
public class TestSettings
{
public bool CanDoX { get; set; }
public int ValueY { get; set; }
public double ValueZ { get; set; }
public TimeSpan ValueA { get; set; }
public DateTime ValueB { get; set; }
}
public class TestObject
{
public TestObject(TestSettings testSettings)
{
Console.WriteLine("Boolean; {0}", testSettings.CanDoX);
Console.WriteLine("Integer; {0}", testSettings.ValueY);
Console.WriteLine("double; {0}", testSettings.ValueZ);
Console.WriteLine("TimeSpan; {0}", testSettings.ValueA);
Console.WriteLine("DateTime; {0}", testSettings.ValueB);
}
}
public class TestRegistry : Registry
{
public TestRegistry()
{
Scan(x =>
{
x.TheCallingAssembly();
x.WithDefaultConventions();
x.Convention<SettingsScanner>();
});
For<ISettingsProvider>().Use<AppSettingsProvider>();
For<IObjectResolver>().Use<ObjectResolver>();
For<IServiceLocator>().Use<StructureMapServiceLocator>();
For<IBindingLogger>().Use<NulloBindingLogger>();
For<IObjectConverter>().Use<ObjectConverter>();
For<ITypeDescriptorCache>().Use<TypeDescriptorCache>();
}
}
/// Original from https://github.com/structuremap/structuremap/blob/master/src/FubuMVC.StructureMap3/StructureMapServiceLocator.cs
/// To save pulling in MVC related code
public class StructureMapServiceLocator : IServiceLocator
{
private readonly IContainer container;
public StructureMapServiceLocator(IContainer container)
{
this.container = container;
}
public object GetInstance(Type type)
{
return container.GetInstance(type);
}
public IContainer Container { get { return container; } }
public TService GetInstance<TService>()
{
return container.GetInstance<TService>();
}
public TService GetInstance<TService>(string name)
{
return container.GetInstance<TService>(name);
}
}
/// Original from https://github.com/structuremap/structuremap/blob/master/src/FubuMVC.StructureMap3/SettingsScanner.cs
/// To save pulling in MVC related code
public class SettingsScanner : IRegistrationConvention
{
public static readonly Func<Type, bool> DefaultFilter = type => type.Name.EndsWith("Settings") && !type.IsInterface;
private readonly Func<Type, bool> filter;
public SettingsScanner()
: this(DefaultFilter)
{
}
public SettingsScanner(Func<Type, bool> filter)
{
this.filter = filter;
}
public void Process(Type type, Registry graph)
{
if (!filter(type))
{
return;
}
var instanceType = typeof(SettingsInstance<>).MakeGenericType(type);
var instance = Activator.CreateInstance(instanceType).As<Instance>();
graph.For(type).Add(instance).Singleton();
}
}
/// Original from https://github.com/structuremap/structuremap/blob/master/src/FubuMVC.StructureMap3/SettingsScanner.cs
/// To save pulling in MVC related code
public class SettingsInstance<T> : LambdaInstance<T> where T : class, new()
{
public SettingsInstance()
: base("Building {0} from application settings".ToFormat(typeof(T).FullName), c => c.GetInstance<ISettingsProvider>().SettingsFor<T>())
{
LifecycleIs<SingletonLifecycle>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment