Code Monkey home page Code Monkey logo

unityserviceprovider's Introduction

Gameframe.ServiceProvider ๐Ÿ‘‹

Version Twitter: coryleach

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

Show your support

Give a โญ๏ธ if this project helped you!


This README was generated with โค๏ธ by Gameframe.Packages

unityserviceprovider's People

Contributors

coryleach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

oyemad

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.