public class ProductController : Controller { #region Constructor public ProductController() : this(new ProductModel()) { } public ProductController(IProductModel productModel) { if (productModel == null) throw new ArgumentNullException("productModel"); ProductModel = productModel; } private IProductModel ProductModel { get; set; } #endregion
The other way to achieve this is by defining CustomeControllerFactoryWithoutDefaultConstructor something like this
public class CustomeControllerFactoryWithoutDefaultConstructor : DefaultControllerFactory { protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) { return Activator.CreateInstance(controllerType, new ProductModel()) as IController; } }
And then register this in Global.asax:
ControllerBuilder.Current.SetControllerFactory(typeof(CustomeControllerFactoryWithoutDefaultConstructor));
No comments:
Post a Comment