自己动手搭建经典的3层 Asp.Net MVC

1:IBaseDAL

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Linq.Expressions;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7
 8 namespace C01.ZRF.IDAL
 9 {
10     public interface IBaseDAL<T> where T : class
11     {
12
13         int SaveChanges();
14
15         void Add(T model);
16
17         void Delete(T model);
18
19         void DeleteBy(System.Linq.Expressions.Expression<Func<T, bool>> delWhere);
20
21         void Modify(T model, params string[] propertyNames);
22
23         IQueryable<T> Where(Expression<Func<T, bool>> whereLambda);
24
25         IQueryable<T> WhereOrder<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> keySelector, bool isAsc = true);
26
27         IQueryable<T> WhereInclude(Expression<Func<T, bool>> whereLambda, params string[] includePropertyNames);
28
29         IQueryable<T> WhereInclude<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> keySelector, bool isAsc = true, params string[] includePropertyNames);
30
31         IEnumerable<T> WherePaged<TKey>(int PageIndex, int PageSize, out int totalCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> keySelector, bool isAsc = true, params string[] includePropertyNames);
32
33         IQueryable<T> QueryBySql(string sql, params System.Data.SqlClient.SqlParameter[] ps);
34     }
35 }

T4模板生成的接口

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile1 = @"E:\Temp\test01\3LaySolution\C10.ZRF.Model\Model1.edmx";
EdmItemCollection ItemCollection1 = loader.CreateEdmItemCollection(inputFile1);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using C10.ZRF.Model;

