google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.42k stars 2.01k forks source link

More examples (Servlets, Jersey) #89

Closed husayt closed 8 years ago

husayt commented 9 years ago

Would be really useful to get more realistic examples, especially for webframeworks.

Has anyone attempted to get Dagger2 working with Jersey?

tbroyer commented 9 years ago

The problem with Jersey is that it itself implements JSR330 (through HK2), so you'd have to create a bridge between HK2 and Dagger2, and that doesn't work well because of the opposite design choices of those two libraries (you could possibly use tbroyer/bullet but that wouldn't solve the issue with scopes and having to deal with several components). Resteasy would be OK though, using a component-per-resource approach (akin to the component-per-activity approach in Android development), and similarly for servlets. The issue with JAX-RS is that of scopes: you cannot reliably make a per-request scope with JAX-RS spanning filters and resources, unless you resort to hacks (such as passing your component as a header (sic!)), or rely on being deployed in a servlet environment (where you can use HttpRequest attributes)

husayt commented 9 years ago

Does this mean then we are stuck with guice then? I was delaying with upgrading to jersey2 hoping we could switch directly to dagger. But it seems this ain't gonna happen soon.

Thankfully there seems to be finally a good bridge between jersey2 and guice. https://github.com/Squarespace/jersey2-guice

I imagine a lot of people who use Jersey and Guice have the similar dilemma.

Thanks

reinert commented 9 years ago

I imagine a lot of people who use Jersey and Guice have the similar dilemma.

o/

As a Jersey1 user, I was happy to have the freedom of choosing my preferred DI framework. Jersey2 has applied an unpleasant strategy to tie its use to H2K. The bridge approach doesn't please me because it forces us to overload the software with two DI frameworks, when one is just enough. I ended moving to Resteasy.

husayt commented 9 years ago

To change to Resteasy just because of DI is bit too much and is not a practical solution for us.

I understand that the problem is rather on Jersey side. Still would be nice to find an answer. The best solution would be for Jersey team reconsider its DI philisophy (which is very anti JAVA way btw). But since they haven't done it for Guice, I guess they wouldn't do it here. :disappointed:

cgruber commented 9 years ago

Our focus has been on Android and core functionality. That said, we want more examples, and would take contributions, provisionally. Ultimately we have plans for more robust integrations, but not in a 2.0 timeline.

ronshapiro commented 8 years ago

We'd gladly accept contributions with more examples (preferably from people that have experience with these frameworks since we don't)!