I'm using Autofac with DynamicProxy2 to intercept my classes like this
builder.RegisterType<Calculator>().As<ICalculator>().EnableInterfaceInterceptors()
.InterceptedBy(typeof (CallLogger));
This will intercept all methods on given interface.
Is there any way to intercept only particular methods of the interface ?
There are a couple of ways I can think of, and there are probably more out there.
You could create a custom attribute like [LogCall] or [DoNotLogCall] (opt in or opt out) and apply that to the methods to intercept, then check for the presence of the attribute inside CallLogger.
Or, you could configure the CallLogger with the names of (or rules to find) the methods that should be logged.
Related
I read about guice AOP in the documentation here :- https://github.com/google/guice/wiki/AOP
From the documentation :-
Behind the scenes, method interception is implemented by generating bytecode at runtime. Guice dynamically creates a subclass that applies interceptors by overriding methods. If you are on a platform that doesn't support bytecode generation (such as Android), you should use Guice without AOP support.
What does the documentation means by extending the subclass. Is this the method interceptor subclass? Does it work by using reflection to intercept the method? The reason I ask this question is because in the documentation, this line is followed by this :-
Due to this, we have the following limitations on the guice AOP
a) AOP cannot be applied to a private method.
Unclear to me why AOP cannot be applied to a private method.
Unclear to me why AOP cannot be applied to a private method.
Because private methods are not inherited by subclasses, i.e. there is no way to intercept a private method and then delegate to it because the subclass cannot even call that method. This is a normal Java limitation and has nothing to do with AOP specifically.
BTW, if you want a full-fledged, powerful AOP tool working with any JVM language and able to intercept private methods without the need to resort to dynamic proxies (subclasses created during runtime like Guice or Spring AOP), just use AspectJ.
What does the documentation means by extending the subclass.
The JRE knows the concept of dynamic proxies in order to be able to intercept method calls and (optionally) delegate to the original call before/after doing something else in an interceptor method. This works for interfaces only, but CGLIB extends this concept to subclasses of non-interface classes, which is why in proxy-based AOP frameworks such as Spring AOP (and maybe Guice, not 100% sure) proxies are used in order to implement AOP. AspectJ works differently, it does not use or need any dynamic proxies or other types of subclasses.
I read this on a guideline
"When using Spring AOP always use interfaces so normal AOP proxying can be used (rather than CGLIB)"
What could than mean? I have very good idea on AOP and have implemented AOP myself before. But completely out of clue.
Simply said there are 2 way for proxying an object :
dynamic : you create a new object that implement the same interface than the target object and encapsulate this last in addition with the proxy logic
static : when you compile the target class you add the aspect logic directly into the compiled class (cglib way)
Dynamic proxy can only apply on object that implement an interface and will be used only if you reference the instance using the interface (which is advised here) whereas static proxy can apply on everything
Following the spring guideline will allow you to use any of the method (you can simply switch using spring configuation) instead of being coupled to cglib.
Note than static proxy allow proxy logic to be applied even when you call a proxied method from another method inside the same class whereas with dynamic proxy the call must come from outside.
For the case of a project requirement, I need to instantiate WsdlContractConversionContext which is not having a constructor for doing so.
Is there any work around to achieve this?
WsdlContractConversionContext is a member of the System.ServiceModel.Description namespace.
Note:
The requirement exactly is that, I am doing an implementation of IWsdlExportExtension.ExportContract and IWsdlImportExtension.ImportContract, and to unit test this implemetation I need the instance of WsdlContractConversionContext.
There are basically two ways to do that: you can either use reflection to call the non-public constructor of the class (making sure you're passing appropriate parameters to it); or you can let WCF create it for you, and use it wherever you need. The WsdlContractConversionContext is passed as one of the parameters to either IWsdlExportExtension.ExportContract or an IWsdlImportExtension.ImportContract, so you'd need to implement one of the two interfaces (exporting is usually easier, since you won't need to fiddle with WSDL-consuming tools), and force the interface to be called (you may need to hit the service metadata endpoint for that).
The post at http://blogs.msdn.com/b/carlosfigueira/archive/2011/10/06/wcf-extensibility-wsdl-export-extension.aspx has an example of an implementation of a WSDL export extension.
Update following edit in the question: many parts of WCF are notoriously hard to be unit tested. If you can't use WCF itself to create the instance, the only alternative you have is to use reflection. To create an instance of the conversion context class you need an instance of a ContractDescription (which you can create for your contract, but isn't easy), and a PortType, which is even harder. I'm afraid that unit testing your implementation of the WSDL export / import extension may not be worth the effort.
I'm engaged in building NServiceBus Gateway handler, and I need to avoid config files so that all configuration is defined inside c# classes. As a result I have to convert the following section to c# code
<GatewayConfig>
<Channels>
<Channel Address="http://localhost:25899/SiteB/" ChannelType="Http" Default="true"/>
</Channels>
</GatewayConfig>
I've found GatewayConfig, ChannelCollection and ChannelConfig in a NServiceBus.Config namespace, but I can not link them together, coz GatewayConfig refers to ChannelCollection, but ChannelCollection has nothing to do with ChannelConfig. Please help
Just create a class implementing IProvideConfiguration of GatewayConfig. That gives you a way to provide your own config. Look at the pubsub sample for the exact details on how to do this.
Well, I've found the way to do it as I installed Reflector and looked into the implementation. There is a ChannelCollection.CreateNewElement() method returning back System.Configuration.ConfigurationElement. NServiceBus overriden the method instantiating ChannelConfig inside it, so all I have to do is to cast ConfigurationElement type to ChannelConfig type which is far from intuitive interface. Looks like this NServiceBus.Config.ChannelCollection is kind of unfinished work, because if you look at other collections like NServiceBus.Config.MessageEndpointMappingCollection you can find there all necessary type-safe methods to work with its child elements NServiceBus.Config.MessageEndpointMapping, so I think NServiceBus team was just lazy to make the same for ChannelCollection.
UPDATE: as CreateNewElement() method is protected, I have to implement my own class inherited from ChannelCollection to make a method adding new ChannelConfig element publicly available
We use RhinoMocks. I have a type into whose constructor 9 types are injected. I'd like a way of automocking the type, but being able to detect a particular method invocation on one of the injected objects (i.e. I only care about a single method invocation on one of the injected objects).
Is this possible, or do I have to manually inject all the mock objects into the constructor?
I haven't seen any frameworks that would auto-create these mocks for you. You can do it in your [SetUp] method, so at least the tests will not be cluttered with boilerplate code.
I need to check out http://autofixture.codeplex.com/. Its not really container specific, there is an extension for rhino mocks. Disclaimer: I haven't tried autofixture yet.