Provide your own classes
Provide a class
The Attribute Provider gives instances of the classes marked [Provide]. To provide one of your classes,
perform the following steps:
- Add the
[Provide]attribute to the class. - Add a member of the class's type, one of its base classes, or one of its interfaces to a script.
using Selvage.Providers;
[Provide] // 1. The class Selvage creates
public class Inventory : IInventory { ... }
[Inject] Inventory inventory; // 2. Replaces Inventory.Instance
[Inject] IInventory items; // 2. Also works with an interface
Selvage creates instances when members need them, and injects their own [Inject] members. To set the
class's lifetime, pass it to the attribute: [Provide(Lifetime.Pooled)].
Class requirements
Selvage creates the class with new, so the class must meet the following requirements:
- It isn't a Unity object, such as a component or a ScriptableObject.
- It isn't abstract or static.
- It has a constructor without parameters. The constructor can be private.
Code analysis reports a class that doesn't meet these requirements as SLV0007, and the Attribute Provider leaves it out with an error in the Console window.
To provide a component, refer to Provide scene components. A
ScriptableObject can't have [Provide], because Selvage would create a blank one that wins over your
configured asset. To inject a configured ScriptableObject or another asset, refer to Provide
assets. To provide one your code creates, add a function that creates it to the
registry:
RegistryProvider.Listed.Add(() => ScriptableObject.CreateInstance<GameSettings>());
The Attribute Provider reads the classes marked [Provide] from every assembly that references
Selvage. When two classes could be injected into a member and nothing tells them apart, the Validation tab names
both before you enter Play mode. To see every class the Attribute Provider gives, expand Attribute
Provider in the provider list.
Provide a generic class
A generic class marked [Provide] is given for every type it can become:
[Provide]
public class Repository<T> : IRepository<T> { ... }
[Inject] IRepository<Enemy> enemies; // a Repository<Enemy>
If a non-generic class is also marked for the same type, the non-generic class wins.
Wrap a class
To add behavior around a class, such as logging, write a wrapper class that receives the original class by its own
type. Add an empty [Preference] to the wrapper: a preference says which members a
class is for, and an empty one matches every member, so the wrapper wins over the original class:
[Provide]
public class SteamSave : ISave { ... }
[Provide, Preference]
public class LoggedSave : ISave
{
[Inject] SteamSave inner;
}
In this example, members of type ISave receive LoggedSave, which receives
SteamSave. The wrapper's member asks for the concrete SteamSave type, so the wrapper never
receives itself. A wrapper whose member asks for ISave would receive itself, and Selvage would report
the dependency cycle by name. For more information, refer to Choose
between several candidates.