GWT.create(Class<?>) vs. GIN?
Asked Answered
J

2

6

It looks like GWT has its own baked-in DI mechanism (GWT.create(Class<?>)). What benefits does GIN offer on top of this? Should you ever use them in conjunction with one another, or are they mutually exclusive? I like Guice so I'm tempted to use GIN, but don't want to introduce it if GWT already does the same stuff right out of the box.

Jhelum answered 23/10, 2012 at 12:30 Comment(0)
K
8

Gin and GWT.create have a few differences - Gin is more about providing dependencies via the @Inject annotation, either on fields, setters, or the constructor, whereas GWT.create is specifically about obtaining an implementation. Gin will use any constructor you provide, but you must specifically provide the replacement type, whereas GWT.create will only work with a default constructor, and your 'rebind rules' don't need to be quite as precise, and can even cause new classes to be created at compile time. It also is able to look at what environment the user is running and select a specific set of rules based on that, which Gin is not able to do.

Gin actually makes use of GWT.create to get these other features - if you don't have a rule defined, Gin will call GWT.create automatically. This means if you have a rule like

@Inject MyRemoteServiceAsync rpcService;

Gin will call GWT.create to build that rpc call. This also works for UiBinder, Editor Drivers, I18n Messages and Constants, etc.

If you already understand and like Guice, Gin shouldn't be a big step for you, and you'll still have the ability to directly invoke GWT.create, or implicitly ask Gin to do it. In fact, in order to even use Gin, you need to call GWT.create(MyGinjector.class) to get the ball rolling.

Kirkman answered 23/10, 2012 at 13:14 Comment(0)
P
8

GWT.create() has nothing to do with dependency injection. It's a factory method.

The only difference between GWT.create(SomeThing.class) and new SomeThing() is that the exact SomeThing class can be replaced or generated using so-called deferred binding. But there's nothing in GWT.create() about providing dependencies to the GWT.create()d object for instance.

Predicable answered 23/10, 2012 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.