.net - 在 .NET Web 应用程序中使用 CaSTLe DynamicProxy 的性能建议

标签 .net aop castle castle-dynamicproxy dynamic-proxy

我从 CaSTLe DynamicProxy 开始,我有这个示例来跟踪对象属性的更改。

问题:

  • 我应该缓存(在静态字段中)我在 AsTrackable() 中使用的 ProxyGenerator() 实例吗?我打算在 ASP.NET Web 应用程序中使用,但我不确定该类是否是线程安全的?创作成本高吗?
  • 如果我保留代码原样,生成的代理类型是否会被不同的 ProxyGenerator 实例重用。我读了caching tutorial ,但不确定“模块范围”是什么意思。
  • 从性能的角度来看,还有其他建议可以改进代码吗?

  • 代码:
    class Program
    {
        static void Main(string[] args)
        {
            var p = new Person { Name = "Jay" }.AsTrackable();
    
            //here's changed properties list should be empty.
            var changedProperties = p.GetChangedProperties();
    
            p.Name = "May";
    
            //here's changed properties list should have one item.
            changedProperties = p.GetChangedProperties();
        }
    }
    
    public static class Ext
    {
        public static T AsTrackable<T>(this T instance) where T : class
        {
            return new ProxyGenerator().CreateClassProxyWithTarget
            (
              instance, 
              new PropertyChangeTrackingInterceptor()
            );
        }
    
        public static HashSet<string> GetChangedProperties<T>(this T instance) 
        where T : class
        {
            var proxy = instance as IProxyTargetAccessor;
    
            if (proxy != null)
            {
                var interceptor = proxy.GetInterceptors()
                                       .Select(i => i as IChangedProperties)
                                       .First();
    
                if (interceptor != null)
                {
                    return interceptor.Properties;
                }
            }
    
            return new HashSet<string>();
        }
    }
    
    interface IChangedProperties
    {
        HashSet<string> Properties { get; }
    }
    
    public class PropertyChangeTrackingInterceptor : IInterceptor, IChangedProperties
    {
        public void Intercept(IInvocation invocation)
        {
            invocation.Proceed();
    
            this.Properties.Add(invocation.Method.Name);
        }
    
        private HashSet<string> properties = new HashSet<string>();
    
        public HashSet<string> Properties
        {
            get { return this.properties; }
            private set { this.properties = value; }
        }
    }
    
    public class Person
    {
        public virtual string Name { get; set; }
        public virtual int Age { get; set; }
    }
    

    }

    最佳答案

    缓存代理生成器的静态副本是线程安全的,您绝对应该这样做。这被认为是此 API 的最佳实践,不这样做会导致在新的动态程序集中无缘无故地定义额外的类型。

    关于.net - 在 .NET Web 应用程序中使用 CaSTLe DynamicProxy 的性能建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6302617/

    相关文章:

    .net - 是否有 .NET 命名空间可以在其中找到 WIN32 API 消息相关的 #defines,例如 WM_COMMAND 等

    c# - 界限在哪里 - 是否有可能过于热爱 LINQ?

    C# : So if a static class is bad practice for storing global state info, 提供相同便利的好的替代方案是什么?

    c# - 异常处理 : AOP vs Classic Handling?

    aop - 如何为一系列方法定义切点?

    C# 类设计 - 我应该考虑将 "Project"类放在哪里

    java - Spring AOP 切入点表达式中包名称的通配符支持

    caSTLe-windsor - CaSTLe 温莎错误 : Resolving singleton through child container

    c# - C# 中的本地化属性参数

    c# - 统一工厂注入(inject)