Azure functions - should functions be written inside static classes

懵懂的女人 提交于 2020-12-30 05:35:55

问题


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).

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?


回答1:


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.

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.




回答2:


As always it depends. Among other answers which stay to keep function class static, I would like to present another case where the non-static class can be helpful. Please look at: Functions dependency injection. If you want use DI inside your functions there is no easy way to do it with static class because it's can not have instance constructors with parameters. So you are not able to write something like this:

public class HttpTrigger
{
    private readonly IMyService _service;
    private readonly HttpClient _client;

    public HttpTrigger(IMyService service, IHttpClientFactory httpClientFactory)
    {
        _service = service;
        _client = httpClientFactory.CreateClient();
    }

    [FunctionName("GetPosts")]
    public async Task<IActionResult> Get(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "posts")] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
        var res = await _client.GetAsync("https://microsoft.com");
        await _service.AddResponse(res);

        return new OkResult();
    }
}

But then I wonder if this approach is safe if there would be enormous requests?




回答3:


Given that the functions are invoked in a serverless fashion, static methods have the correct semantics here, i.e., you should assume the process can exit after every function invocation, and so you shouldn't be accumulating state on instance methods in between function invocations.

That said, we are investigating Dependency Injection.




回答4:


Static method is indeed required.

Static class is not a requirement, but a sensible default. I can't think of a good reason not to make it static, but if you have one - there's nobody to stop you. Just make sure it's not confusing for your team mates.



来源:https://stackoverflow.com/questions/45168659/azure-functions-should-functions-be-written-inside-static-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!