I find myself adding dependencies a lot to constructors like so:
public class SomeClass() {
private ISomeService _service;
private IAnotherService _anotherService;
public SomeClass(ISomeService service, IAnotherService anotherService) {
_service = service;
_anotherService = anotherService;
}
}
They're quite tedious to write, I've been looking for code snippets in visual studio to automatically add one to the constructor but haven't found one.
What I want is:
OR
If you have R# you can enter the field declarations and then highlight them and hit Alt-Enter
which will give you the option to generate the constructor and field assignments.
If you don't have Resharper, you can add the parameter on the constructor, write the assignament to an unexisting property and hit CTRL+. . This will prompt you with the options to automatically create a property or field for you.
For example, you have this class:
public class MyClass
{
public MyClass()
{
}
}
You then add the parameter to the constructor, and the assignament:
public class MyClass
{
public MyClass(IDependency myDependency)
{
this.myDependency = myDependency;
}
}
And hit CTRL+. while on the asignament line, and select create field, and you'll get this:
public class MyClass
{
IDependency myDependency;
public MyClass(IDependency myDependency)
{
this.myDependency = myDependency;
}
}
I don't know about previous versions, but in vanilla Visual Studio 2017, you can actually add a constructor parameter
public SomeClass(ISomeService service)
{
}
Then put your cursor on service
and from "Quick actions" you can chose Introduce and initialize field _someService
which will do what you want :
private readonly ISomeService _someService;
public SomeClass(ISomeService service)
{
_someService = service;
}
You can easily add code snippets as you like, defining it in XAML and adding it the editior, you can use place holders like "class name" to use it as a constructor for example, and then place your variables in it as static text
I don't want to write the code because it's duplicated you can check out how to do it here: https://msdn.microsoft.com/en-us/library/ms242312.aspx?f=255&MSPPError=-2147217396
you can also see this question: How can I automatically generate a constructor that receives and stores services for free?
what you are trying to do with the ISomeService and IAnotherService is to do dependency inversion.
I would strongly recommend you combine this with a dependency injection framework. There are many available but one i would recommend is MEF. MEF is build into the .net framework. For example your code would look like this with MEF
[Export(typeof(ISomeService))]
public class SomeService : ISomeService {
}
public class SomeClass {
[Import]
public ISomeService SomeService {get; set;}
[Import]
public IAnotherService AnotherService {get; set;}
}
Now MEF will actually ensure your SomeService and AnotherService Properties are filled when your class is created. It will construct (if needed) a instance of SomeService, fill all it's dependencies and put it in the right property. You can even control if you want your services instantiated as singletons or as a new service instance each time you need one.
for more details on MEF you can look here https://msdn.microsoft.com/en-us/library/ee155691(v=vs.110).aspx
This should avoid writing the many constructors that do nothing other than initializing services.
Telerik's JustCode can do exactly what you need.
if you have an unused argument in the constructor you can create a field and initialise it. http://www.telerik.com/products/justcode/quick-fixes.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.