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.

Filter by
Sorted by
Tagged with
0
votes
0answers
27 views

Registering dependency implemented in separate assembly with shared interfaces

Situation: I have a project with a Business Layer (WizBang.BL) that contains nearly all of my application logic. It communicates with a Data Layer (WizBang.Data) that contains by entity classes and ...
1
vote
1answer
55 views

Abstracting common functionality for REST and CLI

I have made an application that deals with collections of images. Currently it has a REST api to add/remove images, create/clear/delete collections and a worker that can automatically fetch images ...
-1
votes
1answer
56 views

Dependency Injection and Factory resolution

In the below example, assume FooA and FooB each have constructors that have a large amount of dependencies being injected into them. If I have a class that needs to determine which IFoo ...
2
votes
3answers
131 views

Abstract Factory with Pure DI

I have an application where I'm using pure DI to construct my object graph. I would like to use a factory so I can create and destroy instances of a certain class at run time. The trouble is that ...
0
votes
2answers
201 views

Interface/Contracts in separate assembly?

Say I have BusinessService & domain logic as part of MyProj.MiddleTier csproj which connects to MyProj.DAL DataRepository via interface IDataRepository. I am using IOC unity container to build my ...
2
votes
2answers
159 views

3 layer architecture In DI based software

I'm building software with 3 layer architecture and asp.net core , as you know the Dependency Injection is Built Into Asp.net core As i know the 3 layer architecture diagram is like this: ...
2
votes
2answers
146 views

Documenting Dependency Injection Parameters

Should documentation be added for constructor parameters that are passed via dependency injection? In my current project I have decided to omit documentation to describe each of these parameters and ...
0
votes
1answer
83 views

IoC/DI + “normal” constructor arguments mvvm

I am using unity in C# to achieve dependency injection and inversion of control. What if i have a class, lets say a viewmodel, that depends on several services like a repository, a validator and a ...
0
votes
1answer
73 views

Chain of responsibility or simple dependency injection for Querying API

I am working on refactoring a project code that is wired up in a tangled way. It was started with decent dependency injection and over time with all custom requirements it looks it got tangled up. ...
1
vote
1answer
113 views

Proper way of handling large Dependency Injected constructors [duplicate]

I have many dataservices/classes injected into a constructor. public MyClassConstructor (IAclass A, IBclass B, .... , IZclass Z){ ... } The number I have to pass down goes well beyond 100. To ...
0
votes
1answer
71 views

Is it bad practice to have a helper package in go for testing purposes. Or is this introducing dependence's

