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
5. Custom Ninject Controller Factory
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{ Public classNinjectcontrollerfactory:defaultcontrollerfactory {PrivateIkernel Ninjectkernel; Publicninjectcontrollerfactory () {Ninjectkernel=NewStandardkernel (); Addbindings (); } protected OverrideIController getcontrollerinstance (System.Web.Routing.RequestContext requestcontext, System.Type controllertype ) { returnControllertype = =NULL?NULL: (IController) Ninjectkernel.get (Controllertype); } Private voidaddbindings () {Ninjectkernel.bind<IProductService> (). To<productservice>(); } }}
Registering ninjectcontrollerfactory in the global
Public class MvcApplication:System.Web.HttpApplication { protectedvoid 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{ Public classBasecontroller:controller {protectedIlist<idisposable> Disposableobjects {Get;Private Set; } PublicBasecontroller () { This. Disposableobjects =NewList<idisposable>(); } protected voidAdddisposableobject (Objectobj) {IDisposable disposable= obj asIDisposable; if(Disposable! =NULL) { This. Disposableobjects.add (disposable); } } protected Override voidDispose (BOOLdisposing) { if(disposing) {foreach(IDisposable objinch This. disposableobjects) {if(NULL!=obj) {obj. Dispose (); } } } Base. Dispose (disposing); } }}
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;usingNinject;namespacemysportsstore.webui.controllers{ Public classProductcontroller:basecontroller {[Inject] iproductservice productservice {Get;Set; } PublicProductcontroller () { This. Adddisposableobject (Productservice); } PublicViewResult List () {returnView (productservice.loadentities (p =true). AsQueryable ()); } }}
The corresponding prduct/list.cshtml views are:
@model ienumerable<mysportsstore.model.product>@{Viewbag.title="List"; Layout="~/views/shared/_layout.cshtml";} @foreach (varIteminchModel) { <divclass="Item"> @item. Description"C") }
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:
<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 to:
routes. MapRoute ( "Default", "{Controller}/{action}/{id} ", new"Product""List ", id = urlparameter.optional} );
Run and get the following interface:
The Mysportsstore database has also been added to the database:
"MVC Project Practice, implementing Sportsstore in a three-tier architecture" series includes:
MVC project Practice, in the three-tier architecture implementation of SPORTSSTORE-01,EF Code first modeling, DAL layer and other MVC project practices, in the three-tier architecture implementation of the Sportsstore-02,dbsession layer, the BLL Layer MVC Project Practice, Implementing MVC project practices such as the Sportsstore-03,ninject controller factory in the three-tier architecture, implementing SportsStore-04 under the three-tier architecture, implementing the page-out MVC project practice, implementing SportsStore-05 under the three-tier architecture, Implementation of the Navigation MVC Project practice, the implementation of SportsStore-06 in the three-tier architecture, implementation of the shopping Cart MVC project, implementation of SPORTSSTORE-07 in the three-tier architecture, implementation of the Order Submission MVC Project practice, in the three-tier architecture to achieve SportsStore-08, Deploying to an IIS server