Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [dependency-injection]

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). It is a special form of the more general dependency inversion.

2
votes
1answer
65 views

Passing DI container to a factory – is this always an antipattern?

Generally I would always answer "yes" to this question, because at this point, your DI container has become a service locator, and you're losing the benefits of DI. However, I wonder if it is ...
1
vote
2answers
55 views

Structural problem related to Dependecy Injection and Asynchronous Operations

I'm implementing the onion architecture with webapi, service layer, repository, unit of work and the domain classes. I use also DI, using simple injector. But my issue, is how can I implement a ...
0
votes
1answer
35 views

MVP Passive View -> Dependency Injection -> Factory Pattern -> Is testing behaviour whilst ignoring state enough?

I am writing an application using the Model/View/Presenter passive view pattern. I have view that contains multiple items in a list. My model is of type AudioDrama. For filtering that list I have ...
0
votes
2answers
93 views

Design issue with delegation, inheritance and dependency injection

My question relates to usage of delegation together with inheritance and dependency injection. I have a MailerService class that requires a delegate in order to do its job. Furthermore, I have a ...
1
vote
2answers
88 views

Construct Entities via Dependency Injection Framework

I need help settling a disagreement, a co-worker and I have. We're working on a pretty huge Angular project and are in the process of looking at incorporating some Domain Driven Design principles into ...
1
vote
2answers
91 views

Performing serial communication in constructor to initialize an object

I have a class UnitInfo which represents a collection of unit information with methods to get the unit information in a structured way, such as a specific encoding, etc. This unit info consists of ...
1
vote
3answers
131 views

Perplexed by dependency injection principle - what if it is runtime dependant which objects are created?

The antipattern-plagued game I'm creating is using ASP.NET Core, which, in many cases, requires the use of dependency injection. This is new and counter-intuitive to me. Up to now, I managed to limit ...
2
votes
5answers
293 views

How can you use DI without DI framework in a reasonably complex project?

edit I asked this question not because this is my opinion, but because I am trying to understand someone else's. Personally I prefer IoC via DI (in my specific case Simple Injector) but not everybody ...
1
vote
1answer
53 views

Does a service-locator lead to more straightforward code in this situation?

