2五、ASP.NET MVC入門到精通——Spring.net-業務層倉儲

本系列目錄: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;}
 <#}#>
 }
}
View Code

八、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

<#}#>
}
}
View Code

九、業務層倉儲創建好了,如今咱們表現層來調用業務層倉儲的時候,每次又要去建立一個業務層對象,一樣的,咱們能夠建立一個表現層上下文來統一管理業務層對象的建立。在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(); }

如今調用起來是否是方便了許多。

相關文章
相關標籤/搜索