Last active
September 23, 2015 17:08
-
-
Save rarous/587632 to your computer and use it in GitHub Desktop.
Minimalistická ukázka integrace Windsor Containeru do webové aplikace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ControllersInstaller : IWindsorInstaller { | |
public void Install(IWindsorContainer container, IConfigurationStore store) { | |
container.Register( | |
Classes. | |
FromThisAssembly(). | |
BasedOn<IController>(). | |
LifestyleTransient(), | |
Component. | |
For<IKernel>(). | |
Instance(container.Kernel), | |
Component. | |
For<IControllerFactory>(). | |
ImplementedBy<WindsorControllerFactory>()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Global : HttpApplication { | |
static IWindsorContainer container; | |
protected void Application_Start() { | |
container = BootstrappContainer(); | |
RegisterControllerFactory(container); | |
} | |
protected void Application_Error() { | |
var log = container.Resolve<ILogger>(); | |
log.Fatal("Application Error", Server.GetLastError()); | |
} | |
protected void Application_End() { | |
DisposeContainer(container); | |
} | |
public static IWindsorContainer BootstrappContainer() { | |
return new WindsorContainer(). | |
AddFacility<LoggingFacility>(f => f. | |
LogUsing(LoggerImplementation.Log4net).WithAppConfig()). | |
Install(FromAssembly.This()); | |
} | |
public static void DisposeContainer(IWindsorContainer container) { | |
if (container != null) | |
container.Dispose(); | |
} | |
public static void RegisterControllerFactory(IWindsorContainer container) { | |
var controllerFactory = container.Resolve<IControllerFactory>(); | |
if (controllerFactory == null) | |
return; | |
ControllerBuilder.Current.SetControllerFactory(controllerFactory); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WindsorControllerFactory : DefaultControllerFactory { | |
readonly IKernel kernel; | |
public WindsorControllerFactory(IKernel kernel) { | |
if (kernel == null) | |
throw new ArgumentNullException("kernel", "kernel is null."); | |
this.kernel = kernel; | |
Log = NullLogger.Instance; | |
} | |
public ILogger Log { get; set; } | |
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { | |
if (controllerType == null) | |
LogAndThrowNotFound(requestContext); | |
return kernel.Resolve(controllerType) as IController; | |
} | |
void LogAndThrowNotFound(RequestContext requestContext) { | |
Log.Warn(String.Format("Not found: {0}", requestContext.HttpContext.Request.Url)); | |
throw new HttpException(404, "Not found"); | |
} | |
public override void ReleaseController(IController controller) { | |
kernel.ReleaseComponent(controller); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment