It almost seems like Unity is providing 2 different routes to achieve AoP functionality.

The question is why? What are the differences? What would be the pros and cons of each approach?

For example using ICallHandler:

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

But a similar functionality can also be achieved using IInterceptionBehavior in place of ICallHandler

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

There is also a hybrid somewhere that lets you set the interception but uses a call handler eg.

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()
                );

So which one to use? Why are there seemingly redundant solutions in a single framework?

有帮助吗?

解决方案 2

Nevermind I was not looking carefully enough.

You can create your own InterceptionBehavior, but this would only apply to the class, or you can use the library provided PolicyInjectionBehavior, which then uses ICallHandler and policies.

So the difference ends up like a simple vs. a multi cast delegate. The policy injection allows you to define the pointcut using a container wide query (multi-cast) and apply the advice against multiple types that match the query, whereas the IInterceptionBehavior allows you to apply specific advice against specific type only (single-cast).

The PolicyBehavior is an implementation of IInterceptionBehavior that provides the multi casting functionality.

其他提示

The answer is primarily historical. The Policy Injection stuff came first, in Enterprise Library 3.0, before Unity existed. All the matching rule stuff was needed because there was no container or central point to hook things up.

When we started work on integrating interception into Unity, this gave us the opportunity to simplify things; experience with the policy injection block indicated that for a lot of people the PIAB experience was overkill.

However, we didn't want to arbitrarily break everyone using PIAB (we'd already done that a couple of times) so we kept the new, simpler interface AND implemented the old one as well.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top