Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38721

Re: How to make my library independent using DI

From Andreas Leitgeb <avl@logic.at>
Newsgroups comp.lang.java.programmer
Subject Re: How to make my library independent using DI
Date 2019-01-30 10:11 +0000
Organization A noiseless patient Spider
Message-ID <slrnq52u2s.k8o.avl@logic.at> (permalink)
References <c9bc00ee-744c-49e2-b026-08a68841cf48@googlegroups.com>

Show all headers | View raw


mike <mikaelpetterson@hotmail.com> wrote:
> In my library I have the following classes:
> public interface MyDataClass {
>    [...]
> }
> public class MyDataClassImpl implements MyDataClass{
>     private ExternalDataService externalDataService;
>     public MyDataClassImpl(ExternalDataService externalDataService){
>         this.externalDataService = externalDataService;
>     }

I'd consider this design already fine w.r.t. independence of
lib from app.

>     public String productNumber() {
>       //Here we have a dependency to an external library call it x
>        return externalDataService.productNumber();
>     }

You might want to wrap this with a null check for the field, such that
if a user of the lib doesn't provide an implementation (but instead null),
they could still get around.

If the lib doesn't make sense without at least some implementation of
ExternalDataService, then you can leave it as is here, and instead let
the constructor throw some exception for a null argument. There might
even be @NotNull annotations, but I don't know how reliable they are,
or whether they are already available in the versions of Java you use.

> public interface ExternalDataService {
>     public String productNumber();
>     public String version();
> }
>
> Then in the application using this lib I have:
> public class AHelper implements ExternalDataService{
>    [...]
> }
> I have two questions:
>
> 1. How can I inject AHelper into MyDataClassImpl without creating
> a "hard dependency" without using a DI container?

Not sure what you mean by inject, but according to your code snippet,
there is no "hard dependency" to AHelper in your lib.

Unless you strictly disallow "null", there isn't even a necessity
that a user even implements ExternalDataService at all.

> 2. If I used a container like Guice how would that look like? 

I don't know Guice, so can't tell.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to make my library independent using DI mike <mikaelpetterson@hotmail.com> - 2019-01-29 10:11 -0800
  Re: How to make my library independent using DI Martin Gregorie <martin@mydomain.invalid> - 2019-01-29 19:29 +0000
  Re: How to make my library independent using DI Andreas Leitgeb <avl@logic.at> - 2019-01-30 10:11 +0000
    Re: How to make my library independent using DI mike <mikaelpetterson@hotmail.com> - 2019-01-30 06:13 -0800
      Re: How to make my library independent using DI Andreas Leitgeb <avl@logic.at> - 2019-02-01 14:13 +0000
  Re: How to make my library independent using DI Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-02-04 16:33 +0100

csiph-web