天天看点

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

一份耕耘,一份收获,付出就有回报永不遭遇过失败,因我所碰到的都是暂时的挫折