Skip to content

Instantly share code, notes, and snippets.

@chadmyers
Forked from jmarnold/SMBug.cs
Created June 28, 2011 21:47
Show Gist options
  • Save chadmyers/1052324 to your computer and use it in GitHub Desktop.
Save chadmyers/1052324 to your computer and use it in GitHub Desktop.
SM Bug
using System;
using System.Diagnostics;
using System.Linq;
using StructureMap;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
ObjectFactory
.Initialize(x =>
{
x.ForSingletonOf<ModelBinderCache>();
x.For<IModelBinderCache>()
.Singleton()
.Use<ModelBinderWrapper>();
x.For<IDebugReport>().HybridHttpOrThreadLocalScoped().Use<DebugReport>();
});
var container = ObjectFactory.Container;
var allSingletons = container.Model.PluginTypes.Where(x => x.Lifecycle == InstanceScope.Singleton.ToString());
Console.WriteLine("Found singletons: " + allSingletons.Count());
foreach (var pluginType in allSingletons)
{
var instance = container.GetInstance(pluginType.PluginType);
Console.WriteLine("Initialized singleton in primary container: " + instance);
}
var report = ObjectFactory.GetInstance<IDebugReport>();
using(var ctx = ObjectFactory.Container.GetNestedContainer())
{
ctx.Configure(x => x.For<IDebugReport>().Use(report));
Console.WriteLine(report.Id);
ctx
.GetInstance<RecordingObjectResolver>()
.test();
Console.ReadLine();
}
}
}
public interface IModelBinderCache
{
void test();
}
public class ModelBinderCache : IModelBinderCache
{
public void test()
{
}
}
public class ModelBinderWrapper : IModelBinderCache
{
private readonly Func<IDebugReport> _report;
private readonly ModelBinderCache _inner;
public ModelBinderWrapper(Func<IDebugReport> report, ModelBinderCache inner)
{
_report = report;
_inner = inner;
}
public void test()
{
Console.WriteLine(_report().Id);
_inner.test();
}
}
public interface IDebugReport
{
Guid Id { get; }
}
public class DebugReport : IDebugReport
{
public DebugReport()
{
Id = Guid.NewGuid();
}
public Guid Id { get; private set; }
public override string ToString()
{
return Id.ToString();
}
}
public interface IObjectResolver
{
void test();
}
public class ObjectResolver : IObjectResolver
{
private readonly IModelBinderCache _cache;
public ObjectResolver(IModelBinderCache cache)
{
_cache = cache;
}
public void test()
{
_cache.test();
}
}
public class RecordingObjectResolver : IObjectResolver
{
private readonly IDebugReport _report;
private readonly ObjectResolver _resolver;
public RecordingObjectResolver(IDebugReport report, ObjectResolver resolver)
{
_report = report;
_resolver = resolver;
}
public void test()
{
_resolver.test();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment