Spring.net-业务层仓储
Spring.net-业务层仓储
本系列目录:ASP.NET MVC4入门到精通系列目录汇总
上一节,我们已经把项目框架的雏形搭建好了,那么现在我来开始业务实现,在业务实现的过程当中,不断的来完善我们现有的框架。
1、假设我们来做一个用户登录的业务
那么我们可以现在IDAL项目中定义的的接口IOu_UserInfoDAL,注意是部分类partial,为了方便管理,把这些扩展的部分接口都统一放到文件夹ExtensionIDAL中进行管理,注意命名空间要和之前的部分接口一致。

using Model;
namespace IDAL
{
public partial interface IOu_UserInfoDAL
{
Ou_UserInfo GetUserInfoByName(string loginName);
}
}

2、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();
}
}
}

3、IBLL项目新建文件夹ExtensionIBLL,文件夹下面新建IOu_UserInfoBLL接口

using Model; namespace IBLL
{
public partial interface IOu_UserInfoBLL
{
Ou_UserInfo Login(string strName, string strPwd);
}
}

BLL项目中新建文件夹ExtensionBLL,文件夹下面新建Ou_UserInfoBLL类

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;
}
}
}

4、关于spring.net的使用请参考:17、ASP.NET MVC入门到精通——Spring.net入门
我们下载下来spring.net包,然后把Spring.Core.dll 、Common.Logging.dll拷贝过来。在Web项目中添加一个Lib文件夹用来存放第三方的dll。
DI项目中,添加这两个dll的引用,然后新建类SpringHelper

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
}

5、修改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

6、我们来看下控制器的调用

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");来调用是不是很不方便,那么我们可以来创建一个业务层仓储来统一管理这些对象的创建。
7、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;}
<#}#>
}
}
8、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
<#}#>
}
}
9、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");
}
}

10、修改控制器调用

using IBLL;
using Web.Helper;
public ActionResult Index()
{
var userInfo = OperateContext._IBLLSession.IOu_UserInfoBLL.Login("", "");
//2.加载视图
return View();
}

现在调用起来是不是方便了许多。
Spring.net-业务层仓储的更多相关文章
- 25、ASP.NET MVC入门到精通——Spring.net-业务层仓储
本系列目录:ASP.NET MVC4入门到精通系列目录汇总 上一节,我们已经把项目框架的雏形搭建好了,那么现在我来开始业务实现,在业务实现的过程当中,不断的来完善我们现有的框架. 1.假设我们来做一个 ...
- 在spring的业务层获取request,response
1.直接通过controller层获取到传输到业务层2.SpringMVC提供的RequestContextHolder可以直接获取代码: RequestAttributes requestAttri ...
- SSH三种框架及表示层、业务层和持久层的理解
Struts(表示层)+Spring(业务层)+Hibernate(持久层) SSH:Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts:Struts是一个表示 ...
- SSH三种框架及表示层、业务层和持久层的理解(转)
Struts(表示层)+Spring(业务层)+Hibernate(持久层) SSH:Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts:Struts是一个表示 ...
- 【转载】 JAVA三层架构,持久层,业务层,表现层的理解
JAVA三层架构,持久层,业务层,表现层的理解 转载:http://blog.csdn.net/ljf_study/article/details/64443653 SSH: Struts(表示层)+ ...
- Spring.NET在MVC中实现业务层和UI层解耦
最近在项目中用到了Spring.NET,使用它来实现业务层和UI层解耦.使用过程中难免遇到问题,现把遇到的一些问题整理出来,留作笔记. 使用的开发工具是vs2017,.netframework 4.6 ...
- 阶段3 2.Spring_07.银行转账案例_5 编写业务层和持久层事务控制代码并配置spring的ioc
Service中就需要用到 TransactionManager中的方法.提供set方法等着spring注入 这里面所有的操作都可以加上事物控制 其他的方法都是相同的操作 这里没有返回结果 转账的方法 ...
- 关于Spring Boot中的业务层(Service)是否要创建接口的分析
在借助Spring Boot框架开发web项目时,在业务层(Service)这一部分,标准做法是:定义一个接口,然后再一个或多个类去实现.那么疑问来了: 为什么我们要维护两份同构代码,而不直接使用一个 ...
- Spring/SpringMVC/MyBatis(持久层、业务层、控制层思路小结)
准备工作: ## 7 导入省市区数据到数据库中 1. 从FTP下载SQL脚本文件 2. 把脚本文件移动到易于描述绝对路径的位置 3. 进入MySQL控制台 4. 使用`xxx_xxx`数据库 5. 运 ...
随机推荐
- 离别·伤
天边露出尖尖的小月 青涩似梦 一点萤火虫落在时光的蘋 搜索 若然恍惚 莺归晚巢 日隐西山 至此予你别过 未曾听你轻启朱唇 未曾见你合身回眸 风,走过紫罗兰花 淡淡的香绕过你的长发 ...
- bzoj2301(莫比乌斯反演+分块)
传送门:2301: [HAOI2011]Problem b 题意:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y ...
- VB.NET 机房收费系统项目总结
VB.NET机房收费系统项目总结 从2013年5月3日——2013年8月20日历时三个多月的.NET机房收费系统终于完成了.项目做完了,真有一种如释重负的感觉. 下面我将从文档.UML图,代码这三个方 ...
- cocos2dx-3.0(1)------win7 32位android环境搭建
參照链接http://blog.csdn.net/wonengxing/article/details/23601359 ----我的生活,我的点点滴滴!! 一. Android工具安装 1. 安装J ...
- linux它SQL声明简明教程---WHERE
我们并不一定必须注意,每次格里面的信息是完全陷入了.在很多情况下,我们需要有选择性地捕捞数据.对于我们的样本.我们可以只抓住一个营业额超过 $1,000 轮廓. 做这个事情,我们就须要用到 WHERE ...
- 福利 城市名的python list
["上海","北京","北京市","朝阳","朝阳区","海淀","元 ...
- js正则匹配html内容
1.得到网页上的链接地址: string matchString = @"<a[^>]+href=\s*(?:'(?<href>[^']+)'|"" ...
- uvalive4327(单调队列优化)
这题我有闪过是用单调队列优化的想法,也想过有左右两边各烧一遍. 但是不敢确定,搜了题解,发现真的是用单调队列,然后写了好久,调了好久下标应该怎么变化才过的. dp[i][j] 表示走到第i行,第j个竖 ...
- net MVC 的八个扩展点
net MVC 的八个扩展点 MVC模型以低耦合.可重用.可维护性高等众多优点已逐渐代替了WebForm模型.能够灵活使用MVC提供的扩展点可以达到事半功倍的效果,另一方面Asp.net MVC优秀的 ...
- HDU2647-Reward(拓扑排序)
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...