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


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

Re: DI/wiring

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx05.eternal-september.org!.POSTED!not-for-mail
From markspace <markspace@nospam.nospam>
Newsgroups comp.lang.java.programmer
Subject Re: DI/wiring
Date Thu, 18 Apr 2013 12:16:56 -0700
Organization A noiseless patient Spider
Lines 43
Message-ID <kkpgls$6aq$1@dont-email.me> (permalink)
References <DI-wiring-20130418174944@ram.dialup.fu-berlin.de>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 8bit
Injection-Date Thu, 18 Apr 2013 19:14:04 +0000 (UTC)
Injection-Info mx05.eternal-september.org; posting-host="fba3415ba68d85d643935af2f52f0b4b"; logging-data="6490"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19388IXbWjXlraRR0Q2XDy1NOc124nbHKw="
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5
In-Reply-To <DI-wiring-20130418174944@ram.dialup.fu-berlin.de>
Cancel-Lock sha1:yPjs7rsxpI9d/fy+KVGnma7U770=
Xref csiph.com comp.lang.java.programmer:23508

Show key headers only | View raw


On 4/18/2013 9:03 AM, Stefan Ram wrote:
>    I have »invented« myself something that also seems to be
>    known as »dependency injection« or »wiring«. I am doing it

I should have a look at Joerg's answer myself, but here's another idea.

Make an app context object that holds the state of your app.

class Context {
   Engine getEngine()....
   Printer getPrinter()...
}

Inject that into submodules in the code.  Submodules are larger than 
classes and can treat the context as immutable.  The context then 
becomes global but only for that module.  The idea is that you still get 
dependency injecting, but you can still test reasonably as well.

class SomeConfig {
   Context context;
   public SomeConfig( Context c ) { context = c; }

   public init() {
    Component c0 = new Component( context );

...
   }
}

class Component  {
   Context context;...
   public printIt() {
     context.getPrinter().do something...
   }
}

This reduces boilerplate but doesn't eliminate it.  For some projects it 
might be better to get a real dependency injection framework.




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


Thread

Re: DI/wiring markspace <markspace@nospam.nospam> - 2013-04-18 12:16 -0700
  Re: DI/wiring Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-04-22 09:03 +0200
    Re: DI/wiring markspace <markspace@nospam.nospam> - 2013-04-22 10:59 -0700
      Re: DI/wiring Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-04-24 06:37 +0200
      Re: DI/wiring Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-04-24 10:28 -0300

csiph-web