namespace C01.ZRF.IDAL
{
<#
foreach (EntityType entity in ItemCollection1.GetItems<EntityType>().OrderBy(e => e.Name))
{
#>
    public partial interface I<#=entity.Name#>_DAL : IBaseDAL<<#=entity.Name#>>{ }
<#}#>
}

生成后的效果如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using C10.ZRF.Model;

namespace C01.ZRF.IDAL
{
    public partial interface IBuyCar_DAL : IBaseDAL<BuyCar>{ }
    public partial interface IMenu_DAL : IBaseDAL<Menu>{ }
    public partial interface IProduct_DAL : IBaseDAL<Product>{ }
    public partial interface IRole_DAL : IBaseDAL<Role>{ }
    public partial interface IroleMenu_DAL : IBaseDAL<roleMenu>{ }
    public partial interface IuerRole_DAL : IBaseDAL<uerRole>{ }
    public partial interface IUser_DAL : IBaseDAL<User>{ }        

}

2:DAL

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data.Entity.Infrastructure;
  4 using System.Linq;
  5 using System.Linq.Expressions;
  6
  7 namespace C01.ZRF.DAL
  8 {
  9     using C01.ZRF.IDAL;
 10     using IOC;
 11     using System.Data.Entity;
 12
 13     public class BaseDAL<T>:IBaseDAL<T> where T:class
 14     {
 15         //1.创建EF上下文
 16         //  BaseDBContext db = new BaseDBContext();
 17         DbContext db = DBContextFactory.GetDbContext();
 18
 19         #region 0.0 批量更新EF容器数据到数据库 +int SaveChanges()
 20         /// <summary>
 21         /// 0.0 批量更新EF容器数据到数据库
 22         /// </summary>
 23         /// <returns>返回受影响行数</returns>
 24         public int SaveChanges()
 25         {
 26             return db.SaveChanges();
 27         }
 28         #endregion
 29
 30         #region 1.0 新增方法 +void Add(T model)
 31         /// <summary>
 32         /// 1.0 新增方法
 33         /// </summary>
 34         /// <param name="model"></param>
 35         public void Add(T model)
 36         {
 37             //1.直接通过EF上下文的 Set方法 获取一个 针对于 T类 做操作的 DbSet对象
 38             //var dbSet = db.Set<T>();
 39             //dbSet.Add(model);
 40             db.Set<T>().Add(model);
 41         }
 42         #endregion
 43
 44         #region 2.0 删除方法 +void Delete(T model)
 45         /// <summary>
 46         /// 2.0 删除方法
 47         /// </summary>
 48         /// <param name="model"></param>
 49         public void Delete(T model)
 50         {
 51             DbEntityEntry entry = db.Entry<T>(model);
 52             entry.State = System.Data.Entity.EntityState.Deleted;
 53         }
 54         #endregion
 55
 56         #region 2.1 条件删除方法 +void DeleteBy(System.Linq.Expressions.Expression<Func<T, bool>> delWhere)
 57         /// <summary>
 58         /// 2.1 条件删除方法
 59         /// </summary>
 60         /// <param name="delWhere">要删除的元素查询条件</param>
 61         public void DeleteBy(System.Linq.Expressions.Expression<Func<T, bool>> delWhere)
 62         {
 63             var delList = db.Set<T>().Where(delWhere);
 64             foreach (T model in delList)
 65             {
 66                 Delete(model);
 67             }
 68         }
 69         #endregion
 70
 71         #region 3.0 修改实体 + void Modify(T model, params string[] propertyNames)
 72         /// <summary>
 73         /// 3.0 修改实体
 74         /// </summary>
 75         /// <param name="model"></param>
 76         /// <param name="propertyNames"></param>
 77         public void Modify(T model, params string[] propertyNames)
 78         {
 79             DbEntityEntry entry = db.Entry<T>(model);
 80             entry.State = System.Data.Entity.EntityState.Unchanged;
 81             foreach (string proName in propertyNames)
 82             {
 83                 entry.Property(proName).IsModified = true;
 84             }
 85         }
 86         #endregion
 87
 88         #region 4.0 查询方法 +IQueryable<T> Where(Expression<Func<T, bool>> whereLambda)
 89         /// <summary>
 90         /// 4.0 查询方法
 91         /// </summary>
 92         /// <param name="whereLambda"></param>
 93         /// <returns></returns>
 94         public IQueryable<T> Where(Expression<Func<T, bool>> whereLambda)
 95         {
 96             return db.Set<T>().Where(whereLambda);
 97         }
 98         #endregion
 99
100         #region 4.1 查询方法 -带排序 +IQueryable<T> WhereOrder<TKey>
101         /// <summary>
102         /// 4.1 查询方法 -带排序
103         /// </summary>
104         /// <typeparam name="TKey"></typeparam>
105         /// <param name="whereLambda"></param>
106         /// <param name="keySelector">u=></param>
107         /// <param name="isAsc"></param>
108         /// <returns></returns>
109         public IQueryable<T> WhereOrder<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> keySelector, bool isAsc = true)
110         {
111             if (isAsc)
112                 return db.Set<T>().Where(whereLambda).OrderBy(keySelector);
113             else
114                 return db.Set<T>().Where(whereLambda).OrderByDescending(keySelector);
115         }
116         #endregion
117
118         #region 4.2 查询方法 -带Include +IQueryable<T> WhereInclude
119         /// <summary>
120         /// 4.2 查询方法 -带Include
121         /// </summary>
122         /// <param name="whereLambda"></param>
123         /// <param name="includePropertyNames">要进行连接查询的 属性名</param>
124         /// <returns></returns>
125         public IQueryable<T> WhereInclude(Expression<Func<T, bool>> whereLambda, params string[] includePropertyNames)
126         {
127             DbQuery<T> dbQuery = db.Set<T>();
128             foreach (string includeName in includePropertyNames)
129             {
130                 dbQuery = dbQuery.Include(includeName);
131             }
132             return dbQuery.Where(whereLambda);
133
134             //DbQuery<T> dbSet = (DbQuery<T>)db.Set<T>().Where(whereLambda);
135             //foreach (string includeName in includePropertyNames)
136             //{
137             //        dbSet = dbSet.Include(includeName);
138             //}
139             //return dbSet;
140         }
141         #endregion
142
143         #region 4.3 查询方法 -带Include 和 排序 +IQueryable<T> WhereInclude<TKey>
144         /// <summary>
145         /// 4.3 查询方法 -带Include 和 排序
146         /// </summary>
147         /// <typeparam name="TKey"></typeparam>
148         /// <param name="whereLambda"></param>
149         /// <param name="keySelector"></param>
150         /// <param name="isAsc"></param>
151         /// <param name="includePropertyNames"></param>
152         /// <returns></returns>
153         public IQueryable<T> WhereInclude<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> keySelector, bool isAsc = true, params string[] includePropertyNames)
154         {
155             DbQuery<T> dbQuery = db.Set<T>();
156             if (includePropertyNames != null && includePropertyNames.Length > 0)
157             {
158                 foreach (string includeName in includePropertyNames)
159                 {
160                     dbQuery = dbQuery.Include(includeName);
161                 }
162             }
163             IQueryable<T> query = dbQuery.Where(whereLambda);
164             if (isAsc)
165                 return query.OrderBy(keySelector);
166             else
167                 return query.OrderByDescending(keySelector);
168         }
169         #endregion
170
171         #region 4.4 查询方法 - 分页+Include+排序 + void WherePaged<TKey>
172         /// <summary>
173         /// 4.4 查询方法 - 分页+Include+排序
174         /// </summary>
175         /// <typeparam name="TKey"></typeparam>
176         /// <param name="pagedData"></param>
177         /// <param name="whereLambda"></param>
178         /// <param name="keySelector"></param>
179         /// <param name="isAsc"></param>
180         /// <param name="includePropertyNames"></param>
181         public IEnumerable<T> WherePaged<TKey>(int PageIndex, int PageSize, out int totalCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> keySelector, bool isAsc = true, params string[] includePropertyNames)
182         {
183             //0.获取 要操作的 数据表 对应的查询对象
184             DbQuery<T> dbQuery = db.Set<T>();
185             if (includePropertyNames != null && includePropertyNames.Length > 0)
186             {
187                 foreach (string includeName in includePropertyNames)
188                 {
189                     dbQuery = dbQuery.Include(includeName);
190                 }
191             }
192
193             IOrderedQueryable<T> orderQuery = null;
194             //2.排序
195             if (isAsc) { orderQuery = dbQuery.OrderBy(keySelector); }
196             else { orderQuery = dbQuery.OrderByDescending(keySelector); }
197             //3.分页查询
198             var list = orderQuery.Where(whereLambda).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
199             //4.获取总行数
200             totalCount = orderQuery.Where(whereLambda).Count();
201             return list;
202         }
203         #endregion
204
205         #region 4.5 查询方法  QueryBySql(string sql, params System.Data.SqlClient.SqlParameter[] ps) SQl语句的查询方法
206         public IQueryable<T> QueryBySql(string sql, params System.Data.SqlClient.SqlParameter[] ps)
207         {
208             return db.Database.SqlQuery<T>(sql, ps).AsQueryable();
209         }
210         #endregion
211     }
212 }

