Skip to main content



      Home
Home » Eclipse Projects » e(fx)clipse » Dependency Injection in Controller Classes(Should we mix constructor injection and field injection in controller classes?)
Dependency Injection in Controller Classes [message #1171515] Tue, 05 November 2013 04:31 Go to next message
Eclipse UserFriend
Hi,

I was happy to find out that e(fx)clipse's OSGiFXMLLoader could handle constructor injection (using @Inject) in controller classes. This allows me to define a non-default constructor with all the dependencies my controller needs, which is particularly useful for tests.

This approach, however, has the drawback of mixing field injection with constructor injection, as I always have field injection by the @FXML annotations. This is bad practice, of course, but one might make a case for this because we are dealing with two different and independent types of DI: the field injection is an internal feature of JavaFX, whereas the "real" dependencies are injected using the @Inject annotation.

Where this approach eventually fails is in unit tests with Mockito. Mockito uses either constructor injection or field injection. This means Mockito injects my mocks using the constructor, but does not inject the @FXML-annotated fields.

Is there a recommended way to deal with DI in controllers? Any best practices?

Thanks,
Uwe
Re: Dependency Injection in Controller Classes [message #1173112 is a reply to message #1171515] Wed, 06 November 2013 04:47 Go to previous messageGo to next message
Eclipse UserFriend
On 05.11.13 10:32, Uwe San wrote:
> Hi,
>
> I was happy to find out that e(fx)clipse's OSGiFXMLLoader could handle
> constructor injection (using @Inject) in controller classes. This allows
> me to define a non-default constructor with all the dependencies my
> controller needs, which is particularly useful for tests.
>
> This approach, however, has the drawback of mixing field injection with
> constructor injection, as I always have field injection by the @FXML
> annotations. This is bad practice, of course, but one might make a case

I don't see a problem with mixing injection types
(constructor,field,method) in one class. They simply surf different
purposes.

> for this because we are dealing with two different and independent types
> of DI: the field injection is an internal feature of JavaFX, whereas the
> "real" dependencies are injected using the @Inject annotation.

This @FXML is really a strange type of injection and from a JSR330 point
of view it is not injection at all! I see the @FXML stuff more as a
binding than an injection (did you know that freaking @FXML is not
needed at to make the controller fields fill if your field/method is
public!)

>
> Where this approach eventually fails is in unit tests with Mockito.
> Mockito uses either constructor injection or field injection. This means
> Mockito injects my mocks using the constructor, but does not inject the
> @FXML-annotated fields.

Can mockito deal with the @FXML at all?

>
> Is there a recommended way to deal with DI in controllers? Any best
> practices?
>

I'm personally not a friend of constructor injection but like
@PostConstruct much more because then @Inject-Field and method
injections are done already.

Unfortunately in the controller sense @PostConstruct could lead to the
impression that @FXML-"Binding" has been done already which is not right
but for this the initialize-method is used.

I'm not sure what to suggest :-(

Tom
Re: Dependency Injection in Controller Classes [message #1173553 is a reply to message #1173112] Wed, 06 November 2013 10:51 Go to previous message
Eclipse UserFriend
Hi Tom,

Thomas Schindl wrote on Wed, 06 November 2013 04:47

This @FXML is really a strange type of injection and from a JSR330 point
of view it is not injection at all! I see the @FXML stuff more as a
binding than an injection


And I fully agree. The problem is that Mockito thinks it's an injection.


Thomas Schindl wrote on Wed, 06 November 2013 04:47

Can mockito deal with the @FXML at all?


Yes, it can. Mockito can inject all fields annotated with @FXML for you using @Mock/@Spy. But Mockito cannot use field injection for FXML fields and constructor injection for the actual dependencies.

Thomas Schindl wrote on Wed, 06 November 2013 04:47

I'm not sure what to suggest Sad


Thanks anyway for sharing your thoughts. For the moment, I think I'll stick with pure field injection.

Uwe

[Updated on: Thu, 09 January 2014 05:29] by Moderator

Previous Topic:fxGraph and HBox layout
Next Topic:menu mnemonics
Goto Forum:
  


Current Time: Tue Apr 22 16:19:17 EDT 2025

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

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

Back to the top