Skip to main content

Provider

Provider base class allows you to create simple IOC containers. Ideally your provider should have virtuals methods which return interface types that can be overriding in concrete providers (runtime provider & testing provider). Main goal of this pattern is to decouple dependencies and easy testing.

Implementation#

Simple provider implementation
public class SimpleProvider : Provider<SimpleProvider> {
public SimpleSettings Settings { get; set; }
public virtual ISimpleComponent CreateComponent() => new SimpleComponent(Settings.Health);
}
Singletons

Can be created by storing the objects in member variables


Initialization#

Providers need to be initialized once before use and this can be done by calling the initialize method.

Provider Initialization
Provider<SimpleProvider>.Initialize(new TestSimpleProvider(), provider => {
provider.Settings = _settings;
});

Initialize has 2 overloads :-

  • Initialize(provider, initializer) :-
    • provider - it's the concrete instance of your provider, usefull when you are overiding your provider and want to use differnt provider depending upon config
    • initializer - it's a callback that is called just after the provider is initialized, you can set singleton member variables here for eager initialization
  • Initialize(initializer) - same as above just that provider is created according to the generic type, can't use different implementation's of provider in this case

Usage#

Objects can be requested in the following manner.

Provider usage
var component = Provider<TestProvider>.Current.CreateComponent();