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

1
vote
3answers
241 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 ...
2
votes
1answer
60 views

For DI, where to create dependencies (new objects) specifically within framework code?

Basic requirement I'm making a framework for learning purposes (and likely usage on personal sites). I'm using dependency injection in classes and I'm trying to design where would be a good place ...
1
vote
2answers
133 views

Dependency injection of ViewModel with unmanaged parameter

I have a PersonEditViewModel which needs two other objects, the personId and the PersonRepository. PersonRepository is obtained from a service locator: private final int personId; public ...
2
votes
1answer
129 views

Usage - Factory Pattern vs Dependency Injection [closed]

Okay, this question is not on which is better. It is more about when to use Dependency Injection vs Factory. I have done a bit of research and it goes like this. DIP: Provides loose coupling, ...
0
votes
1answer
59 views

Inject or not dependencies in a component based framework

I'm currently developing a small component-based framework the Android platform. The framework has some ready for use and some abstract (has some internal logic, but the developer needs to add some ...
0
votes
2answers
88 views

How to avoid using service location when using dependency injection? [closed]

I am trying to use dependency injection in a WPF application, and I really can't figure how to avoid using service location, which is considered to be an anti-pattern in many articles. I am using the ...
0
votes
1answer
95 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 ...
7
votes
3answers
2k 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 ...
4
votes
1answer
135 views

Dependency Management in the real world?

Most of the examples i've see when watching anything on dependency management hit what i consider to be the holy grail in that the only dependencies on classes are interfaces that the IOC container ...
1
vote
2answers
53 views

Is the use of a constant as a parameter of an Unit Test mocked dependency's method wrong?

We have a Unit Test Case where we want to assert that after a login operation, the SUT is able to call a method on one of it's dependencies. This dependency is a NavigationManager and the expectation ...
24
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 ...
3
votes
1answer
13k views

Difference between spring setter and interface injection?

I know how constructor and setter injection works in spring. Normally I use interfaces instead of classes to inject beans using setter and I consider it as interface injection, but in case of ...
0
votes
2answers
389 views

Pooling (Singleton) Objects Against Connection Pools

Given the following scenario A canned enterprise application that maintains its own connection pool A homegrown client application to the enterprise app. This app is built using Spring framework, ...
94
votes
4answers
39k views

Difference between Dependency Injection (DI) and Inversion of Control (IOC)

I've been seeing a lot of references of Dependency Injection (DI) & Inversion Of Control (IOC), but I don't really know if there is a difference between them or not. I would like to start using ...
1
vote
1answer
502 views

How should dependencies be managed across a modular application?

Let's say that we have a structure like this: Application -- Modules --Module1 -- Controller -- PublicHelper --Module2 -- Controller ...
2
votes
4answers
188 views

Design Pattern for retry and error handling

I'm trying to find a good design pattern, or perhaps series of patterns, for injecting error handling and retry handling when getting data from a webservice. For instance, I have: do { //get the ...
0
votes
3answers
109 views

How should I refactor a singleton (to be used by a container) when the refactored class requires initialization and takes a dependency?

I'm refactoring an old app to use dependency injection.n I'm pretty new to DI. I have a class that used to be a singleton. I'm refactoring it into a non-singleton class and using the container to ...
5
votes
1answer
193 views

PHP: Injecting the same database connection into multiple objects

Suppose that there are two classes that define objects of vastly different function such that in the datastore, the information they require is divided into two separate databases. For example, the ...
0
votes
2answers
124 views

Constructor Injection vs. IocFactory

Today at work a collegue and I had a discussion about the following: Basically we have a rule engine which works in the following way: RuleExecutor Gets all rules to execute in the constructor ...
123
votes
7answers
116k views

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

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 ...
2
votes
1answer
178 views

Injection, strategies and OO

I'm working on refactoring a project. The business logic looks very much like using the Strategy pattern would be very beneficial, because depending on the values of three properties (let's say age, ...
0
votes
0answers
30 views

Parameters from configuration files and dependency injection. What is the best way to combine them?

I am trying to figure out what is the best way to configure the services I want to be installed by the DI container. Imagine to have a configuration provider that abstracts over the configuration ...
323
votes
13answers
95k 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, ...
83
votes
9answers
6k views

Understanding dependency injection

I'm reading about dependency injection (DI). To me, it is a very complicated thing to do, as I was reading it was referencing inversion of control (IoC) as well and such I felt I was going to be in ...
2
votes
1answer
203 views

C# Dependency Injection with Adapter Pattern

In the following code sample, I have a client that works through a controller. In my specific case the controller establishes a session to an address on a GPIB bus. An instance of this is then ...
2
votes
1answer
170 views

How to deal with dependency when you have separate projects?

I know there is all this talk about having only one composition root that set all your dependencies and that you should always prefer constructor injection to other types, but sometimes it just ...
1
vote
1answer
40 views

Share service instances amongst all controllers?

