开发者

GWT - GIN - GWTP - Dispatcher Injection Problem

开发者 https://www.devze.com 2023-04-04 18:44 出处:网络
I am in a situation where someone might be already in. I am using GWTP in my application. GWTP is using GIN as a client side

I am in a situation where someone might be already in. I am using GWTP in my application. GWTP is using GIN as a client side dependency injection. GWTP also uses Dispatcher mechanism for sending request to server side.

Now, There are some classes (PRESENTERS) which is injected by GWTP, I have some other classes which are created runtime without injection that means using "new" keywork. Let's have an example :

// Injected
class Dispatcher {
}

// Injected
Class A {
    @Inject // I can 开发者_运维知识库do this and access the disp without any hurdle.
    Dispatcher disp;
}

// Created using "new" keywork 
class B {
    //@Inject I can't do this because of B is not an injected class. Correct me if I am wrong here 
    Dispatcher disp;
}

So, I need to access Injected class in non injected classes. You might think I havn't tried GWT.Create(Injector.class) method. I have tried that and I am able to get that object in non inected classes but the object Dispatcher is something different it increments(adds) "/dispatch/" to its base url. That means i need to have the same object which GWTP internally using. And GWTP creates the GIN INJECTOR at the entry point.

One solution is to put the same GIN INJECTOR to client side session (Static Hashmap) and use it in non injected classes (I have also tried this solution and it is working like charm).

I need more perfect solution which can work.

Thank you in advance.


Injection is like a (good) virus, once you start using it, it spreads around your source code.

As you said, you cannot inject instances in objects created using the keyword 'new'. I can think of a couple of quick, dirty and not recommended alternatives:

  • When you create your injector (through GWT.create(MyInjector.class)), assign it to a public static variable. MyInjector should have a getDispatcher() method that returns the Dispatcher. This is a really dirty alternative, and I wouldn't recommend it, because every component that uses it will be very difficult to test.
  • Whose creating an instance of B? Traverse the hierarchy until you find an object that is created using injection, then inject the Dispatcher and pass it all the way down to the creator of B.

Now, what I would really recommend is to review your design. Normally, you should inject every class with business logic. If you're passing a Dispatcher, I suppose that B would execute a piece of business logic, and therefore it should be injected.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号