T4模板生成的代码如下:

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile1 = @"E:\Temp\test01\3LaySolution\C10.ZRF.Model\Model1.edmx";
EdmItemCollection ItemCollection1 = loader.CreateEdmItemCollection(inputFile1);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using C01.ZRF.IDAL;
using C10.ZRF.Model;

namespace C01.ZRF.DAL
{
<#
foreach (EntityType entity in ItemCollection1.GetItems<EntityType>().OrderBy(e => e.Name))
{
#>
    public partial class <#=entity.Name#>_DAL : BaseDAL<<#=entity.Name#>>,I<#=entity.Name#>_DAL{ }
<#}#>
}

如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using C01.ZRF.IDAL;
using C10.ZRF.Model;

namespace C01.ZRF.DAL
{
    public partial class BuyCar_DAL : BaseDAL<BuyCar>,IBuyCar_DAL{ }
    public partial class Menu_DAL : BaseDAL<Menu>,IMenu_DAL{ }
    public partial class Product_DAL : BaseDAL<Product>,IProduct_DAL{ }
    public partial class Role_DAL : BaseDAL<Role>,IRole_DAL{ }
    public partial class roleMenu_DAL : BaseDAL<roleMenu>,IroleMenu_DAL{ }
    public partial class uerRole_DAL : BaseDAL<uerRole>,IuerRole_DAL{ }
    public partial class User_DAL : BaseDAL<User>,IUser_DAL{ }        

}

3:IBLL

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Linq.Expressions;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7
 8 namespace C01.ZRF.IBLL
 9 {
10   public  interface IBaseBLL<T>where T:class
11     {
12         int SaveChanges();
13
14         void Add(T model);
15
16         void Delete(T model);
17
18         void DeleteBy(Expression<Func<T, bool>> delWhere);
19
20         void Modify(T model, params string[] propertyNames);
21
22         IQueryable<T> Where(Expression<Func<T, bool>> whereLambda);
23
24         IQueryable<T> WhereOrder<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> keySelector, bool isAsc = true);
25
26         IQueryable<T> WhereInclude(Expression<Func<T, bool>> whereLambda, params string[] includePropertyNames);
27
28         IQueryable<T> WhereInclude<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> keySelector, bool isAsc = true, params string[] includePropertyNames);
29
30         IEnumerable<T> WherePaged<TKey>(int PageIndex, int PageSize, out int totalCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> keySelector, bool isAsc = true, params string[] includePropertyNames);
31
32         IQueryable<T> QueryBySql(string sql, params System.Data.SqlClient.SqlParameter[] ps);
33     }
34 }

T4代码生成器:

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile1 = @"E:\Temp\test01\3LaySolution\C10.ZRF.Model\Model1.edmx";
EdmItemCollection ItemCollection1 = loader.CreateEdmItemCollection(inputFile1);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using C10.ZRF.Model;

namespace C01.ZRF.IBLL
{
<#
foreach (EntityType entity in ItemCollection1.GetItems<EntityType>().OrderBy(e => e.Name))
{
#>
    public partial interface I<#=entity.Name#>_BLL : IBaseBLL<<#=entity.Name#>>{ }
<#}#>
}

效果如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using C10.ZRF.Model;

namespace C01.ZRF.IBLL
{
    public partial interface IBuyCar_BLL : IBaseBLL<BuyCar>{ }
    public partial interface IMenu_BLL : IBaseBLL<Menu>{ }
    public partial interface IProduct_BLL : IBaseBLL<Product>{ }
    public partial interface IRole_BLL : IBaseBLL<Role>{ }
    public partial interface IroleMenu_BLL : IBaseBLL<roleMenu>{ }
    public partial interface IuerRole_BLL : IBaseBLL<uerRole>{ }
    public partial interface IUser_BLL : IBaseBLL<User>{ }        

}

4:BLL

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4
 5 namespace C01.ZRF.BLL
 6 {
 7     using C01.ZRF.IBLL;
 8     using C01.ZRF.IDAL;
 9     using System.Data.SqlClient;
10     public partial class BaseBLL<T> : IBaseBLL<T> where T : class
11     {
12         protected IBaseDAL<T> basedal;
13         public void Add(T model)
14         {
15             basedal.Add(model);
16         }
17
18         public void Delete(T model)
19         {
20             basedal.Delete(model);
21         }
22
23         public void DeleteBy(System.Linq.Expressions.Expression<Func<T, bool>> delWhere)
24         {
25             basedal.DeleteBy(delWhere);
26         }
27
28         public void Modify(T model, params string[] propertyNames)
29         {
30             basedal.Modify(model, propertyNames);
31         }
32
33         public IQueryable<T> QueryBySql(string sql, params SqlParameter[] ps)
34         {
35             return basedal.QueryBySql(sql, ps);
36         }
37
38         public int SaveChanges()
39         {
40             return basedal.SaveChanges();
41         }
42
43         public IQueryable<T> Where(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda)
44         {
45             return basedal.Where(whereLambda);
46         }
47
48         public IQueryable<T> WhereInclude(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, params string[] includePropertyNames)
49         {
50             return basedal.WhereInclude(whereLambda, includePropertyNames);
51         }
52
53         public IQueryable<T> WhereInclude<TKey>(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, TKey>> keySelector, bool isAsc = true, params string[] includePropertyNames)
54         {
55             return basedal.WhereInclude<TKey>(whereLambda, keySelector, isAsc, includePropertyNames);
56         }
57
58         public IQueryable<T> WhereOrder<TKey>(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, TKey>> keySelector, bool isAsc = true)
59         {
60             return basedal.WhereOrder<TKey>(whereLambda, keySelector, isAsc);
61         }
62
63         public IEnumerable<T> WherePaged<TKey>(int PageIndex, int PageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, TKey>> keySelector, bool isAsc = true, params string[] includePropertyNames)
64         {
65             return basedal.WherePaged<TKey>(PageIndex, PageSize, out totalCount, whereLambda, keySelector, isAsc, includePropertyNames);
66         }
67     }
68 }

T4模板生成器:

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile1 = @"E:\Temp\test01\3LaySolution\C10.ZRF.Model\Model1.edmx";
EdmItemCollection ItemCollection1 = loader.CreateEdmItemCollection(inputFile1);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using C10.ZRF.Model;
using C01.ZRF.IBLL;
using C01.ZRF.IDAL;
namespace C01.ZRF.BLL
{
<#
foreach (EntityType entity in ItemCollection1.GetItems<EntityType>().OrderBy(e => e.Name))
{
#>
    public partial class <#=entity.Name#>_BLL : BaseBLL<<#=entity.Name#>>,I<#=entity.Name#>_BLL{
        I<#=entity.Name#>_DAL dal;
        public <#=entity.Name#>_BLL(I<#=entity.Name#>_DAL dal){
            this.dal=dal; base.basedal=dal;
        }
    }
<#}#>
}

