1

I have two case:

-Use static service

public class TestService {
   public static bool FunctionA(int b) {
      return b > 0;
   }
}

-Use interface

public interface ITestSerice {
    bool FunctionA(int b);
}

public class TestService : ITestService {
    public bool FunctionA(int b) {
        return b > 0;
    }
}

Static class is very simple. But I often see more people using the interface (or higher than Dependency Injection). Please explain to me why and when to use the interface? (which is better?)

Sorry if my english is too bad :D

Khai King
  • 39
  • 5
  • Avoid static services, they are very inflexible, you can't switch the implementation with another(for testing or other purposes). An interface is better, you can implement it with any class, ideally the client should not new-up the implementing class and have it passed in so it only depends on the interface. Dependency injection is there to help with exactly this last point, pass in registered implementation without having the clients know the exact implementation. – Titian Cernicova-Dragomir Mar 24 '18 at 07:32
  • 3
    Static service and Interface are totally different things. And Dependency Injection is something else. What you want to achieve ? – SSD Mar 24 '18 at 07:33
  • Let's say that `TestService` hits the database. And `OtherClass` uses `TestService`. If `OtherClass` uses a `static` `TestService` then you have a problem - you can't unit test `OtherClass` without hitting the database. If `OtherClass` uses an injected `ITestService` then the problem goes away - you can mock `ITestService` to ensure the database is not hit. – mjwills Mar 24 '18 at 08:08

3 Answers3

1

If you use a static method, then the code which calls that method is tied to that particular implementation.

If your class uses that method via an interface, this allows you to use a different implementation of that interface.

The static method makes your tho classes closely-coupled. The interface approach makes them loosely-coupled. What is the difference between loose coupling and tight coupling in the object oriented paradigm?

Richardissimo
  • 5,596
  • 2
  • 18
  • 36
0

Please explain to me why and when to use the interface?

It boils down to Pull vs Push - meaning is it your class responsible for defining or pulling service implementation or some else is taking responsibility to inject or push that service implementation ?

If you going to use static version, then you basically coupled your class with that service implementation only.

Biggest point is to consider if you can unit test your class without concrete dependency of service implementation - which you cannot if you use static version.

If you use Interface version - you can provide mock implementation of service while writing unit test for class. make sense ?

which is better?

Using Interface.

rahulaga-msft
  • 3,964
  • 6
  • 26
  • 44
0

The answer to the question why developers generally prefer dependency on interfaces instead of static implementations, sits in one of the SOLID principles called Dependency inversion principle.

I'll try to give you an example: Let's imagine you have a store. And you want to buy juice making machine and start selling the juice. Let's imagine that in your town there are several variants of machines, and for each variant there are different juice flavor pots with different forms, which makes them irreplaceable in terms of machines. Let's imagine you want to sell all flavors in your store, in that way you would need to have 4 or 5 different machines. Wouldn't it be great if all flavor producers have one standard for flavor pots, so you can use one machine for any of those flavors?

So in my example the unified flavor pot's standard is interface. Having the standard makes your juice machine independent of the flavor pot implementation, so at any time you can change the flavor in your machine or even create your own flavor.