ASP.NET MVC企业级项目框架

来源:互联网 发布:js cinfirm 编辑:程序博客网 时间:2024/05/23 10:38

MVC项目搭建笔记----

 

项目框架采用ASP.NET MVC+Entity Framwork+Spring.Net等技术搭建,搭建过程内容比较多,结合了抽象工厂的思想降低了三层之间的耦合,可以使用此套框架进行可扩展性要求高的企业级MVC项目开发。本框架的架构图如下:

第一步(创建分类文件夹):

创建5个文件夹。分别为UI,Model,BLL,DAL,Common,以便于将各模块分类整理。

第二步(项目类库的创建):

在UI文件夹创建ASP.NET MVC4项目模板选择基本。

在Model文件夹创建Model类库项目。

在BLL文件夹创建BLL和IBLL类库项目。

在DAL文件夹创建DAL,IDAL,DALFactory类库项目。

在Common文件夹创建Common类库项目。

第三步(创建EF实体):

在数据库管理工具新建一个数据库,在Model层添加一个ADO.Net实体模型。

建好实体模型,右键选择“根据模型生成数据库”,也可以先建好数据库再右键“从数据库更新模型”。

第四步(各层内容的创建,重点!):

在DAL层创建一个EFDbContextFactory类。

复制代码
复制代码
 1 public class EFDbContextFactory 2     { 3         public static DbContext GetCurrentDbContext() 4         { 5             //单例模式:保证线程实例唯一 6             DbContext db = (DbContext)CallContext.GetData("DbContext"); 7             if (db == null) 8             { 9                 db = new Model1Container();10 11                 CallContext.SetData("DbContext", db);12             }13             return db;14         }15     }
复制代码
复制代码

在DAL层创建一个BaseDal类,作为所有Dal的基类,封装crud方法。

复制代码
复制代码
 1  public class BaseDal<T> where T : class , new() 2     { 3         private DbContext db 4         { 5             get 6             { 7                 return EFDbContextFactory.GetCurrentDbContext(); 8             } 9         }10         public virtual T Add(T entity)11         {12             db.Set<T>().Add(entity);13             return entity;14         }15 16         public virtual bool Update(T entity)17         {18             db.Entry(entity).State = EntityState.Modified;19             return true;20         }21 22         public virtual bool Delete(T entity)23         {24             db.Entry(entity).State = EntityState.Deleted;25             return true;26 27         }28 29         public virtual int Delete(params int[] ids)30         {   31             foreach (var item in ids)32             {33                 var entity = db.Set<T>().Find(item);//如果实体已经在内存中,那么就直接从内存拿,如果内存中跟踪实体没有,那么才查询数据库。34                 db.Set<T>().Remove(entity);35             }36             return ids.Count();37         }38 39         public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda)40         {41             return db.Set<T>().Where(whereLambda).AsQueryable();42         }43 44         public IQueryable<T> LoadPageEntities<S>(int pageSize, int pageIndex, out int total, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderbyLambda, bool isAsc)45         {46             total = db.Set<T>().Where(whereLambda).Count();47             if (isAsc)48             {49                 return50                 db.Set<T>()51                   .Where(whereLambda)52                   .OrderBy(orderbyLambda)53                   .Skip(pageSize * (pageIndex - 1))54                   .Take(pageSize)55                   .AsQueryable();56             }57             else58             {59                 return60                db.Set<T>()61                  .Where(whereLambda)62                  .OrderByDescending(orderbyLambda)63                  .Skip(pageSize * (pageIndex - 1))64                  .Take(pageSize)65                  .AsQueryable();66             }67         }68     }
复制代码
复制代码

在DAL层添加Dal类的T4模板(Dal类生成模板,生成各Dal类,包括继承类和接口,未给出,可自行编写)。T4模板生成的Dal类内容模板如下:

1 public partial class UserInfoDal : BaseDal<UserInfo>,IUserInfoDal2 {3        4 }    

在IDAL层添加IDal接口类的T4模板(未给出,自行编写)。T4模板生成的IDal类内容模板如下:

1 public partial interface IUserInfoDal :IBaseDal<UserInfo>2 { 3 4 }    

在IDAL层添加IBaseDal接口类,作为IDal的基接口类,子接口只要继承此接口就可以实现crud(增删改查)及分页接口。

复制代码
复制代码
1 public interface IBaseDal<T>2 {3         T Add(T entity);4         bool Update(T entity);5         bool Delete(T entity);6         int Delete(params int[] ids);7         IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda);8         IQueryable<T> LoadPageEntities<S>(int pageSize, int pageIndex, out int total, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderbyLambda, bool isAsc);9 }
复制代码
复制代码

在IDAL层添加IDbSession接口类(此类作为DbSession类的约束,符合抽象的思想,不直接返回对象本身,而是返回他的接口,这样就不会直接对对象本身造成依赖,只要修改IDbSession)的T4模板(未给出,自行编写)。T4模板生成的IDbSession类内容模板如下:

复制代码
1 public partial interface IDbSession2 {      3     IUserInfoDal UserInfoDal { get; }4     int SaveChanges();5 }    
复制代码

在DALFactory层添加DbSession类的T4模板(未给出,自行编写)。T4模板生成的DbSession类内容模板如下:

复制代码
复制代码
 1 public partial class DbSession :IDbSession 2     {   3      4         private IUserInfoDal _UserInfoDal; 5         public IUserInfoDal UserInfoDal { 6             get { 7                 if (_UserInfoDal == null) 8                 { 9                     _UserInfoDal =new UserInfoDal();10                 }11                 return _UserInfoDal;12             }13         }14 15         public int SaveChanges()16         {17             //这里只需要调用当前线程内部的上下文SaveChange。18             DbContext dbContext = EFDbContextFactory.GetCurrentDbContext();19             return dbContext.SaveChanges();20         }21     }
复制代码
复制代码

在DALFactory层添加DbSessionFactory类,作为dbSession的工厂。

复制代码
复制代码
 1 public class DbSessionFactory 2 { 3         public static IDbSession GetDbSession() 4         { 5             IDbSession dbSession = (IDbSession) CallContext.GetData("DbSession"); 6             if (dbSession == null) 7             { 8                 dbSession = new DbSession(); 9                 CallContext.SetData("DbSession", dbSession);10                 return dbSession;11             }12             return dbSession;13         }14 }
复制代码
复制代码

在IBLL层创建IBaseService基接口类,作为所有IService接口类的crud公共约束。

复制代码
复制代码
 1 public interface IBaseService<T> 2 { 3         T Add(T entity); 4         bool Update(T entity); 5         bool Delete(T entity); 6         int Delete(params int[] ids); 7         IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda); 8  9         IQueryable<T> LoadPageEntities<S>(int pageSize, int pageIndex, out int total,10                                                   Expression<Func<T, bool>> whereLambda11                                                   , Expression<Func<T, S>> orderbyLambda, bool isAsc);12         int Savechanges();13 }
复制代码
复制代码

在IBLL层添加IBLL接口类的T4模板(未给出,自行编写)。T4模板生成的IBLL接口类内容模板如下:

1 public  partial interface IUserInfoService :IBaseService<UserInfo>2 {3 4 }

在BLL层创建BaseService类(作为所有Service类的基类,封装crud方法)。

复制代码
复制代码
1 public partial class UserInfoService:BaseService<UserInfo>,IUserInfoService2 {3         public override void SetCurrentDal()4         {5             CurrentDal = DbSession.UserInfoDal;6         }7 }
复制代码
复制代码

第五步(配置Spring.Net框架):

在UI层添加lib文件夹(用于存放所有外部引用文件),将Spring.Net程序集文件夹放到lib文件夹下,UI层添加对Spring.Core,Spring.Web,Spring.Web.Extensions,Spring.Web.Mvc4程序集的引用。

在Global.asax文件里将MvcApplication类继承至SpringMvcApplication。

在Web.config文件里的<configuration>下的<configSections>节点下添加:

复制代码
复制代码
1 <!--Spring配置节点-->2     <sectionGroup name="spring">3       <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/>4       <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />5     </sectionGroup>6 <!--Spring配置节点结束-->
复制代码
复制代码

在Web.config文件里的<configuration>节点下添加:

复制代码
复制代码
 1 <!--Spring配置节点--> 2   <spring> 3  4     <context> 5       <!--选择XML文件的位置,3种方式,1 配置文件 2 定位文件 3 程序集--> 6       <!--<resource uri="config://spring/objects"/>--> 7       <!--resource uri="file://ServiceXml.xml"/--> 8       <!--resource uri="file://Controllers.xml"/--> 9       <resource uri="assembly://MyOA_BLL/MyOA_BLL/ServiceXml.xml"/>10       <resource uri="assembly://MyOA/MyOA/Controllers.xml"/>11       <!--<resource uri="assembly://SpringNetTest/SpringNetTest/objects1.xml"/>-->12     </context>13     <objects xmlns="http://www.springframework.net">14 15     </objects>16 17   </spring>18   <!--Spring配置节点结束-->
复制代码
复制代码

第六步(注入Service对象):

在BLL层添加生成ServiceXml配置文件的T4模板(Speing.Net属性注入方法请参见  http://www.cnblogs.com/sunniest/p/4125561.html   ),内容模板为:

复制代码
复制代码
1 <objects xmlns="http://www.springframework.net"> 2     <object name="UserInfoService" type="MyOA_BLL.UserInfoService, MyOA_BLL"  singleton="false">3 4   </object>5   6 </objects>
复制代码
复制代码

在Controller文件夹下的各Controller类中添加

1 public IUserInfoService UserInfoService{get;set;}2 IDbSession session = DbSessionFactory.GetDbSession();

用UserInfoService来调用业务逻辑的方法(通过Spring.net注入UserInfoService对象),在操作完成后用session的savechanges方法控制将对实体的操作保存到数据库中。

在UI层添加Controller.xml文件(用于向Controller类注入UserInfoService对象),内容模板为:

复制代码
复制代码
1 <objects xmlns="http://www.springframework.net">2   <object name="TestController" type="MyOA.Controllers.TestController, MyOA"  singleton="false">3     <property name="UserInfoService" ref="UserInfoService" />4   </object>5 6 </objects>
复制代码
复制代码

至此项目基本框架搭建完成!

 

Controller调用业务逻辑层完整代码示例:

复制代码
复制代码
 1      public ActionResult Test() 2         { 3             return View(); 4         } 5  6         [HttpPost] 7         public ActionResult Test(string uname,string pwd) 8         { 9             UserInfo u =new UserInfo();10             u.UserName=uname;11             u.Pwd=pwd;12             var t = UserInfoService.Add(u);13             session.SaveChanges();14             if(t.Id>0){15                 return Content("注册成功!");16             }17             else{18                 return Content("注册失败!");19             }20         }
复制代码
复制代码
0 0