简体   繁体   English

Visual Studio中的构造函数依赖性代码段

[英]Constructor dependency code snippet in visual studio

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. 编写它们非常繁琐,我一直在寻找Visual Studio中的代码片段来自动添加一个到构造函数但没有找到一个。

What I want is: 我想要的是:

  • When adding a dependency to a constructor some snippet automatically creates a local variable and assigns to it. 向构造函数添加依赖项时,某些代码段会自动创建一个局部变量并分配给它。

OR 要么

  • Add a private variable and then some snippet automatically adds it to the constructor and assigns it to the local variable. 添加一个私有变量,然后一些片段自动将其添加到构造函数并将其分配给局部变量。

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. 如果您有R#,您可以输入字段声明,然后突出显示它们并按Alt-Enter ,这将为您提供生成构造函数和字段分配的选项。

在此输入图像描述

If you don't have Resharper, you can add the parameter on the constructor, write the assignament to an unexisting property and hit CTRL+. 如果你没有Resharper,你可以在构造函数中添加参数,将赋值写入一个未存在的属性并按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+. 然后点击CTRL +。 while on the asignament line, and select create field, and you'll get this: 在asignament线上,选择创建字段,你会得到这个:

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 我不知道以前的版本,但在vanilla Visual Studio 2017中,您实际上可以添加构造函数参数

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 : 然后将光标放在service ,从“快速操作”中,您可以选择Introduce and initialize field _someService ,它将执行您想要的操作:

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 您可以根据需要轻松添加代码片段,在XAML中定义代码片段并将其添加到editior中,您可以使用“类名”等占位符将其用作构造函数,然后将变量作为静态文本放入其中

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 我不想编写代码,因为它是重复的,您可以在这里查看如何执行: 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. 你试图用ISomeService和IAnotherService做的是做依赖性倒置。

I would strongly recommend you combine this with a dependency injection framework. 我强烈建议你将它与依赖注入框架结合起来。 There are many available but one i would recommend is MEF. 有许多可用,但我建议的是MEF。 MEF is build into the .net framework. MEF构建在.net框架中。 For example your code would look like this with MEF 例如,您的代码与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. 现在,MEF将确保在创建类时填充SomeService和AnotherService属性。 It will construct (if needed) a instance of SomeService, fill all it's dependencies and put it in the right property. 它将构造(如果需要)SomeService的实例,填充它的所有依赖项并将其放在正确的属性中。 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 有关MEF的更多详细信息,请访问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. Telerik的JustCode可以完全满足您的需求。
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 http://www.telerik.com/products/justcode/quick-fixes.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM