class Given_the_SM_Configured_With_Func_Registered:nspec
{
void When_asked_to_resolve_the_same_Type_two_times()
{
it["Should give the same instance"] = () =>
{
IContainer Container = new Container();
Container.Configure(p => p.AddRegistry(new RavenDBRegistry()));
var nested = Container.GetNestedContainer();
var documentSession = nested.GetInstance<IDocumentSession>();
var SecondDocumentSession = nested.GetInstance<IDocumentSession>();
documentSession.should_be(SecondDocumentSession);
};
}
} public class StructureMapControllerActivatorSpec:nspec
{
void Given_the_StructureMap_Configured_and_given_to_Activator()
{
it["Should be able to Create a Controller with the Nested Type"] = () =>
{
HttpRequestMessage message = new HttpRequestMessage();
IHttpControllerActivator activator=new StructureMapControllerActivator(TestInstance.ContainerSetUp.Configure());
IHttpController controller=activator.Create(message, null, typeof(OrderController));
controller.should_cast_to<OrderController>();
};
}
}public class StructureMapControllerActivator:IHttpControllerActivator
{
private readonly IContainer container;
public StructureMapControllerActivator(IContainer structureMapContainer)
{
this.container = structureMapContainer;
}
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
var NestedContainer = container.GetNestedContainer();
request.RegisterForDispose(NestedContainer);
// Not used as Operator because it may return Null , Well You cant return a Null Controller
// That will be a disaster
return (IHttpController)container.GetInstance(controllerType) ;
}
} public class RavenDBRegistry:Registry
{
public RavenDBRegistry()
{
var documentStore = new EmbeddableDocumentStore
{
RunInMemory = true
}.Initialize();
For<IDocumentSession>().Use(() => documentStore.OpenSession());
For<IDocumentStore>().Singleton().Use(documentStore);
}
}