Dependency injection is awesome. It helps you build code that is loosely-coupled, testable, maintainable. The code is clean and readable. And if set up properly, you might end up coding a lot less than without dependency injection. Almost :) Because if you use constructor injection (and why wouldn't you if you apply the basic OO principles), you might end up creating a lot of constructors with a lot of parameters. While this is not a bad thing - after all this gives you an instant overview about the dependencies of a component - you do have to code a lot. This tool helps you with that.
Features currently include:
- Get the cursor on a private readonly field of an interface or abstract class type. Hit Ctrl+. (or whatever is your shortcut for the refactoring suggestions) and choose "Generate dependency injection constructor". This will give you a constructor which has a parameter for every private readonly field of an interface or abstract class type and the fields are initialized from the parameters. From v1.5, getter-only autoproperties are also supported.
- Get your cursor over a constructor parameter. Now hit Ctrl+. and you get two options: you can generate a private readonly field that is of the same type as your constructor parameter and you have the option to name the field the same as your parameter, or prefix the name of the parameter with "_"
- v1.5: Bugfixes
- v1.4: Generate for getter-only autoproperties also
- v1.3: Add guard clauses for constructor parameters
- v1.2: VS2017 upgrade and bugfixes
- v1.1 Bugfixes
- v1.0: Initial version, introduce fields and constructor parameters.
Thanks for @trydis for publishing his version of the "Introduce and initalize field" feature. Check out his blog post here.
Also thanks for @varsi94 for helping out with the original version of the code and for the v1.4 feature.
Thanks for @holymoo for the v1.1 bugfixes.
Thanks for @exglade for the v1.2 bugfixes and the VS2017 upgrade.
Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY