论坛首页 编程语言技术论坛

一个不错的开源动态代理类库

浏览 4533 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-10-25   最后修改:2008-12-20
从codeproject里发现了一个非常不错的.net动态代理类库,有一个很好的特点是不仅支持接口代理,还支持非接口代理,关键是开源的。而castle是否开源就不太清楚了。
以下是我在事务处理中的应用。
public class DbTransactionProxy : IInterceptor
    {
        Object target = null;
        

        private DbTransactionProxy(Object target)
        {
            this.target = target;
        }

        public static T NewInstance<T>(Object target)
        {
            return new ProxyFactory().CreateProxy<T>(new DbTransactionProxy(target));
        }

        #region IInterceptor 成员

        object IInterceptor.Intercept(InvocationInfo info)
        {
            Object[] attrs = info.TargetMethod.GetCustomAttributes(typeof(TransactionAttribute), true);
            object ret = null;
            if (attrs != null && attrs.Length > 0)
            {
                TransactionAttribute trans = (TransactionAttribute)attrs[0];
                if (trans.IsTransaction)
                {
                    //开始事务处理
                    try
                    {
                        SessinoUtil.BeginTransaction(trans.ConnectionString);
                        ret = info.TargetMethod.Invoke(target, info.Arguments);
                        SessinoUtil.CommitTransaction(trans.ConnectionString);
                    }
                    catch(Exception ex)
                    {
                        SessinoUtil.RollbackTransaction(trans.ConnectionString);
                    }
                }
            }
            else
            {
                ret = info.TargetMethod.Invoke(target, info.Arguments);
            }
            return ret;
        }

        #endregion
    }


类似的,可以实现AOP缓存一类的应用,这里不多述
   发表时间:2008-11-12  
visualcatsharp 写道

而castle是否开源就不太清楚了。


castle是开源的
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics