Types of Providers -UseValue Provider

1 minute read

UseValue provider configures the Injector to return a value for a token. It comes in that category of providers where we do not have unique thing to identify them at run time.

In our video session Use Value Provider | useValue | Tutorial we will explain

  • What UseValue providers are?
  • What is their significance in web programming?

There are certain scenarios where we want to insert certain set of values/constants into our code base like components/modules etc. Rather than hard coding them inside the component/module, we can set them as a service. Let’s go through one real time example to understand this.

Suppose we prepared a web application for one of our client and want to sell same app to other customer. Here if we hardcode values/constants like logos, names, formatting as per the requirement of client1, then in order to reuse same codebase for client2 it will take lot of effort to prepare exactly same app for other client. But instead of hard coding if we keep all these values/constants in a Config file then we can deliver same app to client2 with minimal effort. This Config file can be injected into required codebase as many times as per the requirement. How to inject this Config file into our code? Answer is UseValue provider.

As we know there is no runtime existence for this, so to make it possible we use Injection Token as a class and use it as a DI token in provider. Snapshot from our video tutorial: