I have the following controller:
public class MyController : Controller {
private readonly IService service;
private readonly int intDependency;
public MyController(IService service, int intDependency) {
this.service = service;
this.intDependency = intDependency;
}
...
}
Obviously resolution does not work, nor can I provide constructor parameter using a delegate, since this would end up with multiple constructor registration.
What is the correct way to proceed in this case? Is it generally uncorrect injecting value type as a dependency? Would it be a better practice to wrap the dependency behind an interface?
Is it generally uncorrect injecting value type as a dependency?
Indeed, as you can see there is no way for the injector to know which int
it has to inject. Doesn't really make sense.
Would it be a better practice to wrap the dependency behind an interface?
Yes.
What is the correct way to proceed in this case?
You said it. Either an interface or a class.
Example:
public class MyConfig
{
public int MyInt {get; set;}
}
And you could configure SimpleInjector like this:
container.RegisterSingleton(new MyConfig { MyInt = 42 });
and your controller would be:
public MyController(IService service, MyConfig config) { ...
Note: There is a way to inject primitive types : Primitive Dependencies with Simple Injector However, it's way easier and cleaner to just wrap the primitive in a class like my example.