Sometimes you might need to inject a different instance of the same service to every client that requests the service. This can be achieved in Init(args) using value providers:
- Create a class that implements IValueProvider<T>, where T is the concrete or defining type of your service.
- Implement IValueProvider<T>.Value in such a way, that a new instance of T is returned every time that the property is called.
- Add the [Service] attribute to the value provider class, and specify defining types for the service which are assignable from T.
[Service(typeof(ILogger))]
class LoggerProvider : IValueProvider<Logger>
{
public Logger Value = new Logger();
}
You can also return services that are customized for the client making the request by providing a custom implementation for the IValueProvider<T>.TryGetFor(Component client, out T value) method:
[Service(typeof(ILogger))]
class LoggerProvider : IValueProvider<Logger>
{
public Logger Value = new Logger(context: null);
public bool TryGetFor(Component client, out Logger logger)
{
logger = new Logger(context: client);
return true;
}
}