Until now I've been using a delegation pattern to implement AOP-style method handling via guice.
So I'll create a dependency of a class on a the same interface it implements, call it a 'delegate' , then within the method call I'll call the same method on the delegate. This way I can wrap that method with logging, authentication, etc. The problem is that I have to create a new binding each time and I don't like doing it because its hard to test, difficult to understand, etc. I could use Guice AOP, but I'm not totally in love with it. The invoke() style handling can lead to bugs where you don't handle all methods correctly. By forcing the developer to implement an interface, if you add a new method their code doesn't compile and you immediately see that you have to figure out how to handle the new method. Now, perhaps one strategy is to just have one method per interface. But that's not ideal either. I can't see a way to enforce it. And additionally, it's going to lead to piling up of methods. One thing I've thought of is that instead of injecting an interface, I could inject the implementation. So if I had an AuthenticatingFoo it would depend on LoggingFoo which would depend on TracingFoo (for stats), which would depend on DefaultFoo (the actual, final implementation). The problem with this strategy , is that while it's easy to use, you're kind of tied to that specific pipeline. For example, you can't go right from AuthenticatingFoo to DefaultFoo in tests... Nor can you make any of the dependencies Mocks for testing because they depend on a specific implementation. Which means they can't really be tested. ... another pattern I thought of is a sort of pipeline, where you create a new class which depends on injection in the constructor, requires ALL the binding implementations in the constructor, and then manually calls setDelegate() on each of the underlying implementations. this would be a bit easier but would require some code to handle. Maybe there isn't an elegant solution here... Anyone have any recommendations? -- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/google-guice. To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/4b7dbd39-ea8e-465f-93a3-f5edab3b9499%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
