Azure函数-函数应在静态类中编写 [英] Azure functions - should functions be written inside static classes
问题描述
我开始尝试Azure功能.我正在使用Visual Studio 2017预览版15.3.当我右键单击我创建的Azure Functions项目并选择Add> New Item ...> Azure Function时,Visual Studio生成的默认模板是带有 public的
方法(该函数). public静态类
静态异步Task
I'm starting to try out Azure functions. I'm using Visual Studio 2017 Preview version 15.3. When I right click on the Azure Functions project I created, and select Add>New Item...>Azure Function, the default template Visual Studio generates is of a public static class
with a public static async Task
method (the function).
该类是否需要为静态类(我将其更改为非静态类,并且似乎可以正常工作)?这是Azure功能的最佳实践吗?如果是这种情况,使用非静态类来保存Azure Function方法可能会出现什么问题?
Does the class need to be static (I changed it to non-static and it seems to work)? Is that a best practice for Azure functions? If that is the case, what problems might rise by using a non-static class to hold the Azure Function method?
推荐答案
该类是否需要为静态类(我将其更改为非静态类,并且似乎可以正常工作)?这是Azure功能的最佳实践吗?
Does the class need to be static (I changed it to non-static and it seems to work)? Is that a best practice for Azure functions?
静态类只能包含静态成员,并且不能实例化.将类更改为非静态将允许您添加非静态成员并创建此类的实例.
A static class can only contain static members, and it can't be instantiated. Changing the class to non-static will allow you to add non-static members and create an instance of this class.
请检查是否需要向此类添加非静态成员或创建此类的实例.由于单一责任原则"规定每个类都应对软件提供的功能的一部分负责,因此建议您创建一个新类并将非静态成员放在那里.
Please check whether you need to add non-static members to, or create an instance of, this class. Due to the Single Responsibility Principle, which states that every class should have responsibility over a single part of the functionality provided by the software, I suggest you create a new class and put the non-static members there.
如果是这种情况,使用非静态类保存Azure Function方法可能会出现什么问题?
If that is the case, what problems might rise by using a non-static class to hold the Azure Function method?
我想使用非静态类的原因是要在其中创建一些非静态成员.这样做会使您的课程变得复杂且难以维护.
I suppose the reason you want to use a non-static class is that you want to create some non-static members in it. Doing so will make your class complex and difficult to maintain.
我的最终答案是可以将类更改为非静态类.为了使类保持简单,我建议您将类保持静态.
My final answer is that the class can be changed to non-static. To keep the class simple, I suggest you keep the class static.
这篇关于Azure函数-函数应在静态类中编写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!