简体   繁体   English

ASP.NET和控制器中的静态方法

[英]ASP.NET and static method in controller

Assume that WebApi2 controller has a SearchClient which is configured in scoped-lifestyle dependency on startup. 假设WebApi2控制器具有SearchClient其在作用域生活方式依赖性配置在启动时。

public class SearchController : ApiController {

    private readonly SearchClient _indexClient;

    public SearchController(SearchClient client) {
        _indexClient = client; // dependency injected
    }

    public IEnumerable<string> Get(string keyword){
        return SearchDocuments(_indexClient, keyword);
    }

    public static IEnumerable<string> SearchDocuments(SearchClient indexClient, string text)
    {
        return indexClient.Search(text);
    }
}

As we can see, SearchDocuments method has static keyword. 我们可以看到, SearchDocuments方法有static关键字。

My questions are; 我的问题是;

  1. How can we decide whether static method is good or bad? 我们如何判断static方法是好还是坏?
  2. Is static method safe or recommended in such a multiple-accessed web environment? 在这样一个多访问的Web环境中, static方法是安全的还是推荐的?
  3. What about async static method in web environment? 那么Web环境中的async static方法呢? Is it different from async method? 它与async方法不同吗?

How can we decide whether static method is good or bad? 我们如何判断静态方法是好还是坏?

Static methods in web applications are just like static methods in desktop applications. Web应用程序中的静态方法就像桌面应用程序中的静态方法一样。 There is no difference in how they are handled or interpreted once they run in a web application. 一旦在Web应用程序中运行,它们的处理方式或解释方式就没有区别。 So they are not bad or good, you use them for everything that is not instance-specific. 因此它们不坏或不好,您将它们用于非特定于实例的所有内容。

Is static method safe or recommended in such a multiple-accessed web environment? 在这样一个多访问的Web环境中,静态方法是安全的还是推荐的?

static fields or properties can have unwanted side effects when user or session specific data is stored in it since static variables are shared across sessions. static字段或属性在用户或会话特定数据存储在其中时可能会产生不必要的副作用,因为static变量是跨会话共享的。 But this is a method, and methods don't have a shared state. 但这是一种方法,方法没有共享状态。 Hence they are safe to use in a multi-user/multi-session environment. 因此,它们可安全地用于多用户/多会话环境中。

What about async static method in web environment? 那么Web环境中的异步静态方法呢? Is it different from async method? 它与异步方法不同吗?

Nothing other than already described in the answer to your first question. 除了您在第一个问题的答案中已经描述的以外,没有其他内容。

Just to add to the already provided answers. 只是添加已经提供的答案。

The use of static method on the controller does not really add any value and is not actually needed in the given scenario. 在控制器上使用静态方法并没有真正添加任何值,并且在给定方案中实际上并不需要。

Consider abstracting explicit dependencies in the controller. 考虑在控制器中抽象显式依赖项。

public class SearchController : ApiController {

    private readonly ISearchClient indexClient;

    public SearchController(ISearchClient client) {
        indexClient = client; // dependency injected
    }

    public IEnumerable<string> Get(string keyword){
        return indexClient.Search(keyword);
    }
}

This will also allow for loose coupling and more flexibility when testing and also refactoring as the implementation of the dependency can change without having to touch the controller. 这也将允许在测试和重构时松散耦合和更多灵活性,因为依赖性的实现可以在不必触摸控制器的情况下改变。

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

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