效果如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace C01.ZRF.BLL
 8 {
 9     using C10.ZRF.Model;
10     using C01.ZRF.IBLL;
11     using C01.ZRF.IDAL;
12     public partial class RoleBLL : BaseBLL<Role>, IRole_BLL
13     {
14         IRole_DAL dal;
15         public RoleBLL(IRole_DAL dal) {
16             this.dal = dal;
17             base.basedal = dal;
18         }
19     }
20 }

5:AutoFac配置文件内容

 1 using Autofac;
 2 using Autofac.Integration.Mvc;
 3 using System.Reflection;
 4
 5 namespace WebApplication1
 6 {
 7     public class AutoFacConfig
 8     {
 9         public static void Register()
10         {
11             //1.0 创建一个autofac的容器创建者对象
12             var builder = new ContainerBuilder();
13
14             //2.0 告诉autofac控制器类所存储的程序集是谁
15             Assembly controllerAss = Assembly.Load("WebApplication1");
16             builder.RegisterControllers(controllerAss);
17
18
19             //3.0 将仓储层中的所有的类实例化以其接口的形式存储起来
20             Assembly dalAss = Assembly.Load("C01.ZRF.DAL");
21             builder.RegisterTypes(dalAss.GetTypes()).AsImplementedInterfaces();
22
23             //4.0 将业务逻辑层中的所有的类实例化以其接口的形式存储起来
24             Assembly bllAss = Assembly.Load("C01.ZRF.BLL");
25             builder.RegisterTypes(bllAss.GetTypes()).AsImplementedInterfaces();
26
27             //5.0 告诉MVC底层控制器的对象创建工作被autofac替代
28             //5.0.1 创建一个真正的autofac工作容器
29             var c = builder.Build();
30
31             //5.0.2 将auto发出工作容器替换MVC底层
32             System.Web.Mvc.DependencyResolver.SetResolver(new AutofacDependencyResolver(c));
33         }
34     }
35 }

配置后再去Global.asax 全局文件里面注册即可:

1 protected void Application_Start()
2 {
3 AreaRegistration.RegisterAllAreas();
4 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
5 RouteConfig.RegisterRoutes(RouteTable.Routes);
6 BundleConfig.RegisterBundles(BundleTable.Bundles);
7 AutoFacConfig.Register();//------
8
9 }

6:Web端来调用:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web.Mvc;
 5
 6 namespace WebApplication1.Controllers
 7 {
 8     using C01.ZRF.IBLL;
 9     using C10.ZRF.Model.ModelView;
10     using COMMOM;
11     using C10.ZRF.Model.Filter;
12     using System.Threading.Tasks;
13     using C10.ZRF.Model;
14
15     // [WebApplication1._Filters.ZrfComparess]
16     public class HomeController : Controller
17     {
18         public ActionResult DoCombresTest() {
19             return View();
20         }
21
22         #region MyRegion
23         IBuyCar_BLL bll;
24         public HomeController(IBuyCar_BLL bll)
25         {
26             this.bll = bll;
27         }
28         // GET: Home
29         public ActionResult Index()
30         {
31             ViewBag.car = bll.Where(c => c.cid == 6).FirstOrDefault().pcount;
32             ViewBag.time = "时间是=" + DateTime.Now.ToString();
33             return View();
34         }
35     }
36 }

原文地址:https://www.cnblogs.com/Fengge518/p/11846243.html

时间: 2024-10-05 21:00:36

自己动手搭建经典的3层 Asp.Net MVC的相关文章

ASP.NET MVC企业级项目框架搭建实战

MVC项目搭建笔记---- 项目框架采用ASP.NET MVC+Entity Framwork+Spring.Net等技术搭建,搭建过程内容比较多,结合了抽象工厂的思想降低了三层之间的耦合,可以使用此套框架进行可扩展性要求高的企业级MVC项目开发.本框架的架构图如下: 第一步(创建分类文件夹): 创建5个文件夹.分别为UI,Model,BLL,DAL,Common,以便于将各模块分类整理. 第二步(项目类库的创建): 在UI文件夹创建ASP.NET MVC4项目模板选择基本. 在Model文件夹

Unity + iBatis + Asp.net Mvc 系统搭建

