上篇中我們已經展示數據到View, 但是這些數據都是來自於我們的mock IProductRepository,在我們真正的實現repository之前,我們需要創建一個SQL Server數據庫並添加一些數據。
我們將使用EF框架操作SQL Server數據庫, EF是一個.net ORM框架,ORM框架能讓我們想操作對象實例一樣操作數據庫的表、列、行。就像使用正常的C#對象,這么做需要一點Linq的知識,Linq不是什么神秘的、高難的東西,相信所有人都能夠在想當短的時間內掌握並使用LInq。
點擊View菜單,打開服務器資源管理器,點擊連接到數據庫。你會看到連接對話框,設置服務器名為(localdb)\v11.0,這是一個特殊的名字,表示你要使用本地數據庫的特性。VS2012新添加了一個特性,就是可以使用SQL Server的內核創建一個免管理員的本地數據庫,相關的詳細使用說明,請參見相關文檔,這里就不細說了。現在,請確保你選擇了windows認證登陸方式,設置數據庫名為SportsStore。如下圖:

點擊確定,然后會出現確認對話框,點擊yes,就會創建一個新的數據庫。

我們現在只有一個Product表,右擊表文件夾,選擇添加新表:

創建新表可以有2種方式,這里我們使用T-SQL方式,因為它更精准,更方便,建表語句如下:
CREATE TABLE Products
(
[ProductID] INT NOT NULL PRIMARY KEY IDENTITY,[Name] NVARCHAR(100) NOT NULL,[Description] NVARCHAR(500) NOT NULL,[Category] NVARCHAR(50) NOT NULL,[Price] DECIMAL(16, 2) NOT NULL
)
這個表與我們之前定義的Product model 類略有不同,現在點擊更新按鈕,你會看到一個更新的Summary
如下:

點擊更新數據庫按鈕。表就會被創建出來,我們需要添加些數據到這個表中。

創建Entity Framework Context
最新版的EF包含了一個很不錯的特性,叫做code-first,意思是我們能在我們的model中定義一些類,然后
從這些類生成數據庫。在此,我不想去談論這種技術的優缺點。相反,我將基於這個code-first,展示給你一個變體,我們將整合我們的model類和一個存在的數據庫,現在我們先安裝EF框架到我們的項目。
打開NuGet工具包管理程序,安裝EF框架,如下圖:

現在,在SportsStore.Domain工程中創建一個文件夾,叫做Concrete,然后添加一個文件,命名為EFDbContext,代碼如下:
using SportsStore.Domain.Entities;
using System.Data.Entity;
namespace SportsStore.Domain.Concrete
{
public class EFDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
}
為了使用code-first特性的優勢,我們需要創建一個類,它派生自 System.Data.Entity.DbContext. 這個類會為數據庫中的每個表自動定義一個屬性,並指定表名為屬性名,我們現在的屬性名是Products,並且類型參數是Product. 我們希望Product model被用作展示 Products表中的行。現在我們要告訴Entity Framework怎樣去連接數據庫,所以我們要添加一個連接字符串到
SportsStore.WebUI工程的Web.config文件中。
<connectionStrings>
<add name="EFDbContext" connectionString="Data Source=(localdb)\v11.0;Initial
Catalog=SportsStore;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
創建Product Repository
現在,我們要為實現真是的數據操作添加一個文件到 Concrete文件夾中,叫做EFProductRepository。編輯這個文件,看起來像下面的樣子:
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;
using System.Linq;
namespace SportsStore.Domain.Concrete {
public class EFProductRepository : IProductsRepository {
private EFDbContext context = new EFDbContext();public IQueryable<Product> Products {
get { return context.Products; }
}
}
}
這個repository 類實現了 IProductRepository接口並使用一個EFDbContext 實例去數據庫中取數據。你將看到EF是怎么樣 工作的。現在我們需要編輯NinjectControllerFactory類,用真正的數據替換Mock 對象。
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
requestContext, Type controllerType) {
return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
}
private void AddBindings() {
Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
//mock.Setup(m => m.Products).Returns(new List<Product> {
// new Product { Name = "Football", Price = 25 },
// new Product { Name = "Surf board", Price = 179 },
// new Product { Name = "Running shoes", Price = 95 }
//}.AsQueryable());
//ninjectKernel.Bind<IProductsRepository>().ToConstant(mock.Object);
ninjectKernel.Bind<IProductsRepository>().To<EFProductRepository>();
}
}
}
運行你的項目,你將看到如下畫面:

下一篇中,我們將添加更多商品,並應用分頁技術,讓用戶能夠瀏覽我們商品,並可以從一個頁面移動到另一個頁面,我們也將逐步的加入購物車等功能,為您一步一步地展開這個項目,如果您喜歡它,請繼續關注我的續篇!