Service Defining Type

  8. Reference No Comments

When you register a global or a local service, you will always have to specify – either implicitly or explicitly – one or more defining types for it.

In the below example, the Logger class is configured to be a global service with the defining types Logger and ILogger, by explicitly specifying the types in the constructors of two [Service] attributes:

[Service(typeof(Logger)), Service(typeof(ILogger))]
class Logger : ILogger { }

Components that derive from MonoBehaviour<T…> will automatically receive a global service as an Init method argument, if the type of the argument matches one of the defining types of the service.

class ClientA : MonoBehaviour<ILogger>
{
     protected override void Init(Logger logger) => Debug.Log($"ClientA received {logger}.");
}

class ClientB : MonoBehaviour<Logger>
{
     protected override void Init(Logger logger) => Debug.Log($"ClientB received {logger}.");
}

Leave a Reply

Your email address will not be published. Required fields are marked *