Assume I have a class like this: public interface IService { // Gets some data, possibly from a website / database List<string> GetStrings(); } public class ItemViewModel : ...
2
votes
2answers
506 views

Unity injection with too many constructor parameters

The question is about choosing the appropriate design for the scenario described below. This is a repost from https://stackoverflow.com/questions/51940180/unity-injection-with-too-many-constructor-...
2
votes
3answers
354 views

How far should I go with dependency injection and mocking?

Say I have a following class: public class A { public void execute() { if (something) ThirdPartyApi.method(); } } Now, I would like to test in particular the execute() method. I ...
0
votes
1answer
110 views

Should services be used in a ViewModel

So I have a service which is injected in my controller via dependency injection. I can either use it in the controller multiple times to convert a value when assigning to a particular property in a ...
-2
votes
1answer
101 views

what are the external dependencies that a class should have

from my class I include all dependencies in the constructor. to decide which dependencies to pass what rules should I follow? when I decide to use the parameters of a method instead of the ...
5
votes
4answers
202 views

Should I inject a Thread.sleeper as a dependency to make unit test easer?

I have a repeated pattern in my code that is not easy to test: classes that execute periodically. Just to simplify things lets say assume something like: while( running ){ long millisToWait = ...
4
votes
2answers
105 views

Handling disposables with dependency injection

I'm struggling to implement disposable objects via dependency injection, as the dependencies are injected in the constructor (and live as long as their containing object does), whereas I want the ...
4
votes
1answer
290 views

Should unit of work create repository?

Our "unit of work" class has a method which takes a class type and creates a repository: public IRepository<TEntity> GetRepository<TEntity>() where TEntity : class { ...
0
votes
1answer
74 views

Can I use Dependency Injection to test my class when a parameterless constructor is needed?

I want to use PowerArgs for a console application I am writing. It offers some nice features I'd like to try. My root looks like this: public static void Main(string[] args) { try ...
3
votes
2answers
172 views

Is conditional dependency injection based on environment a bad pattern?

Is it a bad design if we inject dependencies based on the running environment (production, testing, development, etc)? Consider the following container in Laravel as an example: $app->singleton( ...
2
votes
2answers
182 views

In a codebase with all dependencies injected, can unit testing catch regressions?

Let's say I build my code so that I inject ALL dependencies to every class and when I test, I replace those dependencies with test doubles. Let's also say that when I work on a class I run its tests ...
0
votes
0answers
55 views

Dependency injection and data encapsulation

Let's say I have Game class. This game class gets constructed with all its dependencies by Game(ITimer timer, IPointsUpdater updater). When the timer raises Elapsed the updater calculates the user's ...
-2
votes
1answer
123 views

Why EJB service Injection requires the specific subclass to be created?

Most Java EE guidelines suggest that we should define Abstract EJB services (APIs) and then define concrete implementations in order to conform with OO design principles. But at the injection point, ...
-3
votes
1answer
152 views

Unit Testing with multiple dependencies

Based on this Q: https://stackoverflow.com/questions/50968762/callback-not-being-called I understand (and its obvious in retrospect) what I'm doing wrong. However given the scenario as unit testing ...
0
votes
3answers
123 views

Circular dependency in composition root

I have a MDI MyMDIWinForm that needs an instance of IGUIErrorHanlder for error handling, At the same time IGUIErrorHanlder depends on the MDI MyMDIWinForm for having a place from where to launch pop-...
0
votes
0answers
25 views

How to handle sharing of same context in Repositories-Unit of Work when using dependency injection

I have a problem that knocks out me. Background: I am inside the devolpment of a WinForms app that is not layered. Almost all the events are handled in the Form class. I have multiple IRepositories. ...
7
votes
6answers
1k views

How to know when to use dependency injection?

The question might sound a little weird, and I guess it is. I'm came up with the question while browsing through some design patterns. I came to the notorious state / strategy pattern and I came up ...
0
votes
1answer
178 views

Dependency Injection in one method to other method params

A teammate of mine wrote some code in the following way: Class Foo implements Job { // Framework uses type-hinting for dependency injection, only works on the handle() method // not on other methods ...
0
votes
0answers
47 views

Instantiating class A which is injected by class B, which is injected by class C and so forth

So given a configuration file which has all the non-object parameters needed to create a class like so: { "component1": { "name": "C1", "parameters": { "p1": <component2>, ...
108
votes
8answers
19k views

Criticism and disadvantages of dependency injection

Dependency injection (DI) is a well known and fashionable pattern. Most of engineers know its advantages, like: Making isolation in unit testing possible/easy Explicitly defining dependencies of a ...
0
votes
1answer
98 views

How to deal with complex dependency graph in IOC?

I have several apps witch build from one code base (using common modules). And my question: how to write composition root in this case? Let's imagine simple dependency graph: ClassBase --> ...
3
votes
2answers
401 views

Dependency Injection in Chain of Command pattern

I have a Chain of Command pattern implemented, with modules that implement an interface: interface IRequestHandler { public function handle(&$offset, &$tripData, $request = null); ...
0
votes
2answers
138 views

Class design - Inject data or inject repository/service to fetch data?

Let’s say for example I am developing a fictional class called WidgetMaker. If the class depends at some point on lookup data stored in a database is it better to design the class with a dependency ...
1
vote
0answers
102 views

IoC - Should I use child containers to create scoped dependencies?

I have a number of command handlers defined for doing work against databases on various servers in our network. I am wondering if I should design each command to pass the following information in the ...
1
vote
3answers
191 views

Implementing an ILogger => how to make public some fields that are needed in specific implementation of Logger but not in others

I am implementing a custom ILogger (interface for logging) I have succesfully developed implementations of it with some famous loggers like Serilog or Log4Net. Now i want to create a memory logger, ...
1
vote
2answers
77 views

How Can I Maintain DI Pattern of a Results Writer, with 2 different results sets?

Note: I am note sure how to phrase the title This is pseudo code of my Project that I am working on. For conciseness I have left out some details. I have a 2 types of Consumer classes. One is ...
0
votes
0answers
114 views

WPF DI with DbContext per form

Trying to figure out how to keep class methods clean, follow EF's guidance of short-lived DbContexts, and use DI in WPF. Issue I'm having is implementing transactions with DI. I'm trying to figure ...
3
votes
4answers
290 views

concerns about constructor dependency injection logic

There is a kind of rule in dependency injection literature, stating that we should declare all arguments in the constructor, in order to have a constructor injection, which is better than other ...
3
votes
4answers
172 views

Circular Interface references

I've heard circular references are generally an issue, however I was wondering if this was true for interfaces that reference other interfaces, for example: IQuestion{ IAnswer getCorrectAnswer(); ...
1
vote
2answers
175 views

Depending on Dependencies

public class Connection : IConnection { public Connection(Uri baseAddress, ISerializer serializer) { BaseAddress = baseAddress; Serializer = serializer; ...
0
votes
0answers
64 views

Inject ViewModels created after getting Models from DB

I am currently working on a Picture Management software where I have to follow pretty strict requirements due to specified interfaces I am not allowed to change. Therefore I am forced to have a List&...
0
votes
0answers
45 views

How to pass a variable to class used by a service in DI?

I have a problem with passing a parameter to a request I want to make. I cannot find the proper way of doing it. I made the code simpler, than it really is, only to concentrate on the problem. I ...
0
votes
0answers
155 views

Scoped beans as dependencies - Use proxy or make it dependent/prototype scoped?

Regarding overall performance, when used as a dependency of a higher scoped bean, is it better to downgrade a bean scope to prototype or dependent instead of using proxies? Consider a scenario where ...
1
vote
1answer
193 views

Passing ServiceLayer's services to WCF service

I want to expose my business logic via WCF service in the intranet environment. I have N-tier architecture, pretty simple: Database -> .Data (DAL) -> .Business (Service Layer) -> WCF So, far I have ...
1
vote
2answers
463 views

Dependency Injection and DI Container

I have implemented clean architecture for my app, and I have a few questions. Typically, pure DI is argued for over a Service Locator pattern, because it is very explicit and more testable. However, ...
2
votes
2answers
394 views

How does “Composition Root” DI apply to Domain Driven Design?

I'm confused about applying the "Composition Root" (CR) to create aggregates in DDD. Seemann (2012) defines CR as a "(preferably) unique location ... where modules are composed". He argues for ...
0
votes
2answers
467 views

Is it proper to inject the configuration to the business logic layer?

I have an ASP.NET Core 1.1 application which uses dependency injection and is splitted in 3 layers (web, business logic and data access). I'd like to access to some values of the configuration file ...
3
votes
1answer
219 views

Handle third party library class depedency which do not have interface

I am working on EWS. A class is made to query mail box and read emails. public class MailReader { private readonly ExchangeService _service; private readonly PropertySet _propertySet; ...
-1
votes
1answer
51 views

Laravel Service Providers count and DI

I understand how service prividers in Laravel works and how to use them. But I'm not sure about how to keep clean code, specificaly in count of method parameters. For example I have this route: Route:...
3
votes
1answer
139 views

Dependency injection for tightly coupled components

I am writing some code to parse some files (which I call "assets"), and I'm planning to structure this as three classes: AssetParser, NamespacesParser, and TransfomersParser. AssetParser will use ...
1
vote
3answers
186 views

How to make sure to inject valid dependencies?

Let’s say we have the following abstract class HungryAnimal { private MovementStrategy movementStrategy; public HungryAnimal(MovementStrategy movementStrategy) { this....
4
votes
2answers
1k views

Java: Is it okay to abuse Spring beans (@Component) rather than use static final utility classes?

I'm torn between using DI and static utility classes on my code. I currently created a DateUtil static class that is only meant to be accessed statically. This class is responsible for just creating ...