Unity + iBatis + Asp.net Mvc 系统搭建 之前用EntityFramework Code First做了一些小项目,很是方便:后来在一个 Java 项目中接触了myBatis之后,深深的喜欢上了这种最直接最原始最灵活的数据库操作,所以最终决定改造之前的项目,使用IBatis访问数据库: 一.框架搭建 1)新建一个Asp.net Mvc的应用,.Net使用4.5 2)使用 Nuget 安装 Unity 3.5,因为比较熟悉 Unity: 3)使用 Nuget 安装 Uni

IIS经典模式对ASP.NET MVC应用程序的影响

IIS有两种模式:经典模式和集成模式.IIS7之前的版本都是经典模式,而从IIS7开始出现了集成模式,为了向后兼容也有经典模式. 经典模式是用ISAPI扩展Aspnet_isapi.dll来处理asp.net服务器页面,而集成模式把ISAPI扩展组件集成到了IIS中.从性能上讲,集成模式要优于经典模式. 下面我们通过一个例子来看看在IIS经典模式下运行ASP.NET MVC应用程序需要做哪些额外的配置. 新建一个MVC4.0程序 这里我们用IIS EXPRESS服务器调试,将模式设置为经典模式,

NHibernate构建一个ASP.NET MVC应用程序

NHibernate构建一个ASP.NET MVC应用程序 什么是Nhibernate? NHibernate是一个面向.NET环境的对象/关系数据库映射工具.对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种技术,用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去. NHibernate 是一个基于.Net 的针对关系型数据库的对象持久化类库.NHibernate 来源于非常优秀的基于Java的Hibernate 关系型持久化工具.

ASP.NET MVC 概述

目标:学习ASP.NET MVC 和ASP.NET WebForm的不同之处.学习在合适的地方使用ASP.NET MVC. MVC(Model-View-Controller)结构模式把一个对象分离成3大块:Model(数据模型层),View(视图层),Controller(控制层),ASP.NET MVC 框架提供了一种有别于ASP.NET WebForm的模式,提供了基于mvc的网页对象.ASP.NET MVC是一种轻量级的,高度可测试的展示型框架(和WebForm对比),并且融合了已有的A

asp.net mvc 简单项目框架的搭建过程(一)对Bll层和Dal层进行充分解耦

学习asp.net 已经有近三个月的时间了,在asp.net mvc上花的时间最多,但个人真是有些菜,不得不说,asp.net mvc的水真的还是蛮深的.目前在公司实习,也见过公司几个项目的代码了.对项目的代码始终停留在一知半解的地步,能改一些简单的bug,但关于项目的来龙去脉始终云里雾里.对于asp.net mvc的架构始终看不懂.因此,照着传智博客的学习视频,学了一下简单的架构搭建.真个架构的搭建我看了将近两遍视频,才稍稍有些头绪,今天在这里记录一下,一方面加深理解,一方面如果以后忘记了,还

ASP.NET MVC+EF框架+EasyUI实现权限管理系列(4)-业务逻辑层的封装

原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(4)-业务逻辑层的封装 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建    (2):数据库访问层的设计Demo    (3):面向接口编程 前言:前面几篇博客我们基本已经介绍完了搭建整个项目和数据库访问层以及一些业务逻辑层的实现,当然了,我们的数据库访问层这样还是可以在进行封装的,但是我到这里就行了吧,项目也不大,不需要那么麻烦的,那么我们今天开始介绍我们需要介绍的内容,那就是我

ASP.NET MVC+EF框架+EasyUI实现权限管理系列(2)-数据库访问层的设计Demo

原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(2)-数据库访问层的设计Demo ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1)框架搭建 前言:这篇博客我们继续来实现我的权限系列,这个博客一段时间也没有写了,重点是我在想还写不写,最终我决定还是写下去,因为我们是为了学习,当别人提出意见的时候,我们可以参考和采纳,但是我们不一定非要采纳,上几篇博客大家都说用CodeFirst来实现,是啊,现在基本很少有人用我的这种方法来实现了,都是用CodeF

ASP.NET MVC+EF框架+EasyUI实现权限管理系列(1)-框架搭建

原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(1)-框架搭建 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) 前言:这篇博客开始我们便一步一步的来实现这个权限系统的初步设计-框架搭建,首先我要说的是我们需要开发工具Visual Studio 2012或者10也行,其次是我们要有SQL Server数据库,如果是Visual Studio 2010的话,你还要安装MVC4的开发文件,这个是吗?我不记得了,谁可以回答我一下的,我一直用2012,都是集成