Problem
I regularly find myself manually typing out code that looks like this:
public class SomeClass
{
readonly ServiceA serviceA;
readonly ServiceB serviceB;
public SomeClass(ServiceA serviceA, ServiceB serviceB)
{
this.serviceA = serviceA.;
this.serviceB = serviceB;
}
}
Requirements
I would like to save time by having a way to generate as much of this as possible. I don't want to use a tool that requires a purchase. The only information that is variable here is the class name and the types of the services. So in this example, I would like to be to type something minimal like this:
- Some+Shortcut
SomeClass
ServiceA
ServiceB
- Enter
I'm also open to a solution that still requires me to type in the names of the fields. I don't mind whether or not the private
access modifier is present in the field definitions, although I prefer to not have it since the code is a bit leaner that way. Similarly, I'm willing to accept a solution that doesn't generate read-only fields, but I do prefer them since I seldom want to change a service after my class is initialised.
What I've Tried
The quickest solution I know of at the moment is to type something like the following in another section of code and tell Visual Studio to create the class and constructor from its usage.
new SomeClass((ServiceA)null, (ServiceB)null);
However, I don't always work in this order; sometimes I want to create a class before using it. So what I typically do is this:
- Invoke the
ctor
code snippet. - Fill in the constructor body.
- Use CodeRush Xpress to generate the fields. (It provides a way to generate read-only fields, whereas Visual Studio doesn't make them read-only.)
Code-snippets work well, but I don't think they support variable numbers of parameters, so they're probably not suited to this problem.
class1
,class2
, etc. – Blandishment