1.0.14 • Published 1 year ago

com.hashbang.monarch.core v1.0.14

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

Monarch Core

Monarch Core is a lightweight, flexible, and extensible Inversion of Control (IoC) framework for Unity game engine, designed to make it easy to create and manage dependencies between game objects and components.

The framework provides a number of services, including a message bus, a localization system, an asset management system, and an event system. These services are built on top of a simple and powerful IoC container, which allows developers to easily manage dependencies and create complex object graphs with minimal code.

Monarch Core is highly configurable and can be extended to meet the specific needs of a project. It is designed to work with Unity's existing systems and can be easily integrated into any Unity project, regardless of its size or complexity.

The framework also includes a number of utility classes and extensions to make common programming tasks easier, such as string manipulation, type checking, and task management.

Overall, Monarch Core is a powerful and flexible framework that can help developers build better, more maintainable Unity projects with ease.

Installers

In Monarch, installers are used to install and configure services in the dependency injection (DI) container. The DI container is used to manage and resolve dependencies between objects in the application.

To create a new installer, you need to create a new class that implements the IInstaller interface. This interface requires two methods: InstallServices and DisposeServices.

The InstallServices method is called when the installer is installed, and it is used to register the services in the DI container. You can use the Injector object passed as a parameter to register services using the SetSingleton, SetInstance, SetFactory, or SetWeakSingleton methods.

Here's an example of an installer that installs a MailService service:

public class MailServiceInstaller : IInstaller
{
    public int ExecutionOrder { get; } = InstallerSequence.StoreServiceOrder + 1;

    private MailService _service;

    public void InstallServices(Injector injector, IMonarchConfiguration configuration)
    {
        _service = new MailService(injector, configuration);
        injector.SetSingleton<MailService>(_service);
    }

    public void DisposeServices()
    {
        if (_service != null)
        {
            _service.Destroy();
            _service = null;
        }

        IoC.inject.RemoveAllInjectorsFor<MailService>();
    }
}

The DisposeServices method is called when the installer is uninstalled, and it is used to remove the services from the DI container. This method should dispose any disposable objects and remove any references to the DI container.

You can install an installer by calling one of the InstallerExtensions.Install methods. The Install method takes an instance of the installer as a parameter and installs the services in the DI container. You can also use the InstallerExtensions.InstallServicesInAssembly method to automatically install all the installers in the current assembly.

Here's an example of how to install the MailServiceInstaller:

MailServiceInstaller installer = new MailServiceInstaller();
installer.Install();

// Or, to install all the installers in the current assembly:
InstallerExtensions.InstallServicesInAssembly();

You can also install installers with a custom configuration by calling the InstallerExtensions.InstallServicesInAssemblyWithConfiguration method and passing an instance of the configuration object.

var configuration = new MonarchConfiguration();
InstallerExtensions.InstallServicesInAssemblyWithConfiguration(configuration);

Once the services are installed, you can use the DI container to resolve the dependencies between objects in your application. You can do this by calling the Injector.Get<T> method, where T is the type of the service you want to resolve.

var mailService = IoC.inject.Get<MailService>();

When your application is shutting down, you should call the InstallerExtensions.Dispose method to uninstall all the installers and remove all the services from the DI container.

InstallerExtensions.DisposeServicesInAssembly();

You can view more documentatoin for Monarch Core Here.