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

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

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

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

ExpandedBlockStart.gif 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. 获取容器实例

ExpandedBlockStart.gif 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()获取容器实例

如:

ExpandedBlockStart.gif 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/archive/2010/03/01/1675868.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值