Usage.Service Container - JuDelCo/Core GitHub Wiki

Namespace: Ju.Services


The Service Container is the dependency injection container of this framework, it will help to achieve decoupling in your project.

There is no automatic injection anywhere in the code (you can implement your own though).

Service Container API

void RegisterService<T>() where T : IService;
void RegisterService<T1, T2>() where T2 : T1, IService;
void RegisterLazyService<T>() where T : IService;
void RegisterLazyService<T1, T2>() where T2 : T1, IService;
void RegisterFactory<T>(Func<T> classConstructor);

T Get<T>();

void UnloadService<T>() where T : IService;
RegisterService<T>()

Register a service. The service must implement the empty interface IService.

It will be loaded right away when you get any service for the first time.

You can register a service T2 but as the type T1 (T2 must inherit or implement T1) so you can use interfaces or abstract services. For example:

// This will register TaskService using ITaskService as a type
ServiceContainer.RegisterService<ITaskService, TaskService>();

// Get the TaskService but using the interface ITaskService
ServiceContainer.Get<ITaskService>();
// Same but using the syntactic sugar (See Getting Started page for your platform)
Core.Get<ITaskService>();
// Same, but shorter
Core.Task
RegisterLazyService<T>()

This works the same as the previous method but the service won't load until you get it for the first time using Get.

RegisterFactory<T>(Func<T> classConstructor)

Register a constructor function in order to set up a factory of objects. You can use any type you want.

T Get<T>()

This method will return a service reference or constructs a new object if it was registered as a factory.

UnloadService<T>()

Unloads a service so it won't be available anymore.

Service Container API (Using string IDs)

Additionally, you can register multiple services or factories of the same type if you give them a string ID.

void RegisterService<T>(string id) where T : IService;
void RegisterService<T1, T2>(string id) where T2 : T1, IService;
void RegisterLazyService<T>(string id) where T : IService;
void RegisterLazyService<T1, T2>(string id) where T2 : T1, IService;
void RegisterFactory<T>(string id, Func<T> classConstructor);

T Get<T>(string id);

void UnloadService<T>(string id) where T : IService;
⚠️ **GitHub.com Fallback** ⚠️