Created
October 15, 2010 04:02
-
-
Save jakcharlton/627595 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ControllerFactory : IControllerFactory | |
{ | |
readonly IWindsorContainer container; | |
public ControllerFactory() : this(((IContainerAccessor) HttpContext.Current.ApplicationInstance).Container) | |
{ | |
} | |
public ControllerFactory(IWindsorContainer container) | |
{ | |
if (container == null) | |
throw new ArgumentNullException("container", "Windsor Container cannot be null"); | |
this.container = container; | |
} | |
#region IControllerFactory Members | |
public virtual IController CreateController(RequestContext context, string controllerName) | |
{ | |
string baseNs = typeof (BaseController).Namespace; | |
string action = context.RouteData.GetRequiredString("action").ToLower() + "controller"; | |
try | |
{ | |
var controller = | |
(IController) container.Resolve(baseNs.ToLower() + "." + controllerName.ToLower() + "." + action); | |
return controller; | |
} | |
catch (ComponentNotFoundException ex) | |
{ | |
context.HttpContext.Response.Redirect("/PageNotFound/?url=" + context.HttpContext.Request.Url); | |
} | |
return null; | |
} | |
public void ReleaseController(IController controller) | |
{ | |
var disposable = controller as IDisposable; | |
if (disposable != null) | |
{ | |
disposable.Dispose(); | |
} | |
container.Release(controller); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment