Sportsstore is the MVC project demonstrated in the master ASP. NET MVC3 Framework (third edition), which covers many aspects of MVC, including: Using di containers, URL optimization, navigation, paging, shopping carts, orders, product management, image uploads ... is a good MVC practice project, but the project is not developed in a multi-layered framework, and there is a distance from the real project. This series will attempt to implement the Sportsstore project in a multi-layered framework and implement some functions in its own way.
This article is the third series, including:
5. Custom Ninject Controller Factory
6, the first operation of the project
Add the following references under Mysportsstore.webui:
Add a reference to Ninject
Add a reference to MYSPORTSSTORE.IBLL
Add a reference to MYSPORTSSTORE.BLL
Add a reference to Mysportsstore.model
Create Ninjectcontrollerfactory:
UsingSYSTEM.WEB.MVC;UsingMYSPORTSSTORE.BLL;UsingMYSPORTSSTORE.IBLL;UsingNinject;Namespacemysportsstore.webui.extension{PublicClassninjectcontrollerfactory:defaultcontrollerfactory {PrivateIkernel Ninjectkernel;public Ninjectcontrollerfactory () {Ninjectkernel = new Standardkernel (); Addbindings (); } protected override IController getcontrollerinstance (System.Web.Routing.RequestContext requestcontext, System.Type controllertype) { Span style= "COLOR: #0000ff" >return controllertype = = null? null: (IController) Ninjectkernel.get (Controllertype);} private void Addbindings ( ) {ninjectkernel.bind<iproductservice> (). To<productservice> ();}}
Registering ninjectcontrollerfactory in the global
Class MvcApplication:System.Web.HttpApplication { void Application_Start () {... ControllerBuilder.Current.SetControllerFactory (new ninjectcontrollerfactory ());}}
Why do I need ninjectcontrollerfactory?
Ninject This di container can help us manage interfaces and implementations well, and inject them into the controller as attributes or constructors to invoke the methods of the interface implementation class. Also, Ninject provides a get () method that allows you to create a controller using Ninject.
6, the first operation of the project
Create Basecontroller, which gives you a mechanism for manual garbage collection.
UsingSystem;UsingSystem.Collections.Generic;UsingSYSTEM.WEB.MVC;Namespacemysportsstore.webui.controllers{PublicClassBasecontroller:controller {Protected ilist<idisposable> Disposableobjects {GetPrivateSet; }PublicBasecontroller () {This. Disposableobjects =New list<idisposable>(); }Protectedvoid Adddisposableobject (Objectobj) {IDisposable disposable = objAsIDisposable;if (disposable! =null) {this. Disposableobjects.add (disposable); }} protected override void Dispose (bool disposing) { If (disposing) {foreach (IDisposable obj in thisif (null! = obj) {obj. Dispose (); }}} base
When the other controller is derived from Basecontroller, if a xxxservice of type ixxxservice is used, the adddisposableobject of the Basecontroller (object obj Put the Xxxservice in the Disposableobjects collection attribute in Basecontroller and destroy these Xxxservice using Dispose ().
In the Baseservice also provides the manual garbage collection mechanism, can recover the currentrepository in time.
Finally, a manual garbage collection mechanism is also provided in Baserepository, which can be used to reclaim EF context in a timely manner.
Create a Productcontroller to derive it from Basecontroller:
UsingSYSTEM.WEB.MVC;UsingMYSPORTSSTORE.IBLL;Using Ninject; namespace mysportsstore.webui.controllers{public class Productcontroller: Basecontroller {[Inject] iproductservice productservice {get; set;} public Productcontroller () { thispublic ViewResult List () { return View (productservice.loadentities (p = true< Span style= "color: #000000"). AsQueryable ()); } }}
The corresponding prduct/list.cshtml views are:
@{Viewbag.title = "list "; Layout = "~/views/shared/_layout.cshtml" ;} @foreach (var item in Model) {<div class=" item "> @item. Description "c") < /h4> </div>}
To enable EF to automatically create the database and display it at the first run, we also need to configure the connection string in Web. config in Mysportsstore.webui:
... <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 to:
Routes. MapRoute ( "Default", "{controller}/{action}/{id}""Product" List", id = urlparameter.optional});
Run and get the following interface:
The Mysportsstore database has also been added to the database:
Source is here.
"MVC Project Practice, implementing Sportsstore in a three-tier architecture" series includes:
MVC Project Practice, implement Sportsstore in three-tier architecture, view three-tier architecture from class diagramsMVC Project Practice, implementation of SPORTSSTORE-01,EF Code first modeling, DAL layer, etc. in a three-tier architectureMVC Project Practice, implementation of the Sportsstore-02,dbsession layer, the BLL layer in the three-tier architectureMVC Project Practice, implementing the Sportsstore-03,ninject controller factory in a three-tier architectureMVC Project Practice, implementing SPORTSSTORE-04 in a three-tier architecture for pagingMVC Project Practice, implementing SPORTSSTORE-05 in a three-tier architecture, enabling navigationMVC Project Practice, implement SportsStore-06 in three-tier architecture, realize shopping cartMVC Project Practice, implementing SportsStore-07 in a three-tier architecture for order submissionMVC Project Practice, implementing SportsStore-08 in a three-tier architecture, deploying to an IIS serverMVC Project Practice, implementing the query service for sportsstore-09,asp.net MVC calls ASP. NET Web API in a three-tier architectureMVC Project Practice, implementation of SPORTSSTORE-10 in three-tier architecture, encryption and decryption of connection stringsMVC Project Practice, implementation of SPORTSSTORE-11 in the three-tier architecture, using knockout to implement additions and deletions
MVC Project Practice, implementing the Sportsstore-03,ninject controller factory in a three-tier architecture