本系列目录:ASP.NET MVC4入门到精通系列目录汇总html
上一节,咱们已经把项目框架的雏形搭建好了,那么如今我来开始业务实现,在业务实现的过程中,不断的来完善咱们现有的框架。web
一、假设咱们来作一个用户登陆的业务spring
那么咱们能够如今IDAL项目中定义的的接口IOu_UserInfoDAL,注意是部分类partial,为了方便管理,把这些扩展的部分接口都统一放到文件夹ExtensionIDAL中进行管理,注意命名空间要和以前的部分接口一致。session
using Model;
namespace IDAL { public partial interface IOu_UserInfoDAL { Ou_UserInfo GetUserInfoByName(string loginName); } }
二、DAL项目中,新建文件夹ExtensionDAL,文件夹下面新建部分类Ou_UserInfoDAL框架
using IDAL; using Model; namespace DAL { public partial class Ou_UserInfoDAL : IOu_UserInfoDAL { public Ou_UserInfo GetUserInfoByName(string loginName) { return base.GetListBy(x => x.uLoginName == loginName).FirstOrDefault(); } } }
三、IBLL项目新建文件夹ExtensionIBLL,文件夹下面新建IOu_UserInfoBLL接口ide
using Model; namespace IBLL { public partial interface IOu_UserInfoBLL { Ou_UserInfo Login(string strName, string strPwd); } }
BLL项目中新建文件夹ExtensionBLL,文件夹下面新建Ou_UserInfoBLL类post
using Model; using IBLL; namespace BLL { public partial class Ou_UserInfoBLL : IOu_UserInfoBLL { /// <summary> /// 登录 /// </summary> /// <param name="strName"></param> /// <param name="strPwd"></param> /// <returns></returns> public Ou_UserInfo Login(string strName, string strPwd) { //1.调用业务层方法 根据登录名查询 Ou_UserInfo usr = base.GetListBy(u => u.uLoginName == strName).FirstOrDefault(); //2.判断是否登录成功 return null; } } }
四、关于spring.net的使用请参考:1七、ASP.NET MVC入门到精通——Spring.net入门this
咱们下载下来spring.net包,而后把Spring.Core.dll 、和 Spring.Web.dll、Common.Logging.dll拷贝过来。在Web项目中添加一个Lib文件夹用来存放第三方的dll。url
DI项目中,添加这两个dll的引用,而后新建类SpringHelperspa
using Spring.Context; using Spring.Context.Support; namespace DI { public static class SpringHelper { #region 1.0 Spring容器上下文 -IApplicationContext SpringContext /// <summary> /// Spring容器上下文 /// </summary> private static IApplicationContext SpringContext { get { return ContextRegistry.GetContext(); } } #endregion #region 2.0 获取配置文件 配置的 对象 +T GetObject<T>(string objName) where T : class /// <summary> /// 获取配置文件 配置的 对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="objName"></param> /// <returns></returns> public static T GetObject<T>(string objName) where T : class { return (T)SpringContext.GetObject(objName); } #endregion }
五、修改Web项目中的Web.config文件,添加Spring配置
<!-- Spring 的配置 --> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/> <!-- 支持在 web.config 中定义对象 --> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> </sectionGroup> </configSections> <spring> <context> <resource uri="~/Config/objects.xml"/> </context> </spring>
Web项目中建立一个名为 Config 的文件夹,以保存独立的配置文件
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <object id="Ou_UserInfo" type="BLL.Ou_UserInfo,BLL" singleton="false"></object> <object id="BLLSession" type="BLL.BLLSession,BLL" singleton="false"></object> <object id="DBSessFactory" type="DAL.DBSessionFactory,DAL"></object> </objects>
修改BaseBLL.cs
#region 数据仓储 属性 + IDBSession DBSession /// <summary> /// 数据仓储 属性 /// </summary> public IDAL.IDBSession DBSession { get { if (iDbSession == null) { ////1.读取配置文件 //string strFactoryDLL = Common.ConfigurationHelper.AppSetting("DBSessionFatoryDLL"); //string strFactoryType = Common.ConfigurationHelper.AppSetting("DBSessionFatory"); ////2.1经过反射建立 DBSessionFactory 工厂对象 //Assembly dalDLL = Assembly.LoadFrom(strFactoryDLL); //Type typeDBSessionFatory = dalDLL.GetType(strFactoryType); //IDAL.IDBSessionFactory sessionFactory = Activator.CreateInstance(typeDBSessionFatory) as IDAL.IDBSessionFactory; //2.根据配置文件内容 使用 DI层里的Spring.Net 建立 DBSessionFactory 工厂对象 IDAL.IDBSessionFactory sessionFactory = DI.SpringHelper.GetObject<IDAL.IDBSessionFactory>("DBSessFactory"); //3.经过 工厂 建立 DBSession对象 iDbSession = sessionFactory.GetDBSession(); } return iDbSession; } } #endregion
六、咱们来看下控制器的调用
public ActionResult Index() { //1.经过业务接口查询数据 var Ou_UserInfoBLL = DI.SpringHelper.GetObject<IOu_UserInfoBLL>("Ou_UserInfo"); var userInfo= Ou_UserInfoBLL.Login("", ""); //2.加载视图 return View(); }
我每一个地方都经过DI.SpringHelper.GetObject<IOu_UserInfoBLL>("Ou_UserInfo");来调用是否是很不方便,那么咱们能够来建立一个业务层仓储来统一管理这些对象的建立。
七、IBLL项目,新建T4模板IBLLSession.tt,拷贝以前IDAL中的模板IDALSession过来,稍微修改一下就能够了
<#@ 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 inputFile = @"..\MODEL\OA.edmx"; EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string namespaceName = code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this); #> using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IBLL { public partial interface IBLLSession { <# // Emit Entity Types foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) {#> I<#=entity.Name#>BLL I<#=entity.Name#>BLL{get;set;} <#}#> } }
八、BLL项目,新建T4模板BLLSession.tt,拷贝以前DAL中的模板DALSession过来,稍微修改一下就能够了
<#@ 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 inputFile = @"..\MODEL\OA.edmx"; EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string namespaceName = code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this); #> using System; using System.Collections.Generic; using System.Linq; using System.Text; using IBLL; namespace BLL { public partial class BLLSession:IBLLSession { <# int index=0; // Emit Entity Types foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) { index++; #> #region <#=index #> 数据接口 I<#=entity.Name#>BLL I<#=entity.Name#>BLL i<#=entity.Name#>BLL; public I<#=entity.Name#>BLL I<#=entity.Name#>BLL{ get { if(i<#=entity.Name#>BLL==null) i<#=entity.Name#>BLL=new <#=entity.Name#>BLL(); return i<#=entity.Name#>BLL; } set { i<#=entity.Name#>BLL=value; } } #endregion <#}#> } }
九、业务层仓储创建好了,如今咱们表现层来调用业务层仓储的时候,每次又要去建立一个业务层对象,一样的,咱们能够建立一个表现层上下文来统一管理业务层对象的建立。在UI目录下面,添加类库项目Web.Helper,添加对IBLL和DI项目的引用,新建OperateContext.cs
using DI; using IBLL; namespace Web.Helper { public class OperateContext { public static IBLLSession _IBLLSession = SpringHelper.GetObject<IBLLSession>("BLLSession"); } }
十、修改控制器调用
using IBLL;
using Web.Helper;
public ActionResult Index() { var userInfo = OperateContext._IBLLSession.IOu_UserInfoBLL.Login("", ""); //2.加载视图 return View(); }
如今调用起来是否是方便了许多。