Gameframe.ServiceProvider
Simplified service provider implementation minus the fancy stuff
com.gameframe.serviceprovider Unity Compatibility

Install via UPM
Add to Unity Package Manager using this URL
https://www.pkglnk.dev/serviceprovider.git README Markdown
Copy this to your project's README.md
## Installation
Add **Gameframe.ServiceProvider** to your Unity project via Package Manager:
1. Open **Window > Package Manager**
2. Click **+** > **Add package from git URL**
3. Enter:
```
https://www.pkglnk.dev/serviceprovider.git
```
[](https://www.pkglnk.dev/pkg/serviceprovider)README
Gameframe.ServiceProvider 👋
A simple service provider implementation for use in Unity3D. Provides singleton and transient services.
Because the focus was on creating a simplified service provider this package does not do dependency graphs, property or constructor injection. It is probably most easily used as a glorified singleton manager.
Quick Package Install
Using UnityPackageManager (for Unity 2019.3 or later)
Open the package manager window (menu: Window > Package Manager)
Select "Add package from git URL...", fill in the pop-up with the following link:
https://github.com/coryleach/UnityServiceProvider.git#1.0.0
Using UnityPackageManager (for Unity 2019.1 or later)
Find the manifest.json file in the Packages folder of your project and edit it to look like this:
{
"dependencies": {
"com.gameframe.serviceprovider": "https://github.com/coryleach/UnityServiceProvider.git#1.0.0",
...
},
}
Usage
Configure your ServiceProvider and ServiceCollection
By Default ServiceProvider.Current and ServiceCollection.Current are implemented using the BasicServiceProvider class that is provided so you can immediately use ServiceCollection.Current and ServiceProvider.Current right out of the box without any setup.
//ServiceProvider is used to get service instance(s)
// MyServiceProvider implements IServiceProvider
ServiceProvider.Current = MyServiceProvider;
//ServiceCollection handles adding services to be provided
//It also controls how they will be configured (Singleton vs Transient)
//Singleton = All Get calls will return the same service instance
//Transient = Every Get call will return a newly created instance of the service
//MyServiceCollection implements IServiceCollection
ServiceCollection.Current = MyServiceCollection;
Adding Singleton Service
//This will configure a specific instance which already has been created
ServiceCollection.Current.AddSingleton(serviceInstance.GetType(),serviceInstance);
//You can also configure a service to be served when requesting a parent type
ServiceCollection.Current.AddSingleton(typeof(ParentClass),childClassServiceInstance);
//You can also configure a function that will be used to construct the singleton service on demand
ServiceCollection.Current.AddSingleton((provider)=> new MyService());
Adding Transient Service
//Transient services require a factory because a new instance is created every time
ServiceCollection.Current.AddTransient<ServiceType>((provider) => new ServiceType());
//Adding a transient service with a parent type
ServiceCollection.Current.AddTransient<ParentType,ServiceType>((provider) => (ParentType)new ServiceType());
Getting a Service
//Get a particular service
var service = ServiceProvider.Get<ServiceType>();
//If more than one service of the given type is provided we can get them all
var services = ServiceProvider.GetAll<ServiceType>();
Using the Bootstrapper
The UnityServiceProviderBootstrapper can be used to configure singleton services from MonoBehaviours or ScriptableObjects In Awake() UnityServiceProviderBootstrapper will configure the ServiceCollection and ServiceProvider to an instance of UnityServiceProvider Additional services can be configured by creating a child class of UnityServiceProviderBootstrapper and overriding the ConfigureServices method. Be sure to include a call to base.ConfigureServices(unityServiceProvider) or the singletonService list will not be added.
Author
👤 Cory Leach
- Twitter: @coryleach
- Github: @coryleach
Show your support
Give a ⭐️ if this project helped you!
This README was generated with ❤️ by Gameframe.Packages
Comments
No comments yet. Be the first!
Sign in to join the conversation
Sign In