, ASP.NET MVC StructureMap. ( , .)
factory, . ( factory, ASP.NET MVC, )
, MyController , MyManager ( factory)
public class MyController : Controller
{
private readonly ISomeService _someService;
public MyController(ISomeService someService){
_someService = someService;
}
}
ASP.NET MVC, factory ( StructureMapControllerFactory).
protected void Application_Start()
{
Bootstrapper.ConfigureStructureMap();
ControllerBuilder.Current.SetControllerFactory
(new StructureMapControllerFactory());
RegisterRoutes(RouteTable.Routes);
}
StructureMapControllerFactory ObjectFactory.GetInstance, ( , )
public class StructureMapControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(Type controllerType)
{
if (controllerType == null) return null;
try
{
return ObjectFactory.GetInstance(controllerType) as Controller;
}
catch (StructureMapException)
{
System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
throw;
}
}
}
, , , , .
, - , .
public static class Bootstrapper
{
public static void ConfigureStructureMap()
{
ObjectFactory.Initialize(
x => x.AddRegistry(new MyApplicationRegistry()));
}
}
public class MyApplicationRegistry : Registry
{
public MyApplicationRegistry()
{
ForRequestedType<ISomeService>()
.CacheBy(InstanceScope.Your_Choice_Here)
.TheDefault.Is.OfConcreteType<SomeService>();
}
}
. StructureMap InstanceScope.