I find myself repeating the same code when writing unit tests, for example... When writing functions that work with files, In the setup for the test i often write some code to create a file (in a ...
1
vote
3answers
123 views

What considerations should I mind when designing methods or functions that take in a lot of parameters?

What considerations should I mind when designing methods or functions that take in a lot of parameters? A lot meaning over 4 but less than 10. Example, I am debating whether to pass in an array like ...
1
vote
3answers
195 views

Use just one parameter for all dependencies instead of many?

I understand that having 3 or more parameters as dependency is a sign that the class may be doing too many things, however there are cases that the class really needs more than 3. My question is, is ...
49
votes
7answers
11k views

Managing and organizing the massively increased number of classes after switching to SOLID?

Over the last few years, we have been slowly making the switch over to progressively better written code, a few baby steps at a time. We are finally starting to make the switch over to something that ...
2
votes
1answer
192 views

FP Free Monad vs OOP Dependency Injection

I've written my first moderately large project in functional style (in F#) and can see the advantages. The main challenge was to achieve the "Onion" architecture i.e. large and "smart" pure core / ...
8
votes
5answers
1k views

When should we use dependency injection (C#) [closed]

I would like to be sure I understand the concept of dependency injection (DI). Well, I actually understand the concept, DI is not complicated: you create an interface then you pass the implementation ...
1
vote
4answers
169 views

Dependency injection / testability - would this logic be appropriate?

In a testability perspective, would this logic be appropriate? public class Foo { public List<SomeClass> Elements {get; set;} public Foo(IServiceInterface service) { this....
0
votes
3answers
226 views

Dependecy Injection vs Singleton Pattern

I just read documentation and tutorial about dependency injection (DI) and the singleton pattern (SP). Everybody seems to says DP is bad and DI is the way to do it. Tutorials about dependency ...
1
vote
1answer
87 views

Classes with lots of injected dependencies [closed]

I'm often finding that my classes end up with many (10-15) dependencies being injected. I realise there is no hard and fast rule, but someone once told me that more than 4 or 5 can be a code smell. ...
0
votes
1answer
114 views

Avoid too much dependencies for a Use Case in a Clean Architecture app

Context I'm currently developing an application following the Clean Architecture principles (at least I'm trying really hard to follow these). All my Dependency Injections are done manually, without ...
0
votes
1answer
181 views

Dependency injection: Is there a better way than new A(new B(new C ()))? [closed]

I'm just learning about dependency injection. Unfortunately, I don't have the luxury and time to learn a DI framework right now so I am attempting to do DI through some simple means. Say I have an ...
0
votes
1answer
53 views

How to define Composition Roots for libraries (Javascript/inversify)

I'm building a console app (A), which depends on library B. Library B further depends on library C. I am building all 3 entities. Most of the development completed so far has been done using a ...
2
votes
5answers
579 views

Can Dependency Injection be overkill?

At what point does dependency injection make sense? When I first learned about Guice I was really excited. I have a Concurrency module, which is basically just a singleton controlling concurrency. I ...
0
votes
2answers
182 views

Can I use a library that uses guice to bind contact and implementations in applications without issues?

I have a play application and want to take a common operation out from the application and make it as a library in order to use in other play applications. This proposing library has a contract(...
10
votes
5answers
2k views

Can we completely replace inheritance using strategy pattern and dependency injection?

For example: var duckBehaviors = new Duckbehavior(); duckBehaviors.quackBehavior = new Quack(); duckBehaviors.flyBehavior = new FlyWithWings(); Duck mallardDuck = new Duck(DuckTypes.MallardDuck, ...
6
votes
6answers
560 views

I need help solving a common architectural problem with multiple concrete classes implementing an interface

I have a common issue I encounter which I've solved with a Factory class in the past but it always felt a little "off". Let's say I have multiple Exporter classes which implement IExporter, which has ...
0
votes
2answers
298 views

Dependency Injection in C++ use pointer or object?

While I am learning C++ based on how develop in modern PHP frameworks I decided to use the Dependency Injection pattern with services as Singletons. For making myself clear let suppose we have the ...
3
votes
2answers
169 views

How should domain services be referenced?

I'm working on a test project, with the intention of using DDD. So far I have this basic project structure: My question is: How should the Core.Domain.Services be referenced? do I create an ...
1
vote
1answer
141 views

Avoiding instantiating the whole application at startup when using a composition root

In a typical non-DI application, when you enter a section of code the necessary objects are constructed and when you leave it they are deconstructed. However, when doing dependency injection with a ...
2
votes
2answers
210 views

Tight coupling between parent and children: always to be avoided?

Say we consider two inherently coupled elements, using a real-life like example: Body PhysicalIllness Note: the following code is pseudo-Java for the sole purpose of syntax-coloring, pure syntax ...
0
votes
2answers
103 views

Passing runtime-read configuration when working with an IoC container

I've recently "discovered" the IoC realm, and decided to refactor a project I'm working on to use such a container. To be specific - I'm using autofac in C#, in a .NET Core console application. The ...
1
vote
1answer
140 views

At what point is having a static class with utility methods better than having an injectable service?

I have a pretty general question about the design of Angular web applications. I'm a big fan of writing services. Sometimes I write services that have only utility methods that don't require state at ...
2
votes
1answer
157 views

Clean Architecture - Should you inject all entity dependencies?

According to "Clean Architecture", should you inject all entity dependencies (should you inject core business logic)? For example, if I have a vector entity, should that entity be injected into all ...
1
vote
2answers
281 views

When to use Factory design pattern instead of Dependency Injection?

I'm quite inexperienced in design patterns and while I was studying them I got confused about the application of the Factory pattern. Wouldn't DI decouple the classes more than factory would do it? Or ...
0
votes
0answers
106 views

Inherit a class that inherits a generic class to make it easier to read

I'm creating a simple Dependency Injection library for Unity (no constructors available) and I want to use a generic class that implements a generic call for each class that would inherit it allowing ...
0
votes
1answer
70 views

Which types of objects that are instantiated inside controller's methods should be injected into the controller instead?

Consider code below MyController //MyAction //MyHandler { public function processRequest() { // ... $myObject = new MyObjectClass(); $myObject->methodCall(); ...
11
votes
5answers
2k views

How to use dependency injection and avoid temporal coupling?

Suppose I have the Service that receives dependencies via constructor but also needs to be initialized with custom data (context) before it can be used: public interface IService { void ...
2
votes
2answers
204 views

What is wrong with writing web applications in a way that merges repository layer with view and with controller in the same file?

is there anything wrong with writing code in a way that merges view, repository, controller in the same file? i.e. global $db; // or $db = DBSingleton::get_db_instance(); // or global $container; $...
107
votes
5answers
8k views

What's the difference between using dependency injection with a container and using a service locator?

I understand that directly instantiating dependencies inside a class is considered bad practise. This makes sense as doing so tightly couples everything which in turn makes testing very hard. Almost ...
2
votes
4answers
134 views

Make a class depends on its own ports instead of injecting dependency interfaces

I just have an idea about dependency management in Spring IOC environment that seems to be better than the typical approach, but I am not sure because I can't find any references or example out there ...
2
votes
1answer
173 views

Would de-coupling using interfaces/templates make the system easier to maintain at the cost of over-engineering?

I have been practicing this hybrid approach for dependency injection in the last couple of days and I am wondering if it should also apply to components which are within the same package? For example:...
2
votes
2answers
246 views

Dependency inversion vs repository pattern (app layer dependency on ORM)

Let's say we have an application layer with some command handler and we use an Entity Framework database context/ORM context in that handler. I would invert dependencies from the Entity Framework/...
2
votes
1answer
109 views

Changing static method that now requires a runtime dependency

I had a simple static method which would take a value and a unit type (pounds, gallons, dollars) and would just format a string. A simplified example: public static string Format(decimal value, ...
0
votes
2answers
204 views

How to avoid ad hoc constructor for unit testing

According to the answer in this post Should my classes have separate constructors just for unit testing? ad hoc constructor is not good and should be avoided. Unfortunately, I have the same problem ...
1
vote
1answer
158 views

How to design a reassignable global instance that can be referenced by many other classes

I have a DeviceManager class which can handle different physical devices, and to say there are Classes A,B,C, which require a DeviceManager instance as dependency. They should always use the same ...
1
vote
1answer
264 views

How to manage context: set as property on class, or pass as parameter with each method?

I am writing a service, which is to be consumed by API and Web controllers. The result of each call depends on a UserContext state. Should I pass this UserContext with each method, which includes a ...
2
votes
4answers
307 views

C# Pattern for Creating/Hiding Object Graph Creation

I'd like to get some advice on what would be a good approach at hiding the creation of an object graph. In the code below I would like SomeClass to not be directly responsible for the creation of the ...
1
vote
4answers
432 views

Is dependency injection always a best practice?

Recently I started working on a rather large monolithic system. The solution has web, app and api parts and in general just a lot of stuff. It relies heavily on dependency injection and I noticed ...
4
votes
5answers
317 views

Is supplying concrete class or primitive type only still called dependency injection?

For example, I remember most examples of dependency injection I see is something like this: public interface Fruit{ } public class FruitBox{ public Fruit fruit; public FruitBox(Fruit fruit){ ...
0
votes
1answer
152 views

Factories to create same objects with different data sources

I have several handlers classes that implements same interface and factories to create handlers. Handlers: public class Handler1 : IHandler { private readonly IService1 _service1; private ...