ActiveRecord Enabling lazy load 方案

本文探讨了在ASP.NET 2.0环境下实现Castle ActiveRecord的LazyLoad功能的具体方法,通过自定义PageBase类来替代官方推荐的全局事件监听方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

lazy load AR 官的方案是

            BeginRequest += new EventHandler(OnBeginRequest);
            EndRequest 
+= new EventHandler(OnEndRequest);

            
// Initialization code omitted

        }

        
protected void Application_End(object sender, EventArgs e)
        
{

        }


        
public void OnBeginRequest(object sender, EventArgs e)
        
{
    
//        HttpContext.Current.Items.Add("nh.sessionscope", new SessionScope());
        }


        
public void OnEndRequest(object sender, EventArgs e)
        
{
            
//try
            
//{
            
//    SessionScope scope = HttpContext.Current.Items["nh.sessionscope"] as SessionScope;
            
//    if (scope != null)
            
//    {
            
//        scope.Dispose();
            
//    }
            
//}
            
//catch (Exception ex)
            
//{
            
//    HttpContext.Current.Trace.Warn("Error", "EndRequest: " + ex.Message, ex);
            
//}
        }


但我用这个办法,后来建了个PageBase.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Castle.ActiveRecord;

namespace Wz16.WebApp
{
    
public class PageBase : System.Web.UI.Page
    
{
        
protected SessionScope CurSessionScope ;
        
protected override void OnInit(EventArgs e)
        
{
            CurSessionScope 
= new SessionScope();
            
base.OnInit(e);
        }


        
public override void Dispose()
        
{
            
try
            
{
                
if (CurSessionScope != null)
                
{
                    CurSessionScope.Dispose();
                }

            }

            
catch (Exception ex)
            
{
                HttpContext.Current.Trace.Warn(
"Error""EndRequest: " + ex.Message, ex);
            }

            
            
base.Dispose();
        }

    }

}

继承自PageBase.cs的页面就都支持 AR 的 Lazy Load

我用的 asp.net2.0 为什么官方的方案会不行,有知道的人请赐教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值