Castle学习笔记三:在Asp.Net页面上得到Castle容器

参考:http://shanyou.cnblogs.com/archive/2005/10/28/264103.html

在项目中使用Castle IOC容器,可以在Asp.Net页面上这样得到Castle容器:

1. 在Global.asax.cs中实现IContainerAccessor

Global.asax
public class Global : System.Web.HttpApplication, IContainerAccessor
    {
        
private static WindsorContainer container;
        
protected void Application_Start(object sender, EventArgs e)
        {
            //CacheManager.LoadCacheFile(); 缓存管理类
            container 
= new WindsorContainer(new XmlInterpreter(new ConfigResource()));
        }
        
protected void Application_End(object sender, EventArgs e)
        {

        }
        
public IWindsorContainer Container
        {
            
get { return container; }
        }
    }

 2. 获取容器实例

ContainerAccessorUtil
public class ContainerAccessorUtil
    {
        
private ContainerAccessorUtil()
        {
        }

        
/// <summary>
        
/// 获得容器里的所有实例
        
/// </summary>
        
/// <returns>所有实例的List</returns>
        public static IWindsorContainer GetContainer()
        {
            IContainerAccessor containerAccessor 
= HttpContext.Current.ApplicationInstance as IContainerAccessor;

            
if (containerAccessor == null)
            {
                
throw new Exception("You must extend the HttpApplication in your web project " +
                    
"and implement the IContainerAccessor to properly expose your container instance" + "/n" +
                    
"你必须在HttpApplication中实现接口 IContainerAccessor 暴露容器的属性");
            }

            IWindsorContainer container 
= containerAccessor.Container as IWindsorContainer;

            
if (container == null)
            {
                
throw new Exception("The container seems to be unavailable in " +
                    
"your HttpApplication subclass" + "/n" + "HttpApplication 得不到容器的实例");
            }

            
return container;
        }
    } 

 3. 在具体页面的相关基类内,通过ContainerAccessorUtil.GetContainer()获取容器实例

如:

GetContainer
 public ISqlMapper sqlMap = (ContainerAccessorUtil.GetContainer())["SqlServerSqlMap"as ISqlMapper;

public static IContractBiz iContractBiz = ContainerAccessorUtil.GetContainer()["CCLOG.ContractBiz"as IContractBiz;

IBasicInfoBiz iBasicInfoBiz 
= ContainerAccessorUtil.GetContainer()["CCLOG.BasicInfoBiz"as IBasicInfoBiz;

//...

 要将IBatisNet交给Castle来管理,所以ISqlMapper的实例也必须从Castle容器中获取,这样Castle才能真正的管理IBatisNet。

从容器中获得ISqlMapper实例的方法:BaseSqlMapDao.cs

public ISqlMapper sqlMap = (ContainerAccessorUtil.GetContainer())["SqlServerSqlMap"as ISqlMapper;

 其中"sqlServerSqlMap"即是在我们配置IBatisNet Facility时指定的。

<facility id="ibatis" type="Castle.Facilities.IBatisNetIntegration.IBatisNetFacility, Castle.Facilities.IBatisNetIntegration" >
 
<sqlMap id="SqlServerSqlMap" config="SqlMap.config" />
</facility>

 附:

关于Global.asax文件:

Global.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法。你可以使用这个文件实现应用程序安全性以及其它一些任务。

Global.asax 位于应用程序根目录下。ASP.NET 页面框架能够自动识别出对Global.asax 文件所做的任何更改。

Global.asax 包含很多事件,常被用于安全性方面,可以用来处理应用程序级任务,比如用户身份验证、应用程序启动以及处理用户会话等。

原文地址:https://www.cnblogs.com/niuniu1985/p/1675868.html