加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Unity ICallHandler与IInterceptionBehavior

发布时间:2020-12-16 00:07:01 所属栏目:百科 来源:网络整理
导读:看起来Unity似乎提供了两种不同的路线来实现AoP功能. 问题是为什么?有什么区别?每种方法的优缺点是什么? 例如,使用ICallHandler: unity.ConfigureInterception().AddMatchingRule( new TypeMatchingRule(typeof (ComplexEntity)) ).AddMatchingRule( new
看起来Unity似乎提供了两种不同的路线来实现AoP功能.

问题是为什么?有什么区别?每种方法的优缺点是什么?

例如,使用ICallHandler:

unity.Configure<Interception>()
.AddMatchingRule(
                    new TypeMatchingRule(typeof (ComplexEntity))
                ).AddMatchingRule(
                    new TypeMatchingRule(typeof (ComplexEntity.InnerEntity))
                ).AddMatchingRule(
                    new MemberNameMatchingRule("*")
                ).AddCallHandler(
                    new CallHandler()
                );

但是使用IInterceptionBehavior代替ICallHandler也可以实现类似的功能

unity.RegisterType<ComplexEntity,ComplexEntity>
     (new VirtualMethodInterceptor(),new InterceptionBehavior)

还有一个混合某处可以让你设置拦截,但使用一个调用处理程序,例如.

unity.Configure<Interception>()
                .SetInterceptorFor<ComplexEntity>(new VirtualMethodInterceptor())
                .AddPolicy("TestPolicy")
                .AddMatchingRule(
                    new TypeMatchingRule(typeof (ComplexEntity))
                ).AddMatchingRule(
                    new TypeMatchingRule(typeof (ComplexEntity.InnerEntity))
                ).AddMatchingRule(
                    new MemberNameMatchingRule("*")
                ).AddCallHandler(
                    new CallHandler()
                );

那么哪一个使用?为什么在单一框架中存在看似冗余的解决方案?

解决方法

答案主要是历史性的.在Unity存在之前,策略注入的东西首先出现在Enterprise Library 3.0中.需要所有匹配规则的东西,因为没有容器或中心点可以解决问题.

当我们开始将拦截集成到Unity中时,这给了我们简化事物的机会;政策注入区块的经验表明,对于很多人来说,PIAB的经验是过度的.

但是,我们不想随意使用PIAB打破所有人(我们已经做了几次)所以我们保留了新的,更简单的界面并实现了旧的界面.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读