DI, dependency-injection, is a design pattern where dependencies (instances of objects, properties) of a component are set through the constructor(s), methods or fields (properties)

learn more… | top users | synonyms

13
votes
5answers
1k views

Does following TDD inevitably lead to DI?

I learned to do Test Driven Development (TDD), Dependency Injection (DI) and Inversion of Control (IoC) all at the same time. When I write code using TDD I always end up using DI in my class's ...
2
votes
1answer
227 views

DI or Factory Pattern ? Both ? or a different apprach?

Lets say we have an abstract class called BaseSwitch, inherited by concrete implementations Switch A and Switch B, Each Switch representing a real-life switch (A telephony tool which among its ...
224
votes
13answers
54k views

So Singletons are bad, then what?

There has been a lot of discussion lately about the problems with using (and overusing) Singletons. I've been one of those people earlier in my career too. I can see what the problem is now, and yet, ...
0
votes
1answer
93 views

Reducing Coupling in a Series of Tasks

I am working on some code right now that involves processing user requests. Each request requires going through an approval. When a request is made, one or more records are created on the database ...
3
votes
0answers
650 views

Passing a Context around with dependency injection

In the project I'm working on, I'm using Guice and trying to do as much as possible with Dependency Injection. However, there's one little snag; many of my objects rely on an object Context. This is ...
84
votes
18answers
8k views

Dependency injection: How to sell it

Let it be known that I am a big fan of dependency injection (DI) and automated testing. I could talk all day about it. Background Recently, our team just got this big project that is to built from ...
3
votes
2answers
530 views

What am I missing in my (mis)understanding of IoC/DI/Castle Windsor?

Okay, so here's how I understand IoC and DI in Web API to work when using Castle Windsor. Note, though, that my confidence that I understand it as I should, though, falls somewhere between my ...
9
votes
5answers
993 views

Can I use Dependency Injection without breaking Encapsulation?

Here is my Solution and projects: BookStore (solution) BookStore.Coupler (project) Bootstrapper.cs BookStore.Domain (project) CreateBookCommandValidator.cs CompositeValidator.cs IValidate.cs ...
4
votes
5answers
217 views

Loose coupling and shuffling dependencies

I have a bunch of classes that look something like this: public class MyGame() { private Graphics graphics; private Player player; public MyGame() { graphics = new ...
0
votes
1answer
236 views

High Level Configuration of Constructor Injection in C++

My questions are specifically dealing with dependency injection through the constructor. I understand the pros/cons of service locator pattern, constructor/setter injection, and their flavors, however ...
0
votes
2answers
140 views

Factory for arrays of objects in python

Ok, the title might be a little misleading. I have a Window class that draws widgets inside itself in the constructor. The widgets are all of the same type. So I pass a list of dictionaries, which ...
1
vote
1answer
111 views

Loading data for ShoppingCart item

I'm currently trying to cover our shopping cart with unit tests. Class ShoppingCartItem has property Term. It also loads available terms for this item upon object creation. Currently this is done ...
4
votes
3answers
899 views

Dependency injection: At what point am I allowed to create a new object?

I am refactoring a PHP application, and I am trying to do has much dependency injection (DI) as possible. I feel like I've got a good grasp of how it works, and I can certainly see my classes ...
2
votes
2answers
200 views

How to refactor out static members of basic classes

Currently I am working on C++ project, which can be classified as "legacy" project. A lot of poor written code was committed because of lack of experience and under the pressure of deadlines. When I ...
2
votes
2answers
348 views

Factory Pattern and/or Dependency Injection?

I understand the concept and can use both the Factory Pattern and Dependency Injection, however they seem a little at odds with each other conceptually. Is it a case of using one over the other? Or ...
0
votes
1answer
126 views

Why does the modifier access for the properties need to be public with Unity?

Why does the modifier access for the properties need to be public with Unity? I'm very interested in a detailed explanation for my question. I read about tips to do it in other ways, but not a ...
1
vote
1answer
163 views

it is a good approach to implement dependency injection in a desktop app?

Well, the thing is that I am just about to create a Desktop App (with .NET windows forms) And now I just wonder if it would be really a wise choise to use any IoC (StructureMap,Ninject,Spring .Net), ...
0
votes
2answers
427 views

Can Dependency Injection used as an alternative to Reflection for creating instances

There is a piece of .net C# code which use Reflection to create an instance using Activator.CreateInstance(). This is an old piece of code and runs on the top of Enterprise Library. Is it a better ...
0
votes
3answers
1k views

MVVM application architecture, where to put dependency injection configuration class, BusinessLayer and Common interfaces?

Planning my architecture for an MVVM application I come to this: MyApp.UI View MyApp.BusinessLayer ViewModel MyApp.DataAccessLayer RepositoryImplEF MyApp.DomainLayer DomainObject ...
0
votes
1answer
145 views

Service layer coupling

I am working on writing a service layer for an order system in php. It's the typical scenario, you have an Order that can have multiple Line Items. So lets say a request is received to store a line ...
1
vote
3answers
1k views

MVVM and service pattern

I'm building a WPF application using the MVVM pattern. Right now, my viewmodels calls the service layer to retrieve models (how is not relevant to the viewmodel) and convert them to viewmodels. I'm ...
0
votes
0answers
69 views

Software Architecture and MEF composition location

Introduction My software (a bunch of webapi's) consist of 4 projects: Core, FrontWebApi, Library and Administration. Library is a code library project that consists of only interfaces and ...
1
vote
2answers
422 views

Dependency injection with Finite State Machines and the Interface Segregation Principle

I am trying to figure out what is the best way to setup a FSM and also keep my interfaces clean and use an IOC container to use DI. Let's just visit the Head First design pattern textbook example of ...
1
vote
3answers
408 views

Dependency Injection and method signatures

I've been using YADIF (yet another dependency injection framework) in a PHP/Zend app I'm working on to handle dependencies. This has achieved some notable benefits in terms of testing and decoupling ...
0
votes
1answer
72 views

Inject specific classes or not

I have a TableSqlBuilder interface which has a method getCreateTableSql. This interface is used in some other class as follows: function createTable( $tableDefinition ) { $sql = ...
1
vote
1answer
153 views

The Nature of Spring [duplicate]

Is it accurate to say that dependency injection manually using java (without spring) is nothing but implementing the strategy pattern (as per this example)? Furthermore, is it correct to say that ...
1
vote
5answers
3k views

Dependency Injection Confusion

I think I have a decent grasp of what Dependency Inversion principle (DIP) is, my confusion is more around dependency injection. My understanding is the whole point of DI is to decouple parts of an ...
-1
votes
2answers
242 views

Generics vs IoC [closed]

One of the "Service Locator" pattern drawbacks is that a caller can be misleaded about dependencies of a callee. Ok, but what if I put dependencies as generic parameters at a class level: class ...
16
votes
8answers
1k views

Is Dependency Injection worth it outside of UnitTesting

Given a constructor that will never, ever, have to use any different implementations of several objects that it initializes, is it still practical to use DI? After all, we might still want to unit ...
41
votes
5answers
6k views

When is it not appropriate to use the dependency injection pattern?

Since learning (and loving) automated testing I have found myself using the dependency injection pattern in almost every project. Is it always appropriate to use this pattern when working with ...
6
votes
3answers
226 views

In dependency injection, is there a simple name for the counterpart of the injected object?

In tutorials and books, I have never seen a single word describing the object that the injected object is injected into. Instead, other terms are used, like "injection point" which don't denote the ...
1
vote
2answers
139 views

MVC Models instantiation

Where should the Models be instatiated in a MVC arhitecture? I understand the preferred way would be outside controllers, in a bootstrap file and injected using a Dependecy Injection Container. But ...
8
votes
4answers
730 views

How is dependency injection not just moving the complexity into a separate class?

I've been looking into using the Typhoon framework for dependency injection this week. I get that separating the construction of objects is beneficial for replacing arbitrary components with mocks ...
2
votes
1answer
158 views

Main method templating

Now that I've gotten into a dependency injection groove, I find main methods for different applications all look basically the same. This is similar to stuff you might find on the Guice documentation, ...
4
votes
4answers
537 views

Pattern to use for different Stages in a software

I am developing a software for a laboratory to test some devices. To test each device there are multi subtests which should be done to reach to the final result.To perform a complete test, the ...
0
votes
1answer
188 views

Architecting Domain Layer and other modules with dependency injection in mind

I am currently new to Dependency Injection pattern. I am influenced by link by Mark Seemann. I have a confusion regarding whether an interface for an agent class of some agent module should be ...
2
votes
1answer
918 views

DI / IoC Abstract Factory Galore

Given a project architecture as follows, where each box represents an assembly (all are class libraries), and each arrow stands for a dependency: Might be worth nothing that this project is massive ...
1
vote
1answer
178 views

Unused dependencies and constructor injection

I have a class that has 3 dependencies. WritabbleDBConnection, ReadOnlyDBConnection and a QueryFilter utility object. I want to do constructor Injection so my class would look something like this. ...
15
votes
3answers
7k views

What is constructor injection?

I have been looking at the terms constructor injection and dependency injection while going through articles on (Service locator) design patterns. When I googled about constructor injection, I got ...
1
vote
2answers
435 views

Very-Loose Coupling and Dependency Injection for Database Management

I'm currently setting up a MongoDB database for a web-app. I'm running node.js and using Mongoose to help manage mapping, validation ect. I'm wondering if it's a good idea to really decouple MongoDB ...
12
votes
5answers
4k views

Dependency injection ; good practices to reduce boilerplate code

I have a simple question, and I'm not even sure it has an answer but let's try. I'm coding in C++, and using dependancy injection to avoid global state. This works quite well, and I don't run in ...
9
votes
4answers
849 views

When to use interfaces (unit testing, IoC?)

I suspect I've made a schoolboy error here, and am looking for clarification. A lot of the classes in my solution (C#) - dare I say the majority - I've ended up writing a corresponding interface for. ...
1
vote
1answer
308 views

Configuring DI in a decoupled app

I'm reading through Mark Seeman's Dependency Injection in .NET (excellent read so far) and something is escaping me. If the application's architecture is like [DAL] => [BLL] <= [UI], with the ...
1
vote
1answer
155 views

Reducing repetitive DI code

I have a method which constructs dependencies using Unity. Currently, all of the calls do pretty much the same thing: Registers 2 repository object and 2 'App Part' objects. My issue is, there's a lot ...
1
vote
1answer
395 views

DB connection Dependency Injection and static creator methods

I often use static "factory" methods to create objects from database. class Job { protected $objDb; public function __construct($objDb) { $this->objDb = $objDb; } public static function ...
10
votes
4answers
621 views

Gradual approaches to dependency injection

I'm working on making my classes unit-testable, using dependency injection. But some of these classes have a lot of clients, and I'm not ready to refactor all of them to start passing in the ...
0
votes
2answers
209 views

Should a repository be passed in to the User Interface

In Mark Seeman's Dependency Injection in .NET he injects a repository into his UI layer. I believe that he is doing this so that he can store everything in the UI configuration, however this just ...
0
votes
2answers
369 views

when to mock in unit testing [duplicate]

I am trying to write unit tests in javascript for an application made in nodejs. What I want to know is - Say there is a class Vehicles which is dependent on another class Roads. Should I mock the ...
2
votes
3answers
252 views

Dependency injection and ease of use

I'm writing a handy library (we'll call it Thinger) that goes off and fetches an XML document, does some X-Path query on it and does something helpful with the result of that. (What I'm actually doing ...
1
vote
0answers
177 views

Single write, multiple read of stateful objects

Scenario: One data input feed (call this Source) Multiple objects (call these Layer1) read this Source, and analyze the feed. This analysis is stateful. Layer1 objects have configuration parameters. ...