本系列目录: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 、和 Spring.Web.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, );
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, );
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=;
// 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();
}

现在调用起来是不是方便了许多。

25、ASP.NET MVC入门到精通——Spring.net-业务层仓储的更多相关文章

  1. 17、ASP.NET MVC入门到精通——Spring.net入门

    Spring.NET环境准备 pring.NET 1.3.2下载地址:http://down.51cto.com/data/861700 下载后解压 Spring.NET-1.3.2.7z:这个里面有 ...

  2. 26、ASP.NET MVC入门到精通——后台管理区域及分离、Js压缩、css、jquery扩展

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 有好一段时间没更新博文了,最近在忙两件事:1.看书,学习中...2.为公司年会节目做准备,由于许久没有练习双截棍了,难免生疏,所以现在临时抱 ...

  3. 22、ASP.NET MVC入门到精通——搭建项目框架

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 前面的章节,说了ASP.NET MVC项目中常用的一些技术和知识点,更多的是理论上面的东西,接下来,我将通过一个简单的OA项目来应用我们之前 ...

  4. 24、ASP.NET MVC入门到精通——数据库仓储

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 业务层调用数据层对象,我不想每次都new一个数据层对象,而是在数据层创建一个仓储,统一管理所有的对象调用. 1.在IDAL项目中,新建IDB ...

  5. 1、ASP.NET MVC入门到精通——新语法

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 在学习ASP.NET MVC之前,有必要先了解一下C#3.0所带来的新的语法特性,这一点尤为重要,因为在MVC项目中我们利用C#3.0的新特 ...

  6. 5、ASP.NET MVC入门到精通——NHibernate代码映射

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 上一篇NHibernate学习笔记—使用 NHibernate构建一个ASP.NET MVC应用程序 使用的是xml进行orm映射,那么这一 ...

  7. 6、ASP.NET MVC入门到精通——ASP.Net的两种开发方式

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 目前,ASP.NET中两种主流的开发方式是:ASP.NET Webform和ASP.NET MVC.从下图可以看到ASP.NET WebFo ...

  8. 7、ASP.NET MVC入门到精通——第一个ASP.NET MVC程序

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 开发流程 新建Controller 创建Action 根据Action创建View 在Action获取数据并生产ActionResult传递 ...

  9. 8、ASP.NET MVC入门到精通——View(视图)

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 View视图职责是向用户提供界面.负责根据提供的模型数据,生成准备提供给用户的格式界面. 支持多种视图引擎(Razor和ASPX视图引擎是官 ...

随机推荐

  1. Android开发学习之路-下拉刷新怎么做?

    因为最近的开发涉及到了网络读取数据,那么自然少不了的就是下拉刷新的功能,搜索的方法一般是自己去自定义ListView或者RecyclerView来重写OnTouch或者OnScroll方法来实现手势的 ...

  2. 网页webp转非webp的jpg gif png 图片

    webp 谷歌提出的一种图片格式.支持动图: gif静图: png jpg 网页 webp / jpg / gif / png 图片提取. 已在微信.淘宝.京东.一号店上测试通过. 如果电脑上有 ch ...

  3. android 画虚线,虚线圆

    Paint paint = new Paint(); paint.setAntiAlias(true); paint.setStyle(Paint.Style.STROKE);   paint.set ...

  4. Java基础-输入输出-2.编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt

    2.编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt ...

  5. MongoDB 安装和可视化工具

    MongoDB 是一款非常热门的NoSQL,面向文档的数据库管理系统,官方下载地址是:MongoDB,博主选择的是 Enterprise Server (MongoDB 3.2.9)版本,安装在Win ...

  6. 【Win 10应用开发】认识一下UAP项目

    Windows 10 SDK预览版需要10030以上版本号的Win 10预览版系统才能使用.之前我安装的9926的系统,然后安装VS 2015 CTP 6,再装Win 10 SDK,但是在新建项目后, ...

  7. 斐讯Fir302b救砖教程

    首先本人是路由器小白,不算是硬件改装高手,昨天收到了微信活动中的斐讯Fir302b,大概当时得奖的有300人,所以最近肯定很大一批朋友手里有这样的一款路由. 上网查了一下,此款路由可以刷基于tomat ...

  8. 深入学习jQuery选择器系列第八篇——过滤选择器之伪子元素选择器

    × 目录 [1]通用形式 [2]反向形式 [3]首尾元素 [4]唯一元素 前面的话 本文是子元素选择器的续篇,主要介绍关于nth-of-type()选择器的内容.该部分内容并非没有出现在<锋利的 ...

  9. 【前端性能】必须要掌握的原生JS实现JQuery

    很多时候,我们经常听见有人说jquery有多快多快.在这个各种类库满天飞的时候,不得不说的是,能有原生JS快吗? 是的,明显原生JS要更快,因为诸如JQuery这样的库必须要兼容各种浏览器和低版本和许 ...

  10. angularjs 请求后端接口请求了两次

    用angularjs的过程中发现,每次打开页面,请求后端的接口都请求了两次 如下图可以看到, http://192.168.1.109:8080/zdh/api/v1/goods/54 这个页面loa ...