6

Assume that WebApi2 controller has a SearchClient which is configured in scoped-lifestyle dependency on startup.

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.

My questions are;

  1. How can we decide whether static method is good or bad?
  2. Is static method safe or recommended in such a multiple-accessed web environment?
  3. What about async static method in web environment? Is it different from async method?
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Youngjae
  • 24,352
  • 18
  • 113
  • 198
  • Why would you make it static and require a SearchClient when it could be instance and not need the parameter? But, static is not safe for properties that are changed by controller actions, not for methods or actually static properties (that don't change). – Camilo Terevinto Jul 12 '17 at 11:10
  • That static method serves no real purpose. On a separate not the controller should depend on abstractions and not concretions, assuming `SearchClient` is not an abstract class. – Nkosi Jul 12 '17 at 11:11
  • @CamiloTerevinto // Well, sometimes, Resharper recommends to make it. And it allows to move method to another class less risky which makes refactoring easier. – Youngjae Jul 12 '17 at 11:12
  • Resharper is recommending to move an instance method (cause it uses instance data) to a static method? That's weird. I do agree it's easier to move out of the class though – Camilo Terevinto Jul 12 '17 at 11:14
  • @Youngjae hence the reason for depending on abstractions. – Nkosi Jul 12 '17 at 11:17
  • @CamiloTerevinto // yes, Resharper occationally recommends it. similar discussion is here; https://stackoverflow.com/q/169378/361100 – Youngjae Jul 15 '17 at 10:56

2 Answers2

6

How can we decide whether static method is good or bad?

Static methods in web applications are just like static methods in desktop applications. There is no difference in how they are handled or interpreted once they run in a web application. 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?

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. 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? Is it different from async method?

Nothing other than already described in the answer to your first question.

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

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.

Nkosi
  • 235,767
  • 35
  • 427
  • 472