internal ControllerBuilder ControllerBuilder { get { if (_controllerBuilder == null) { _controllerBuilder = ControllerBuilder.Current; } return _controllerBuilder; } set { _controllerBuilder = value; } }
原来在MvcHandler中创建控制器工厂并不是直接使用IControllerFactroy的实现,而是使用了ControllerBuilder这个对象,这个对象采用了单例模式的实现;MvcHandler通过ControllerBuilder对象获取到一个实例,然后通过ControllerBuilder创建出IControllerFactory实现,ControllerBuilder管理着IControllerFactory的创建过程。
关于ControllerBuilder里面的GetControllerFactory()方法的实现,我们不必细究,但是我们需要知道的是在MVC里面有一个默认的控制器工厂的实现类DefaultControllerFactory。我们来看看
IControllerFactory接口的定义:
public interface IControllerFactory { IController CreateController(RequestContext requestContext, string controllerName); SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName); void ReleaseController(IController controller); }
DefaultControllerFactory的定义:
public class DefaultControllerFactory : IControllerFactory { public virtual IController CreateController(RequestContext requestContext, string controllerName) { if (requestContext == null) { ); } if (String.IsNullOrEmpty(controllerName) && !requestContext.RouteData.HasDirectRouteMatch()) { ); } Type controllerType = GetControllerType(requestContext, controllerName); IController controller = GetControllerInstance(requestContext, controllerType); return controller; } ReleaseController(IController controller) { IDisposable disposable = controller as IDisposable; if (disposable != null) { disposable.Dispose(); } } //...... }
上述的两个方法CreateController()和ReleaseController()通过名字都可以很好理解,分别对应着创建控制器和释放控制器。
了解了上述MVC里面控制器工厂的实现细节,我们自己也来建一个自己的控制器工厂,不过为了简化,我们这里直接去new了一个工厂的实现类。先来看看我们Swift.MVC的控制器工厂。
控制器工厂接口:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Routing; namespace Swift.MVC { IControllerFactory { //创建控制器 IController CreateController(RequestContext requestContext, string controllerName); ReleaseController(IController controller); } }
控制器工厂实现类:
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Swift.MVC { public class SwiftControllerFactory:IControllerFactory { #region Public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName) { if (requestContext == null) { ); } if (string.IsNullOrEmpty(controllerName)) { ); } //得到当前的控制类型 Type controllerType = GetControllerType(requestContext, controllerName); if (controllerType == null) { return null; } //得到控制器对象 IController controller = GetControllerInstance(requestContext, controllerType); return controller; } ReleaseController(IController controller) { IDisposable disposable = controller as IDisposable; if (disposable != null) { disposable.Dispose(); } } #endregion #region Privates IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) { var oRes = Activator.CreateInstance(controllerType) as IController; return oRes; } Type GetControllerType(System.Web.Routing.RequestContext requestContext, string controllerName) { routeNamespaces; object routeAssembly; requestContext.RouteData.Values.TryGetValue(, out routeNamespaces); requestContext.RouteData.Values.TryGetValue(, out routeAssembly); type = Assembly.Load(routeAssembly.ToString()).GetType(routeNamespaces.ToString() + + controllerName + ); return type; } #endregion } }
这里博主主要用到了反射去实例化控制器实例。
2.2、控制器的父类实现上述介绍了控制器工厂的实现。除了控制器工厂,还有我们的控制器接口以及父类的相关实现。
控制器接口的定义: