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

Path csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
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 Mon, 4 Feb 2019 16:33:34 +0100
Organization A noiseless patient Spider
Lines 130
Message-ID <q39m0g$l2$1@dont-email.me> (permalink)
References <c9bc00ee-744c-49e2-b026-08a68841cf48@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 7bit
Injection-Date Mon, 4 Feb 2019 15:33:37 -0000 (UTC)
Injection-Info reader02.eternal-september.org; posting-host="e2fc2f58b26de9be6948b64feb8df723"; logging-data="674"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tcam2Bq7sAhG2E00SP7wr"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.0
Cancel-Lock sha1:82FaXL8UHr0xX77Xl3TsKeDRXt0=
In-Reply-To <c9bc00ee-744c-49e2-b026-08a68841cf48@googlegroups.com>
X-Antivirus-Status Clean
Content-Language en-US
X-Antivirus AVG (VPS 190204-2, 02/04/2019), Outbound message
Xref csiph.com comp.lang.java.programmer:38724

Show key headers only | 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