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
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 ...
3
votes
2answers
254 views

When is using DI and optionally a IoC framework a step too far?

Consider a logging system - used absolutely everywhere in your codebase. (note - logging is just an example, don't take it too literally and point me at your favourite logging system). public ...
1
vote
2answers
1k views

What are the best practices to use NHiberante sessions in asp.net (mvc/web api) ?

I have the following setup in my project: public class WebApiApplication : System.Web.HttpApplication { public static ISessionFactory SessionFactory { get; private set; } public ...
0
votes
1answer
659 views

How to make Ninject inject an MVC view model?

How do I configure Ninject so it injects an instance in a view model? PS. It is an abstracted service to help with validation, so I am not trying to burden the model with too much responsibilities.
7
votes
3answers
1k views

IValidatableObject vs Single Responsibility

I like the extnesibility point of MVC, allowing view models to implement IValidatableObject, and add custom validation. I try to keep my Controllers lean, having this code be the only validation ...
4
votes
2answers
944 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 ...
81
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 ...
1
vote
2answers
2k views

Both constructor and setter injection together in Spring? [closed]

I would like to know whether its possible to use both constructor and setter injection while configuring spring bean?
1
vote
2answers
315 views

Trying to grasp the idea of DI/Unity and possibly apply it to our simple application

Our app basically is a few forms that are filled out by people.. Then these forms are validated and reviewed and reports are created.. So I was thinking about DI and messing with a simple example of ...
6
votes
4answers
533 views

Should injecting dependencies be done in the ctor or per method?

Consider: public class CtorInjectionExample { public CtorInjectionExample(ISomeRepository SomeRepositoryIn, IOtherRepository OtherRepositoryIn) { this._someRepository = ...
1
vote
3answers
315 views

How I can hide dependencies from my Services?

Here's a service abstraction that I am working on : public abstract class Service { public ILogger Logger{get;set;} public IQueryManager QueryManager{get;set;} public ICommandManager ...
14
votes
3answers
2k views

Interception vs Injection: a framework architecture decision

There's this framework that I'm helping to design. There are some common tasks that should be done using some common components : Logging, Caching and raising events in particular. I am not sure if ...
1
vote
1answer
338 views

Are service locators really that bad?

People say Service Locators are bad because they expose the dependencies in some random method from the class (and not in the constructor's arguments like it should): class A{ function F(){ ...
39
votes
5answers
5k 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 ...
7
votes
3answers
540 views

Is the architecture of my project a common one? What's its name?

I am responsible for a software project for a few years now (a Java desktop application, which is mostly event-driven). I started it from scratch, and at the beginning of this project I made some ...
2
votes
1answer
5k views

How do I use PowerMock / Mockito / EasyMock to use a mocked object for dependency injection?

I have an AuthenticationManager.authenticate(username,password) method that gets called in someMethod of a SomeService under test. The AuthenticationManager is injected into SomeService: @Component ...
2
votes
1answer
198 views

Sharding with IoC

I've come across a situation where I need to shard a database (Oracle, but that doesn't particularly matter). The gist of the problem is I have written a large-scale system in a fairly standard ...
17
votes
5answers
8k views

What's the best way to build a factory using NInject?

I am pretty comfortable with dependency injection using NInject in MVC3. While working in an MVC3 application, I developed a custom Controller Creation Factory using NInject, so any controller that is ...
7
votes
1answer
476 views

Problem with understanding “seam” word

I'm reading "Dependency Injection in .NET" of Mark Seemann (it's fantastic, and must have) and author often uses word "SEAM", but I can't understand what does it means? Here is an example of using ...
11
votes
2answers
1k views

What is the difference between DI and IoC? [duplicate]

Possible Duplicate: Difference between Dependency Injection (DI) & Inversion of Control (IOC) I'm new to Inversion of Control, Dependency Injection and everything related to these ...
2
votes
2answers
346 views

Is there a better way to design these classes?

I have two business classes: TimesheetDay and TimeSlot. One TimesheetDay can have one or more TimeSlots. The TimesheetDay and TimeSlot classes will be dependent on an object that implements a ...
1
vote
2answers
154 views

Passing a dependency to a controller class

I'm looking for an approach to the following problem: I have a reusable library. It contains a controller class, in the MVC sense. Let's say it's called UserAdministrationController, and it's for ...
2
votes
1answer
424 views

JSP Model 2 Architecture and Dependency Injection

If I'm writing a web application that uses the model 2 architecture, is it possible to use the Google Guice framework (or really any IoC container)? The reason I ask this question is because ...
2
votes
1answer
497 views

Dependency Injection: Only for single-instance objects?

What if I want to also decouple my application, from classes like Product or User? (which usually have more than one instance) Take a look at this example: class Controller { public function ...
-2
votes
1answer
2k views

What are Dependency Injections and why are they useful?

I have heard of ninject and I have to build a lot of apps that have to integrate with other tools. SO i was wondering what this was and how it is used.
15
votes
2answers
636 views

Is DDD-Lite a pattern language for dependency injection?

I stumbled upon Greg Young's talk 7 Reasons why DDD Projects Fail where he mentions something he calls DDD-Lite at 7:20. Summarizing, he basically says some use DDD as a pattern languages (entities, ...
7
votes
2answers
759 views

c++ Model View Presenter: Where to construct presenter?

I'm using the Model View Presenter (MVP) pattern as described in The Humble Dialog Box paper (pdf) with an MFC project. I'm sure the issue is the same with most GUI toolkits. The thing that's ...
3
votes
2answers
497 views

Which of these is the best strategy for dependency injection?

I am working on a Java project that uses a dependency injection framework built by the company itself. The framework enforces the following naming convention: suppose you have a class Foo that depends ...
9
votes
1answer
1k views

Multiple Zend application code organisation

For the past year I have been working on a series of applications all based on the Zend framework and centered on a complex business logic that all applications must have access to even if they don't ...
3
votes
1answer
305 views

In-process SOA?

Can DI/IoC be used for a simplified, local SOA? I have been reading up on SOA (examples: here, here and here) and it seems that at first a lot of the attention was at interoperability and enabeling ...
5
votes
3answers
486 views

How often do you use DI container in your ASP.NET MVC application

While reading a book, I came across DI(dependency Injector) and the subsequent DI Container tool. Previously, I developed an application following a tutorial on asp.net website which never used such ...
15
votes
6answers
870 views

Should the usage of DI/IoC remove all occurrences of the “new” keyword?

Should the usage of Dependency Injection and an Inversion of Control container remove all occurrences of the "new" keyword from your code? In other words, should every object/dependency, no matter ...
12
votes
1answer
2k views

Domain-Driven-Design - external dependencies in the Entity problem

I'd like to start Domain-Driven-Design, but there are several problems I'd like to solve before starting :) Let's imagine I have a Groups and Users and when user wants to join a group, I'm calling ...
8
votes
3answers
849 views

Keeping an MVC model loosely coupled from the DB?

I like to keep my code testable and have decided to go with the Dependency-Injection strategy for my current MVC framework, which definitely has proven to be a great way to ensure loosely coupled ...
4
votes
1answer
447 views

Understanding the DIP, DI, IoC theory

For about 2 months I've been reading everything I can find for these 3 topics and I'm not yet sure I got it. Dependency Inversion Principle. Means you should always only rely on interfaces and not ...
71
votes
6answers
59k views

What does the Spring framework do? Should I use it? Why or why not? [closed]

So, I'm starting a brand-new project in Java, and am considering using Spring. Why am I considering Spring? Because lots of people tell me I should use Spring! Seriously, any time I've tried to get ...
3
votes
1answer
80 views

Test interface implementation

I have a interface in our code base that I would like to be able to mock out for unit testing. I am writing a test implementation to allow the individual tests to be able to override the specific ...
2
votes
1answer
264 views

Customizing configuration with Dependency Injection

I'm designing a small application infrastructure library, aiming to simplify development of ASP.NET MVC based applications. Main goal is to enforce convention over configuration. Hovewer, I still ...
6
votes
4answers
3k views

Use Dependency Injection For Data Objects?

I'm just learning about dependency injection, and am stuck on something. Dependency Injection recommends sending dependent classes through the constructor, but I'm wondering if this is necessary for ...
17
votes
4answers
2k views

Does “Inversion of Control” promote “Anemic Domain Model”?

When I used IoC Container in my last project, I ended up with anemic entities and most of my business logic in Stateless Services. I have seen projects written by other developers that utilize ...
1
vote
3answers
402 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 ...
41
votes
10answers
4k views

(Why) is it important that a unit test not test dependencies?

I understand the value of automated testing and use it wherever the problem is well-specified enough that I can come up with good test cases. I've noticed, though, that some people here and on ...
3
votes
2answers
411 views

“Don't cross the streams” Database access objects independant of the Data tier in an N-Tier architecture?

I'm adding functionality to our website which performs long-running processes asynchronously using MSMQ. Doing this ansynch, however means we need to notify users when their requests are completed. ...
5
votes
4answers
529 views

How could dependency injection be integrated into the language?

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 ...
2
votes
2answers
3k views

Why would I choose Unity over Autofac

I'm looking to start a new application and I want to use Dependency Injection. I have a lot of "Microsoft is the only way to go guys" in our shop so of course Unity is the way they wanted to go. ...
14
votes
6answers
562 views

Do abstractions have to reduce code readability?

A good developer I work with told me recently about some difficulty he had in implementing a feature in some code we had inherited; he said the problem was that the code was difficult to follow. From ...
14
votes
3answers
2k views

Sell me on IoC containers, please

I've seen several recommend use of IoC containers in code. The motivation is simple. Take the following dependency injected code: class UnitUnderTest { std::auto_ptr<Dependency> d_; public: ...
7
votes
2answers
464 views

How do I inject test objects when the real objects are created dynamically?

I want to make a class testable using dependency injection. But the class creates multiple objects at runtime, and passes different values to their constructor. Here's a simplified example: public ...
10
votes
4answers
594 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 ...
215
votes
13answers
49k 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, ...