Questions tagged [constructor-injection]

Constructor Injection is the Dependency Injection technique of passing an object's dependencies to its constructor.

Constructor Injection is the Dependency Injection technique of passing an object's dependencies to its constructor.

254 questions
94
votes
7 answers

Can I pass constructor parameters to Unity's Resolve() method?

I am using Microsoft's Unity for dependency injection and I want to do something like this: IDataContext context = _unityContainer.Resolve(); var repositoryA = _unityContainer.Resolve(context); //Same instance of…
NotDan
  • 31,709
  • 36
  • 116
  • 156
55
votes
2 answers

Best practice for @Value fields, Lombok, and Constructor Injection?

I'm developing a Java Spring application. I have some fields in my application which are configured using a .yml config file. I would like to import those values using an @Value annotation on the fields in question. I would also like to use the…
Ertai87
  • 1,156
  • 1
  • 15
  • 26
43
votes
3 answers

MEF Constructor Injection

I'm trying to figure out MEF's Constructor Injection attribute. I have no idea how I tell it to load the constructor's parameters. This is the property I'm trying to load [ImportMany(typeof(BUsers))] public IEnumerable LoadBUsers { get; set;…
alpha
  • 487
  • 1
  • 6
  • 8
19
votes
8 answers

Spring constructor injection of SLF4J logger - how to get injection target class?

I'm trying to use Spring to inject a SLF4J logger into a class like so: @Component public class Example { private final Logger logger; @Autowired public Example(final Logger logger) { this.logger = logger; } } I've found the…
Alexander Torstling
  • 18,552
  • 7
  • 62
  • 74
17
votes
3 answers

How to call constructor with interface arguments when mocking a concrete class with Moq

I have the following class, which uses constructor injection: public class Service : IService { public Service(IRepository repository, IProvider provider) { ... } } For most methods in this class, I simply create Moq mocks for IRepository and…
Nick Williams
  • 2,864
  • 5
  • 29
  • 43
17
votes
3 answers

How is the Web API Controller's constructor called?

According to this article, a Controller should have a constructor that gets the interface to be implemented passed in, a la: public class DuckbillsController : ApiController { IDuckbillRepository _platypiRepository; public…
16
votes
3 answers

Constructor Injection in C#/Unity?

I'm using C# with Microsoft's Unity framework. I'm not quite sure how to solve this problem. It probably has something to do with my lack of understanding DI with Unity. My problem can be summed up using the following example code: class…
JP Richardson
  • 38,609
  • 36
  • 119
  • 151
15
votes
2 answers

Best practices for dependency injection via constructor

Inversion of control is a value-proof technique which is used to modularize a system and decouple the components from each other. Low coupling is always an advantage: it simplifies automatic testing of the components and makes the code better…
manlio
  • 18,345
  • 14
  • 76
  • 126
13
votes
2 answers

Registering a type with multiple constructors and string dependency in Simple Injector

I'm trying to figure out how to use Simple Injector, I've used it around the project with no problems registering simple services and their components. However, I wanted to use dependency injector when having a component with more than two…
12
votes
4 answers

Is it necessary to check null values with constructor injection?

I'm using .NET Core constructor injection. In a code review from a colleague, he raised the question if I should check for null values on injected dependencies in controllers. Since the framework is responsible for creating an instance of the…
11
votes
1 answer

Ninject multi-injection is not as greedy as I would have thought! How come?

If I have a class with a ctor set up for multi-injection like this: public Shogun(IEnumerable allWeapons) { this.allWeapons = allWeapons; } And bindings set up like…
11
votes
3 answers

Implicitly injecting dependency in Base class while derived class is resolved through Unity

I have a base Class Base having dependecy Dep and default and Injection Constructor- Class Base : IBase { public IDep Dep { get; set; } public Base() { Console.WriteLine("Default Constructor Base "); } …
10
votes
2 answers

Convert Spring field injection to constructor injection (IntelliJ IDEA)?

I am convinced to convert my Spring Java field injection [@Autowired] to constructor injection (among other reasons, to facilitate mock unit testing)... Is there a utility I can use to automatically do that Spring field to constructor injection…
10
votes
1 answer

When to use constructor injection in Spring?

When to use constructor injection in Spring? I heard that constructor injection is particularly useful when you absolutely must have an instance of the dependency class before your component is used. But what does it mean? Can anybody explain me…
XYZ
  • 133
  • 1
  • 11
10
votes
1 answer

Spring element must specify a ref or value

I'm having a problem with Spring and constructor injection. I want to create dynamically objects with a name (String) and special id (long). But when the spring.xml file is loaded an exception occurs. Exception in thread "main"…
smsnheck
  • 1,563
  • 3
  • 21
  • 33
1
2 3
16 17