天天看點

MVC 架構搭建

1.0 初步建立目錄和項目

MVC 架構搭建
MVC 架構搭建

2.0 設定層之間的引用

irepository引用 model

repository引用 model,irepository

iservices引用 model,irepository

services引用 model,irepository,iservices

site引用 model,common,iservices,webhelper

webhelper引用system.web.mvc,iservices,system.web

common引用:system.web,system.web.extensions

3.0 在model層 添加 ado.net實體資料模型

dal層的 repository項目引用 entityframework.dll(找model層中的dll),system.data.entity

在 repository項目裡,建立類: basedbcontext

内容:

using system.data.entity;//繼承dbcontext 需要該命名空間.

public class basedbcontext : dbcontext

{

  public basedbcontext()

    : base("name=此處需要看app.config") //見 <connectionstrings><add name="jkcrmentities" /></connectionstrings>

  {

  }

}

删除model層裡的兩個檔案

MVC 架構搭建

4.0 各個父類的建立

irepository層建立 ibaserepository接口 -->内容見 ibaserepository接口.txt

repository建立類 baserepository -->内容見 baserepository類.txt (設定線程緩存,讓一個線程裡的所有dal共用一個ef容器)

iservices層 建立接口 ibaseservices-->内容見 ibaseservices接口.txt

services層 建立類 baseservices-->内容見 baseservices類.txt

把basedbcontext.cs等放入建立的base解決方案檔案夾内. 

5.0 t4模闆的使用

測試-->找一個實體,分别建立 irepository,iservices,repository,services或者直接利用 t4模闆6個檔案生成. 注意改t4模闆内容中 edmx的位置.

建立控制器測試(注意構造函數)-->沒有配置 autofac,會失敗.

6.0 autofac的添加

把autofaclibs拷到解決方案下.引用2個dll

在app_start中建立類 autofacconfig,内容見 autofacconfig類.txt. 注意:需要更改命名3個命名空間.

在global.asax中的最後注冊 autofacconfig.register();

7.0 項目配置

設定啟動項 site;設定連接配接webconfig字元串;設定 services層和repository層的生成位置。   ..\wlfhp.site\bin\

測試是否ok

8.0 其它 

建立basecontroller 引用system.web.mvc -->裡面是各種iservices

一份耕耘,一份收獲,付出就有回報永不遭遇過失敗,因我所碰到的都是暫時的挫折