Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » How to do ContextInjectionFactory.make with paramatrized constructor?
How to do ContextInjectionFactory.make with paramatrized constructor? [message #1002456] Fri, 18 January 2013 16:10 Go to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 260
Registered: July 2012
Senior Member
I have a widget named TableToolbar which has a parametrized constructor.
class TableToolbar{

@Inject 
MyData data;

public TableToolbar(Composite parentComposite) {
func(data);
...
}



I would like to Inject the data from IEclipseContext into it, before starting the constructor.
This I usually do with
ContextInjectionFactory.make(TableToolbar, eclipseContext);



Now I have a specific Composite, which I need to pass a specific Composite to the TableToolbar constructor.
Is it possible?

P.S.
I can not inject the data later by
TableToolbar tableToolbar= new TableToolbar(composite);
ContextInjectionFactory.inject(tableToolbar, context);


because the injected data are needed inside the constructor.
Re: How to do ContextInjectionFactory.make with paramatrized constructor? [message #1002555 is a reply to message #1002456] Fri, 18 January 2013 20:38 Go to previous messageGo to next message
Frank Benoit is currently offline Frank BenoitFriend
Messages: 179
Registered: July 2009
Senior Member
How about create a child context and set your parentComposite there, then use it to make the object

c2 = context.createChild();
c2.set( Composite.class, myParentComposite );
ContextInjectionFactory.make(TableToolbar.class, c2);


Frank
Re: How to do ContextInjectionFactory.make with paramatrized constructor? [message #1002597 is a reply to message #1002555] Fri, 18 January 2013 22:52 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
That's correct still he'll fail because workflow is this:

a) run constructor code
b) do field injection
c) do method injection
d) run @PostConstruct

Hence he'll has to have MyData as an constructor argument.

Tom

Am 18.01.13 21:38, schrieb Frank Benoit:
> How about create a child context and set your parentComposite there,
> then use it to make the object
>
>
> c2 = context.createChild();
> c2.set( Composite.class, myParentComposite );
> ContextInjectionFactory.make(TableToolbar.class, c2);
>
>
> Frank
Re: How to do ContextInjectionFactory.make with paramatrized constructor? [message #1002937 is a reply to message #1002597] Sat, 19 January 2013 22:27 Go to previous messageGo to next message
Eclipse UserFriend
This doesn't make sense because you can not expect a (non-static)field of an object to be set before its constructor returns (i.e. the object is created). First you have to have an object (which you don't have until the constructor returns) and then set its fields. DI can't break these rules.
Re: How to do ContextInjectionFactory.make with paramatrized constructor? [message #1003444 is a reply to message #1002937] Mon, 21 January 2013 09:02 Go to previous messageGo to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 260
Registered: July 2012
Senior Member
For now I had to move all the constructor logic into a separate method init().



  1. Now I can manually create the Object, with a concrete composite as parameter.
  2. Then I Inject the missing fields, by using the ContextInjectionFactory
  3. Then I call the init(), which contains all the constructor logic using injected variables.


With other words I am changing the lifecycle, to do the injection, before calling the constructor.


class TableToolbar{

@Inject 
MyData data;

public TableToolbar(Composite parentComposite) {}

public void init(){
 func(data);
 ...
}

        TableToolbar toolbar = new TableToolbar(containerTop);
        ContextInjectionFactory.inject(toolbar, context);
        toolbar.init();
Re: How to do ContextInjectionFactory.make with paramatrized constructor? [message #1003461 is a reply to message #1003444] Mon, 21 January 2013 09:35 Go to previous messageGo to next message
Eclipse UserFriend
I didn't look at the code but you can not do any injection except constructor argument injection before the constructor returns.

Why don't you use @PostConstruct? Remove the @Inject from data and, run your constructor and add a method :

@PostConstruct
public void init(MyData data){
this.data=data;
func(data);
}

and you don't need CIF or manually call init.
Re: How to do ContextInjectionFactory.make with paramatrized constructor? [message #1003557 is a reply to message #1003461] Mon, 21 January 2013 14:10 Go to previous message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 260
Registered: July 2012
Senior Member
Thnx Sopot, now my code looks as following:

class TableToolbar{

@Inject 
MyData data;

public TableToolbar(Composite parentComposite) {}

@PostConstruct
public void init(){
 func(data);
 ...
}


TableToolbar toolbar = new TableToolbar(containerTop);
ContextInjectionFactory.inject(toolbar, context); //calls @PostConstruct annotated method


I still have to inject manually, because I would like to create the TableToolbar myselfe. It makes the code more understandable, than using Tom's version, with a child container:
c2 = context.createChild();
c2.set( Composite.class, myParentComposite );
ContextInjectionFactory.make(TableToolbar.class, c2);
Previous Topic:DI : Define custom parent Composite for inject
Next Topic:Do Addons have another Context, than the parts?
Goto Forum:
  


Current Time: Thu Mar 28 12:30:12 GMT 2024

Powered by FUDForum. Page generated in 0.02543 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top