In my code, I'm often finding myself setting up classes which utilize constructor-based dependency injection and have a number of dependencies coming in. It's not uncommon for me to end up with something like:
public class MyClass
{
private readonly IMyDependency1 _dependency1;
private readonly IMyDependency2 _dependency2;
public MyClass(IMyDependency1 dependency1, IMyDependency2 dependency2)
{
_dependency1 = dependency1;
_dependency2 = dependency2;
}
}
This, while obviously necessary from a functionality perspective, feels like needless boilerplate. I've used libraries in other languages such as Java which help with this by offering annotations/attributes to handle the bindings for you. What I'm looking for is something that handles this in C# (preferably compatible with .net core), something like this:
public class MyClass
{
[FromConstructor]
private readonly IMyDependency1 _dependency1;
[FromConstructor]
private readonly IMyDependency2 _dependency2;
public MyClass(IMyDependency1 dependency1, IMyDependency2 dependency2)
{
//whatever Bind() call required here
}
}
Bonus points if the presence of the annotation can result in the package autogenerating the required constructor meaning I can avoid needing to write a constructor at all in the case where all the constructor is doing is handling dependency binding.
Does anything like this exist?