Types of Providers -UseClass Provider

1 minute read

Class Provider configures the Injector to return an instance of useClass for a token. It is similar to type provider but it has different way of providing corresponding dependency.

There is a typical example to explain useClass provider. Let’s understand this with logger example. We have a function named Log; it takes a message and does in-memory logging i.e. console.log. Suppose this is implemented in thousand components in this way. Now if requirement changes and we are asked to use http logger in place of in-memory logger. If we go and change individual components then we may end up in breaking software programming basic engineering principle that says, when we are changing something we should not change many things. So here we will use polymorphism concept that says once you have class defined and most of the components are using that class, and then tomorrow if you just want to change the behavior of that object then as per object oriented principle, create a new class rather than modifying that class. Means we can create separate class and inherit it from base class. Now we will be having two classes and we can switch anytime between them as per requirement. Tomorrow they can ask us to change it to another thing may be file log.

This can be achieved through useClass provider as it enables us to modify code as per changing requirements. Our tutorial video Use Class Provider | Angular explains how without changing any component, by just adding a new class with useClass provider we resolved the issue in this example.