SportsStore is a master of ASP. the MVC project demonstrated in NET MVC3 framework (Third edition) covers many aspects of MVC, including: use DI container, URL optimization, navigation, paging, shopping cart, order, product management, Image Upload ...... it is a good MVC practice project, but it is not developed under a multi-layer framework, and there is still a distance from the real project. This series will try to implement the SportsStore project in a multi-layer framework and implement some functions in its own way.
This is the third article in the series, including:
■ 5. Custom Ninject controller Factory
■ 6. first run of the project
5. Custom Ninject controller Factory
Add the following reference under MySportsStore. WebUI:
● Add a reference to Ninject
● Add reference to MySportsStore. IBLL
● Add reference to MySportsStore. BLL
● Add reference to MySportsStore. Model
Create NinjectControllerFactory:
using System.Web.Mvc;using MySportsStore.BLL;using MySportsStore.IBLL;using Ninject;namespace MySportsStore.WebUI.Extension{ public class NinjectControllerFactory : DefaultControllerFactory { private IKernel ninjectKernel; public NinjectControllerFactory() { ninjectKernel = new StandardKernel(); AddBindings(); } protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, System.Type controllerType) { return controllerType == null ? null : (IController) ninjectKernel.Get(controllerType); } private void AddBindings() { ninjectKernel.Bind<IProductService>().To<ProductService>(); } }}
Register NinjectControllerFactory globally
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { ...... ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); } }
Why do you need NinjectControllerFactory?
The Ninject DI container can help us well manage interfaces and implementations, and inject them into the controller in the form of attributes or constructor, so as to call methods of interface implementation classes. In addition, Ninject provides the Get () method so that Ninject can also be used to create a Controller.
6. The first running of the project
Create a BaseController with a mechanism for manual garbage collection.
using System;using System.Collections.Generic;using System.Web.Mvc;namespace MySportsStore.WebUI.Controllers{ public class BaseController : Controller { protected IList<IDisposable> DisposableObjects { get; private set; } public BaseController() { this.DisposableObjects = new List<IDisposable>(); } protected void AddDisposableObject(object obj) { IDisposable disposable = obj as IDisposable; if (disposable != null) { this.DisposableObjects.Add(disposable); } } protected override void Dispose(bool disposing) { if (disposing) { foreach (IDisposable obj in this.DisposableObjects) { if (null != obj) { obj.Dispose(); } } } base.Dispose(disposing); } }}
When other controllers are derived from BaseController, if XXXService of the IXXXService type is used, the XXXService is put into the DisposableObjects set attribute of BaseController through AddDisposableObject (object obj) of BaseController, use Dispose () to destroy these xxxservices.
The BaseService also provides a manual garbage collection mechanism to recycle CurrentRepository in a timely manner.
Finally, the manual garbage collection mechanism is also provided in BaseRepository to recycle EF context in a timely manner.
Create a ProductController that is derived from BaseController:
using System.Web.Mvc;using MySportsStore.IBLL;using Ninject;namespace MySportsStore.WebUI.Controllers{ public class ProductController : BaseController { [Inject] IProductService ProductService { get; set; } public ProductController() { this.AddDisposableObject(ProductService); } public ViewResult List() { return View(ProductService.LoadEntities(p => true).AsQueryable()); } }}
The Prduct/List. cshtml view is:
@model IEnumerable<MySportsStore.Model.Product>@{ ViewBag.Title = "List"; Layout = "~/Views/Shared/_Layout.cshtml";}@foreach (var item in Model){ <div class="item">
To enable EF to automatically create and display databases during the first running, we also need to configure the connection string in Web. config in MySportsStore. WebUI:
<connectionStrings> ...... <add name="conn" connectionString="Data Source=.;User=some user name;Password=some password;Initial Catalog=MySportsStore;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings>
Modify the default route:
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Product", action = "List", id = UrlParameter.Optional } );
Run the command to obtain the following interface:
The MySportsStore database is also added to the database:
The series "MVC project practices, implementing SportsStore in a three-tier architecture" includes:
MVC project practice, under the three-tier architecture to achieve SportsStore-01, EF Code First modeling, DAL layer MVC project practice, under the three-tier architecture to achieve SportsStore-02, DbSession layer, BLL layer MVC project practice, in the three-tier architecture to achieve SportsStore-03, Ninject controller factory MVC project practice, in the three-tier architecture to achieve SportsStore-04, achieve paging MVC project practice, in the three-tier architecture to achieve SportsStore-05, implement the MVC project practice of navigation, implement SportsStore-06 under the three-tier architecture, and implement shopping cart