We've taken over coding on a .NET MVC project, and as I'm relatively inexperienced with many of the MVC design approaches, I'm working on getting a good understanding of the design choices. One ...
0
votes
0answers
34 views

Is it possible to define injection rules based on Spring bean scopes?

My application is using Spring as my dependency injection framework. I have been struggling recently with nasty concurrency and security bugs. The bugs were caused by injecting bean with incorrect ...
3
votes
1answer
223 views

What is the motivation behind the @ImplementedBy annotation in Guice?

I recently read about the @ImplementedBy annotation available in Google Guice. It allows the programmer to specify a binding between an interface and its implementation for future use in dependency ...
5
votes
2answers
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 ...
1
vote
1answer
52 views

Polymorphism on repository interface for local and remote operations

I'm in the process of designing a facade ApiClient class that would abstract some CRUD operations between a local database and a remote one (through a REST web app). Said ApiClient class depends on 2 ...
1
vote
0answers
174 views

Class Interfaces with Multiple Classes and Dependency Injection

NOTE ON QUESTION TITLE: I did not really even know how to phrase the title so if someone can edit and make it more articulate I would really appreciate it. QUESTION: Basically, what I'm trying to do ...
3
votes
5answers
219 views

Injecting collections as constructor dependencies

We are using constructor dependency injection in our application. Following that approach we inject everything using an injection container so we are able to replace any dependency with a Mock. Some ...
3
votes
2answers
218 views

Factory for creating a singleton instance

We have some legacy code that has a bunch of singletons all over the place (written in C#). The singleton is a fairly "classic" implementation of the pattern: public class SomeSingleton { ...
-1
votes
2answers
1k views

Initialize in Field, Constructor, or Argument

If I know what a field will be initialized to, should I initialize it in the field, constructor, or receive it as a parameter? I am asking about best practices. All three options effectively provide ...
6
votes
2answers
235 views

Static console object or dependency injection?

For a current project I have created a Console class which wraps up C++'s usual cout, writes output to a file or uses Console::WriteLine depending on the environment. Currently I've setup my code base ...
12
votes
5answers
2k 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 ...
1
vote
3answers
1k 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 ...
1
vote
2answers
213 views

Dependency injection vs dependency lookup in a layered architecture

Given a layered architecture, where the business logic is isolated in a package from all other layers, like persistent storage, user interface, interfaces for various (vendor) remote services, etc, I ...
23
votes
11answers
2k views

Does Refactoring towards Looser Coupling, Smaller Functions, and Separate Classes affect System Performance? (and Should I Care)

In Computer Science field, I have noticed a notable shift in thinking when it comes to programming. The advice as it stands now is write smaller, more testable code refactor existing code into ...
2
votes
1answer
104 views

Transient life time and constructor injection

I'm working on a little light weight Dependency resolver found here https://github.com/AndersMalmgren/DuoCode.SimpleInjector Currently if you do var instance = container.Get<Foo>(); You ...
8
votes
4answers
603 views

What complexity do DI frameworks add?

The currently most upvoted answer to a very recent question states that DI containers are an "enterprise software" pattern, used when the object graph is very large and complex. I suspect that 95% ...
10
votes
4answers
10k views

Should one use Dependency Injection even if the class is used only once? [duplicate]

During a code review I have started having a bit of a dilemma as to whether use dependency injection or not. I would like to hear your thoughts, because this is kind of an ongoing theme and would help ...
2
votes
1answer
89 views

Pick dynamically the right Factory Class with Inversion of Control

I have a TreeView and I want to enable the context menu when clicking with the right mouse button. It is composed by items from different classes, but they all share the same base class. So, I was ...
2
votes
1answer
206 views

Hidden dependencies - why not?

Hidden dependencies: function __construct($dep_registry){ $this->db = $dep_registry->get('db'); $this->request = $dep_registry->get('request'); ... } Not so hidden: function ...
0
votes
0answers
161 views

Dependency Injection with EF - is mapping required?

I'm implementing the Repository pattern with using DI and EF6. I created the database (that inherits from DbContext) object with the Visual Studio wizard ADO.NET Entity Data Model I have a generic ...
0
votes
1answer
115 views

Multiple method calls in the constructor and dependency injection

I was asked to refactor some almost ureadable spaghetti code into object-oriented architecture. I have some doubts regarding a class that I designed. Here is the class' skeleton: require_once ...
1
vote
2answers
120 views

Rich Domain Models, Encapsulation, and the Problem of Constructor Over-Injection

I am refactoring an e-commerce web-application, currently working on the UserBasket class, which will have to handle adding or removing items, changing their quantity, getting total amount, total ...
5
votes
2answers
199 views

Sharing state with dependencies - Object-Oriented Design

Suppose that I define two interfaces below: public interface IReader { void Read(string bookName); } public interface IWriter { void Write(string bookName); } Now I want to implement ...
2
votes
2answers
2k views

What are the best practices to use NHibernate 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 ...