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

12
votes
7answers
2k views

What is the “right” way to implement DI in .NET?

I'm looking to implement dependency injection in a relatively large application but have no experience in it. I studied the concept and a few implementations of IoC and dependency injectors available, ...
0
votes
1answer
42 views

MSDN - ServiceLocation Outdated Why? Alternatives?

I recently (1 month) start working in a project and as I was going through the code I saw an using Microsoft.Practices.ServiceLocation and as I searched for the documentation on that library is says: ...
1
vote
1answer
245 views

Data serialization architecture, injection on construction vs. on function call

I am creating a data serialization/deserialization mechanism for essentially a persistent storage object. Due to the variety of systems this mechanism could run on, there needs to be a a variable ...
4
votes
4answers
345 views

Are there any alternatives to dependency injection for stateless classes?

I am working on an application whereby I have designed classes to fit into several groups: Immutable: initialised through constructors, uses copy-and-swap idiom (inc. move), can deep copy (i.e. ...
7
votes
2answers
821 views

Does Serialization preclude the use of Dependency Injection?

Simple question: I understand that Serialization in C# requires default constructors. This would eliminate the possibility of using Constructor injected DI (which is generally the favored style of DI, ...
0
votes
0answers
23 views

Do you inject everything explicitly used in a module even if it is loaded in a “parent” module?

When you build a app modularized what do you inject into submodules? Only those things that are used there but not added at the top level, or do you inject everything that is explicitly used in that ...
8
votes
2answers
612 views

Who should initialize dependencies in a TDD application?

I'm trying to learn implementing TDD with mocking/fake objects. One of the questions I have is how to initialize a dependency in an application which implements TDD? An example from this article ...
2
votes
4answers
220 views

Class Library - How to communicate between objects that are not aware of each other?

I am trying to improve my programming skills and knowledge and I have been doing some reading about various design patterns and youtubing videos etc. One subject I thought was interesting was about ...
7
votes
4answers
2k views

Is dependency injection by hand a better alternative to composition and polymorphism?

First, I'm an entry level programmer; In fact, I'm finishing an A.S. degree with a final capstone project over the summer. In my new job, when there isn't some project for me to do (they're waiting ...
0
votes
2answers
92 views

Injecting a factory with multiple constructor parameters

Initially I needed only one queue to be created by the MessageQueueFactory: container.RegisterSingleton<IMessageQueueFactory>(() => { var uploadedWaybillsQueuePath = ConfigurationManager ...
3
votes
1answer
168 views

Dependency Injection & In-class Instantiation | Practical Limitations

Dependency Inversion is Good Inversion of dependency is good, it: Simplifies unit-testing Reduces coupling, allowing software components to be used interchangeably Keeps instantiation logic for a ...
9
votes
4answers
499 views

Dependency Inversion expands the API, results in unnecessary tests

This question has bothered me for a few days, and it feels like several practices contradict each other. Example Iteration 1 public class FooDao : IFooDao { private IFooConnection fooConnection;...
0
votes
1answer
64 views

Dependency injection multiple instances or only one InrequestScope

I'm trying to understand what happens with dependencies (Ninject) when a same Interface in injected in multiple classes. I am specifying InRequestScope for my bindings so as I understand there should ...
0
votes
1answer
83 views

Dependency Injection (DI) and Dependency Inversion Principle (DIP) for built-in functions and external libraries

Question I apologize if this question has already been posted. I tried searching but I couldn't find the answer. I often find myself trying to use DI and DIP where I need to open a file or a socket. ...
3
votes
3answers
132 views

Ambient dependency injection through static service locator

After some googling I found some debates about whether constructor injection or property/field injection is better, but there is yet another alternative that strikes me as more beneficial. In most ...
1
vote
1answer
67 views

How broad is the definition of “dependency injection”?

What is the "essence" of dependency injection? Is it the idea of dynamically swapping out/in core logical/structural aspects of a program at runtime? Traditionally, this is done in code via some DI ...
8
votes
4answers
774 views

How could dependency injection be integrated into the language? [closed]

I've been thinking a bit on how dependency injection could be better integrated directly into a C# like language. I've come up with a potential solution I'd like to hear your opinion on. I haven't ...
40
votes
11answers
3k views

Are so called “cross-cutting concerns” a valid excuse to break SOLID/DI/IoC?

My colleagues like to say "logging/caching/etc. is a cross-cutting concern" and then proceed using the corresponding singleton everywhere. Yet they love IoC and DI. Is it really a valid excuse to ...
5
votes
6answers
396 views

Dependency injection: should I use a framework?

I have recently worked on a Python project where we did dependency injection heavily (because we must in order for the app to be testable), but we didn't use any framework. At times it was a little ...
5
votes
4answers
208 views

Does anyone have a good DI registration pattern?

I've done this so many times but I never end up liking how my code turns out. I'm asking if anyone else has the same problems and can offer solutions or conventions that they've used to make cleaner ...
13
votes
3answers
547 views

Do dependency injection frameworks pose a dependency risk?

I have been refactoring an existing system to use dependency injection, and that work has been going smoothly. After a while I noticed that a large number of in-house libraries became dependent upon ...
0
votes
1answer
67 views

Separate interfaces for dependency data providers?

I am attempting to refactor a component of a large project, which currently has a lot of dependencies to global state of the project environment. The goal (for my taste) is a "SOLID" architecture, ...
27
votes
11answers
2k views

Did a gradual shift in methodology of writing code affect system performance? And Should I care?

Lest you be confused by what's to follow, here is the driving idea behind the question: I always intended the question to be what it is. I may not have articulated it well originally. But intent ...
72
votes
5answers
20k 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 ...
1
vote
1answer
163 views

Should composition happen no matter what?

I feel like I should be able to register all my components into the application composition root container no matter what the current state of the environment is, even in the case environment lacks ...
6
votes
2answers
442 views

How to avoid a crazy amount of interfaces in UI with dependency injection?

Problem I recently read a lot about Singletons being bad and how dependency injection (which I understand as "using interfaces") is better. When I implemented part of this with callbacks/interfaces/DI ...
0
votes
3answers
64 views

Value of dependency injection in MVC with skinny controllers

Dependency injection is often recommended in MVC projects because it's easy to implement with the pattern and adds a host of potential benefits. Indeed, it actually comes bundled with the default MVC ...
0
votes
1answer
57 views

Abstract Factories Nesting and DI - Is this acceptable?

I am still trying to get some experience with DI. Let me illustrate my situation and you can give me advice on whether I am using DI and Abstract Factory correctly. I need to create IModels at ...
14
votes
5answers
3k 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. ...
12
votes
4answers
1k views

This way I'm writing this code is testable, but is there something wrong with it I'm missing?

I have an interface called IContext. For the purposes of this it doesn't really matter what's it does except the following: T GetService<T>(); What this method does is look at the current DI ...
7
votes
2answers
169 views

Gradually move codebase to dependency injection container

I have a large codebase with a lot of "anti-pattern" singletons, utility classes with static methods and classes creating their own dependencies using new keyword. It makes a code very difficult to ...
1
vote
3answers
107 views

SOLID principles and generating many objects from a file

The "D" in SOLID stands for dependency inversion. If I have for example, a Blacksmith class that depends on Hammer, I should create my Hammer externally and pass it to Blacksmith, rather than having ...
5
votes
2answers
4k 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 ...
2
votes
1answer
82 views

Constructor Injection vs Field Injection

I know this topic has been discussed a lot already so I don't want to get into which one is the "best way". I have been using field injection for a couple of years now but recently I discovered that ...
4
votes
2answers
118 views

Dependency Injection use in the “Clean Code” by R. Martin

In the Clean Code book by R.Martin, I have encountered the following method on p. 195: private void parseSchemaElement(String element) throws ArgsException { char elementId = element.charAt(0); ...
6
votes
3answers
194 views

How many injections is acceptable in one class when using dependency injection

I'm using Unity in C# for dependency injection, but the question should be applicable for any language and framework that is using dependency injection. I'm trying to follow the SOLID-principle and ...
5
votes
4answers
214 views

How does a Dependency Injection/IOC Container know which implementation to use?

When you use an IoC container, like so: var svc = IoC.Resolve<IShippingService>(); How does the IoC Container choose which implementation of IShippingService to instantiate? Further, if I am ...
2
votes
4answers
257 views

Reusing the logic in another repository in ASP.NET MVC repository pattern

I have an ASP.NET MVC4 web application, with EntityFramework for data access. In the application I have two repositories as follow (this is only an example not my real code): public class ...
4
votes
2answers
340 views

Are there any reasons not to use a DI framework on a standalone project?

While researching technologies for a new "pet project" I analyzed a few well-established project's source code and I noticed that they hardly use any dependency injection frameworks at all. For ...
3
votes
2answers
101 views

Repository pattern and Dependency Injection without an ORM

I want to implement the repository pattern in an old legacy project. I already did some research, and found that, without an ORM, it's completely acceptable to have methods like "GetBySpecialProperty"...
3
votes
0answers
65 views

Merits of DI, Abstract Factory implementation review

I'm writing a brand new system and trying to stay true to the SOLID principles, specifically Interfacing and Dependency Injection. We are sticking closely to the Microsoft stack so we are using C# ...
2
votes
0answers
54 views

Mocking third party web service

I am working on a project to create a wrapper library which will be used to interact with a web service provided by the client's IT team. The reason for this library is that we interact with this ...
0
votes
1answer
95 views

Conditionally creating objects at runtime using Spring.NET

I have a couple classes in a project I'm working on that only register event handlers. public class EventLogger { public EventLogger(EventRaiser someObject) { someObject.EventRaised ...
3
votes
4answers
3k views

How to handle “circular dependency” in dependency injection

The title says "Circular Dependency", but it is not the correct wording, because to me the design seems solid. However, consider the following scenario, where the blue parts are given from external ...
4
votes
1answer
209 views

Do I have to stop using Dependency Injection to keep object debug printouts small?

Say I have a large object - think EntityManager of an ORM such as Doctrine, or a custom DAO object, or what have you. Object, output of which is required to be used inside a class, but the object ...
2
votes
1answer
133 views

Using Spring in Java Project

I've got a question about a correct usage of Spring. I know that some people use the DI "aggressively" so that they always use spring and completely eliminate the usage of word "new" in the ...
1
vote
1answer
118 views

How far do I separate Model code from Controller code when writing MVC?

I have some code where Controller depends on the Model, and in my case Model acts like a Database Access Object. use FQDN\Model; class Controller { /** @var FQDN\Model */ private $model; ...
3
votes
2answers
348 views

Only use a dependency injection container at the root of an application?

normally, I'll use a dependency injection container (unity) in c# like this example: class SomeClass { private readonly ILogger _logger; public SomeClass() { _logger = ...
9
votes
2answers
2k views

Problem with understanding “seam” word

I'm reading "Dependency Injection in .NET" by Mark Seemann (it's fantastic, and must have) and the author often uses the word "seam". But I can't understand what it means. Here is an example of using ...
1
vote
3answers
1k views

How to use Guice for an effective API Design?

I am creating a base API in JavaSE, which includes modules like MVP architecture, Service & Repository Layer, Event Model to fire events between presenter etc. I am trying to implement all best ...