工厂 调用Unity Application Block 通过XML配置文件映射读取数据层方法

1、首先创建FactoryApplication.cs应用程序文件继承HttpApplication
导入using System.Web.Security;
    using System.Configuration;
    using System.Web.SessionState;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.Configuration;
命名空间
Namespace WEB

    public class FactoryApplication : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            //在应用程序启动时运行的代码
            this.registerControllerFactory();
        }
        protected void Application_End(object sender, EventArgs e)
        {
            //在应用程序关闭时运行的代码
        }
        protected void Application_Error(object sender, EventArgs e)
        {
            //在出现未处理的错误时运行的代码
        }
        protected void Session_Start(object sender, EventArgs e)
        {
            //在新会话启动时运行的代码
        }
        protected void Session_End(object sender, EventArgs e)
        {
            //在会话结束时运行的代码。
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时,才会引发 Session_End 事件。如果会话模式
            //设置为 StateServer 或 SQLServer,则不会引发该事件。
        }
        private void registerControllerFactory()
        {
            IUnityContainer container=new UnityContainer();
            UnityConfigurationSection section=null;
            System.Configuration.Configuration config=null;
            ExeConfigurationFileMap map=new ExeConfigurationFileMap();
            
            map.ExeConfigFilename=GetMapPath("/Config/IDAL.Configuration.xml");
            config=ConfigurationManager.OpenMappedExeConfiguration(map,ConfigurationUserLevel.None);
            section=(UnityConfigurationSection)config.GetSection(unity);
            section.Containers.Default.Configure(container);
            
            Application.Add("container",container);
        }
        #region 获得当前绝对路径
        /// <summary>
        /// 获得当前绝对路径
        /// </summary>
        /// <param name="strPath">指定的路径</param>
        /// <returns>绝对路径</returns>
        private static string GetMapPath(string strPath)
        {
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Server.MapPath(strPath);
            }
            else //非web程序引用
            {
                strPath = strPath.Replace("/", "\\");
                if (strPath.StartsWith("\\"))
                {
                    strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
                }
                return                    System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,      strPath);
            }
        }
        #endregion
    }

2、修改Web项目的Global.asax文件 如下
<%@ Application Inherits="WEB.FactoryApplication" Language="C#" %>
3、在web项目中添加Config文件夹并给它添加IDAL.Configuration.xml文件
格式如下
<?xml version="1.0" encoding="utf-8" ?>
<!--DAL对象映射-->
<configuration>

  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>

  <unity>
    <typeAliases>
      <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
      <typeAlias alias="IUserInfor" type="IDAL.IUserInfor, IDAL" />
      <typeAlias alias="UserInfor" type="DAL.UserInfor, DAL" />
      <typeAlias alias="IRoles" type="IDAL.IRoles, IDAL"/>
      <typeAlias alias="Roles" type="DAL.Roles, SDAL" />
    </typeAliases>
    <containers>
      <container>
        <types>
          <type type="IUserInfor" mapTo="UserInfor" name="">
            <lifetime type="singleton" />
          </type>
          <type type="IRoles" mapTo="Roles" name="">
            <lifetime type="singleton" />
          </type>
        </types>
      </container>
    </containers>
  </unity>
</configuration>
4、创建工厂接口调用unity映射
导入using Microsoft.Practices.Unity;命名空间
namespace FactoryLibrary
{
    /// <summary>
    /// 创建对象工厂
    /// </summary>
    public class Factory
    {
        /// <summary>
        /// 创建数据层对象
        /// </summary>
        /// <typeparam name="TInterface">接口</typeparam>
        /// <returns></returns>
        public static TInterface CreateDAL<TInterface>()
        {
            IUnityContainer container = GetConatiner();
            return container.Resolve<TInterface>();
        }
        public static IUnityContainer GetConatiner()
        {
            return (IUnityContainer)System.Web.HttpContext.Current.Application["container"];
        }
    }
}
5、创建Model层 UserInfor、Roles
6、创建IDAL层 IUserInfor、IRoles
7、创建DAL层 UserInfor、Roles
8、创建BLL层 UserInfor、Roles
实现接口映射
IDAL.IUserInfor iUserInfor = FactoryLibrary.Factory.CreateDAL<IDAL.IUserInfor>();
IDAL.IRoles iRoles = FactoryLibrary.Factory.CreateDAL<IDAL.IRoles>();
原文链接:http://www.cnblogs.com/lonelyofsoul/archive/2012/03/21/unityapplicationblock.html
原文地址:https://www.cnblogs.com/lonelyofsoul/p/unityapplicationblock.html