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


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

Re: How to make my library independent using DI

From Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: How to make my library independent using DI
Date 2019-02-04 16:33 +0100
Organization A noiseless patient Spider
Message-ID <q39m0g$l2$1@dont-email.me> (permalink)
References <c9bc00ee-744c-49e2-b026-08a68841cf48@googlegroups.com>

Show all headers | View raw


On 2019-01-29 19:11, mike wrote:
> Hi,
> 
> I am creating a library that can be used with different java applications.
> Sometimes I need data from different applications but I want to avoid a direct dependency to these applications.
> 
> In my library I have the following classes:
> 
> package scrap;
> 
> 
> public interface MyDataClass {
>     String productNumber();
>     String version();
>     String time();
> 
> 
> }
> 
> and 
> 
> public class MyDataClassImpl implements MyDataClass{
>     
>     private ExternalDataService externalDataService;
>     
>     public MyDataClassImpl(ExternalDataService externalDataService){
>         this.externalDataService = externalDataService;
>     }
> 
>     @Override
>     public String productNumber() {
>       //Here we have a dependency to an external library call it x
>        return externalDataService.productNumber();
>     }
> 
>     @Override
>     public String version() {
>         //Here we have a dependency to an external library call it x
>        return externalDataService.version();
>     }
> 
>     @Override
>     public String time() {        
>       return calcTime();
>     }
>     
>     
>     private String calcTime () {
>         return "";
>         
>     }
> }
> 
> as well as:
> 
> package scrap;
> 
> 
> public interface ExternalDataService {
>     
>     public String productNumber();
>     
>     public String version();
> 
> }
> 
> 
> Then in the application using this lib I have:
> 
> package other;
> 
> import scrap.ExternalDataService;
> 
> public class AHelper implements ExternalDataService{
> 
>     @Override
>     public String productNumber() {
>         return methodA();
>     }
> 
>     @Override
>     public String version() {
>         return methodB();
>     }
>     
>     
>     public String methodA() {
>         return "";
>     }
>     
>     String methodB() {
>         return "";
>     }
> }
> 
> I have two questions:
> 
> 1. How can I inject AHelper into MyDataClassImpl without creating a "hard dependency" without using a DI container?
> 2. If I used a container like Guice how would that look like? 
> 
> All hints welcome.
> 
> br,
> 
> Mike
> 

You don't need an "ExternalDataService". It doesn't matter whether the
data is internal or external. You just need a data class and a data
provider class.

public interface Data {
  String productNumber();
  String version();
  String time();
}

public interface DataProvider
  extends Supplier<Data>
{}

In order to get hold of DataProvider instances, I would suggest looking
into the java.util.ServiceLoader mechanism (see Javadoc
<https://docs.oracle.com/javase/10/docs/api/java/util/ServiceLoader.html>).

In your own API then, you simply try to load a DataProvider. If you find
one, get your data from there. If not, simply throw up.

-- 
DF.

Back to comp.lang.java.programmer | Previous | NextPrevious 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