Use mvc4, ninject, EF, Moq to build a real application of e-commerce sportsstore (1)

Source: Internet
Author: User
If you are ready to start the journey of mvc4, let's talk less about it and start our journey! After completing the sportsstore e-commerce platform, you will learn: 1. use mvc4 to develop your application project. 2. use ninject to complete IOC injection. 3. use Entity Framework to operate SQL Server databases. 4. use Moq to test your project. Now, start your vs2012! First, we need to create an empty Visual Studio solution. In this solution, we will create three projects. 1. A Domain module project. 2. An mvc4 application. 3. A unit test project. Now we create an empty solution named sportsstore, which looks like the following:

project name

vs Project template

purpose

sportsstore. domain

class library

domain entities and logic;

use entity framework to create a repository, and set it as a persistent layer.

sportsstore. webui

ASP. net mvc 4 Web application

controllers and views

sportsstore. unittests

unit test project

unit tests

You can delete class1.cs in the sportsstore. Domain project. We will not use it. In the sportsstore. domain creates two folders abstract, entities. in the sportsstore. create a folder, infrastructure, in webui. for example, in this step, the prototype of our project framework has come out. Now we want to add references to it. In The solusion manager, right-click each project at a time and select Add reference.

Project name

Tool dependency

Engineering dependency

Microsoft reference

Sportsstore. Domain

Entity Framework

None

System. Web. MVC

System. componentmodel

. Dataannotations

Sportsstore. webui

Ninject

Moq

Sportsstore. Domain

 

None

Sportsstore. unittests

Ninject

Moq

Sportsstore. Domain

Sportsstore. webui

System. Web. MVC

System. Web

Microsoft. CSHARP

Note: system. web. the version of MVC must be 4.0.0 to set di container. In our application, we have made a lot of extensions to the MVC Framework, which is also the focus of our learning. We have mastered these knowledge points, in future development projects, we will be able to build stable, scalable, and maintained enterprise application architectures with ease. Right-click the infrastructure folder of the sportsstore. webui project and select Add class named ninjectcontrollerfactory, Code As follows:
 Using  System;  Using  System. Collections. Generic;  Using  System. LINQ;  Using  System. Web;  Using  System. Web. MVC;  Using System. Web. Routing;  Using  Sportsstore. domain. abstract;  Using  Sportsstore. domain. entities;  Using  Moq;  Using  Ninject;  Using  Sportsstore. domain. concrete;  Namespace  Sportsstore. webui. Infrastructure {  Public   Class Ninjectcontrollerfactory: defaultcontrollerfactory {  Private  Ikernel ninjectkernel;  Public  Ninjectcontrollerfactory () {ninjectkernel = New  Standardkernel (); addbindings ();}  Protected   Override  Icontroller getcontrollerinstance (requestcontext, type controllertype ){  Return Controllertype = Null ? Null  : (Icontroller) ninjectkernel. Get (controllertype );}  Private   Void  Addbindings (){  //  Put bindings here  }}} 

We have not added any bindings yet, but we can use the addbindings method to add the bindings as needed. Now, we need

Tell MVC that we intend to use ninjectcontroller class to create the Controller object. To achieve this, open the global. asax. CS file of the sportsstore. webui project and add the Code:
 Using System;  Using  System. Collections. Generic;  Using  System. LINQ;  Using  System. Web;  Using  System. Web. HTTP;  Using  System. Web. MVC;  Using  System. Web. optimization;  Using  System. Web. Routing;  Using Sportsstore. webui. Infrastructure;  Namespace  Sportsstore. webui {  //  Note: For instructions on enabling the IIS6 or iis7 Classic mode,  //  Please visit  Http://go.microsoft.com /? Linkid = 9394801      Public   Class  Mvcapplication: system. Web. httpapplication {  Protected   Void Application_start () {arearegistration. registerallareas (); webapiconfig. register (globalconfiguration. configuration); filterconfig. registerglobalfilters (globalfilters. filters); routeconfig. registerroutes (routetable. routes); bundleconfig. registerbundles (bundletable. bundles );  //  Added by wangzhiyue  //  We need to tell MVC that we want to use the ninjectcontroller  //  Class to create controller objects Controllerbuilder. Current. setcontrollerfactory ( New Ninjectcontrollerfactory ());  //  Added end  Authconfig. registerauth ();}}} 

Start the domain Module

Now we are going to start the domain module. Applying the domain model in MVC applications can make everything perfect. Therefore, the domain must be the most perfect place to start the application. Because of our e-commerce applications, we need a product, which is obviously not enough. Right-click the created entities folder and create a new C # class named "product". The Code is as follows:
 Using  System;  Using  System. Collections. Generic;  Using System. LINQ;  Using  System. text;  Using  System. Threading. tasks;  Namespace  Sportsstore. domain. Entities {  Public   Class  Product {  Public   Int Productid { Get ; Set  ;} Public   String Name { Get ; Set  ;}  Public   String Description { Get ; Set  ;}  Public   Decimal Price { Get ; Set  ;} Public   String CATEGORY { Get ; Set  ;}}} 
Pay attention to this public keyword. Because we separate the domain model into an independent project, we define the access keyword as public. You can skip this step, but this helps to separate modules, make the project level clear and concise. If you think Article If you have learned the knowledge, continue to follow me!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.