CallumRose.EasyDI.LifecycleHooks
1.0.4
dotnet add package CallumRose.EasyDI.LifecycleHooks --version 1.0.4
NuGet\Install-Package CallumRose.EasyDI.LifecycleHooks -Version 1.0.4
<PackageReference Include="CallumRose.EasyDI.LifecycleHooks" Version="1.0.4" />
<PackageVersion Include="CallumRose.EasyDI.LifecycleHooks" Version="1.0.4" />
<PackageReference Include="CallumRose.EasyDI.LifecycleHooks" />
paket add CallumRose.EasyDI.LifecycleHooks --version 1.0.4
#r "nuget: CallumRose.EasyDI.LifecycleHooks, 1.0.4"
#:package CallumRose.EasyDI.LifecycleHooks@1.0.4
#addin nuget:?package=CallumRose.EasyDI.LifecycleHooks&version=1.0.4
#tool nuget:?package=CallumRose.EasyDI.LifecycleHooks&version=1.0.4
EasyDI: Lifecycle Hooks
Read EasyDI README.md for information on how the DI framework works.
⚠️ This assembly is vendored into the Unity package as a prebuilt DLL. If you change it, re-run
./scripts/vendor-unity-dlls.shand commit the result — see VENDORED.md.
Why
Lifecycle hooks are what they say on the tin: ways to hook your code into your application's lifecycle.
In Unity lifecycle hooks would include MonoBehaviour events such as Awake, Update and OnDestroy. In Godot they would be _Ready, _Process and _ExitTree. In a WPF application they would be OnStartup and OnExit.
Having a framework to manage these means:
- Easier separation of our code from an application framework.
- Consistent lifecycle management.
- Easier testing.
- More portable code.
What
EasyDI has been designed to be as portable as possible across different application types. So it doesn't make any assumptions about how your lifecycle hooks will be invoked. Instead, it lets you register your own and provides a way to invoke them wherever you choose.
How
Basic Usage
public class MenuPresenter : IInitialisable, IDisposable
{
public void Initialise() { }
public void Dispose() { }
}
public class GamePresenter : IInitialisable, ITickable, IDisposable
{
public void Initialise() { }
public void Tick() { }
public void Dispose() { }
}
registry.RegisterLifecycleHook<GamePresenter>();
registry.RegisterLifecycleHook<MenuPresenter>();
var lifecycleHookManager = resolver.Resolve<ILifecycleHookManager>();
lifecycleHookManager.InvokeAll<IInitialisable>(presenter => presenter.Initialise()); // Calls Initialise on MenuPresenter and GamePresenter
lifecycleHookManager.InvokeAll<ITicktable>(presenter => presenter.Tick()); // Calls Tick on GamePresenter
lifecycleHookManager.Dispose(); // Calls Dispose on GamePresenter and MenuPresenter
Built-In Lifecycle Hooks
IInitialisable- for initialisation logicIDisposable- for cleanup logic
And some that apply to both Unity and Godot:
ITickable- for per-frame update logicIPhysicsTickable- for physics update logic
There are some convenient extension methods for invoking some of these built-in lifecycle hooks
lifecycleHookManager.InvokeInitialisables();
lifecycleHookManager.InvokeTickables();
lifecycleHookManager.InvokePhysicsTickables();
For example, in Unity you could have a MonoBehaviour to invoke the hooks that looks like this:
public class LifecycleHookInvoker : MonoBehaviour
{
private ILifecycleHookManager _lifecycleHookManager;
private void Awake()
{
var registry = ObjectRegistry.CreateRoot();
registry.RegisterLifecycleHook<ExamplePresenter>();
registry.RegisterLifecycleHook<ExamplePresenter2>();
IObjectResolver resolver = registry.Build();
_lifecycleHookManager = resolver.Resolve<ILifecycleHookManager>();
_lifecycleHookManager.InvokeInitialisables();
}
private void Update()
{
_lifecycleHookManager.InvokeTickables();
}
private void FixedUpdate()
{
_lifecycleHookManager.InvokePhysicsTickables();
}
private void OnDestroy()
{
_lifecycleHookManager.Dispose();
}
}
Custom Lifecycle Hooks
You can define your own lifecycle hook by creating an interface that implements ILifecycleHook, and it'll work just like the built-in ones.
public interface IStartable : ILifecycleHook
{
void Start();
}
public static class ILifecycleHookManagerExtensions
{
public static void InvokeStartables(this ILifecycleHookManager lifecycleHookManager)
{
lifecycleHookManager.InvokeAll<IStartable>(startable => startable.Start());
}
}
lifecycleHookManager.InvokeStartables();
Lifecycle Hooks in Child Resolvers
ILifecycleHookManager is registered as scoped so each child resolver can manage its own lifecycle hooks. This is useful, for example, in Unity or Godot scene management where each scene will have its own set of lifecycle hooks.
var parentLifecycleHookManager = parentResolver.Resolve<ILifecycleHookManager>();
parentLifecycleHookManager.InvokeInitialisables(); // Invokes initialisables in the parent resolver
var childLifecycleHookManager = childResolver.Resolve<ILifecycleHookManager>();
childLifecycleHookManager.InvokeInitialisables(); // Invokes initialisables in the child resolver. Parent initialisables are not invoked again.
childLifecycleHookManager.Dispose(); // Disposes of disposables in the child resolver. Parent disposables are not disposed.
parentLifecycleHookManager.Dispose(); // Disposes disposables in the parent resolver.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- CallumRose.EasyDI (>= 1.1.0)
-
net8.0
- CallumRose.EasyDI (>= 1.1.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on CallumRose.EasyDI.LifecycleHooks:
| Package | Downloads |
|---|---|
|
CallumRose.EasyDI.Godot.Core
Contains the core classes to get EasyDI LifetimeScopes working in Godot |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.4 | 136 | 8/11/2026 |