mirror of
https://github.com/Bragin-Stepan/project-entity.git
synced 2026-03-05 07:41:10 +00:00
init: add project
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Infrastructure.DI
|
||||
{
|
||||
public class Registration : IRegistrationOptions
|
||||
{
|
||||
private Func<DIContainer, object> _creator;
|
||||
private object _cachedInstance;
|
||||
|
||||
public bool IsNonLazy { get; private set; }
|
||||
|
||||
public Registration(Func<DIContainer, object> creator) => _creator = creator;
|
||||
|
||||
public object CreateInstanceFrom(DIContainer container)
|
||||
{
|
||||
if (_cachedInstance != null)
|
||||
return _cachedInstance;
|
||||
|
||||
if (_creator == null)
|
||||
throw new InvalidOperationException("Not has instance or creator");
|
||||
|
||||
_cachedInstance = _creator.Invoke(container);
|
||||
|
||||
return _cachedInstance;
|
||||
}
|
||||
|
||||
public void OnInitialize()
|
||||
{
|
||||
if (_cachedInstance != null)
|
||||
if (_cachedInstance is IInitializable initializable)
|
||||
initializable.Initialize();
|
||||
}
|
||||
|
||||
public void OnDispose()
|
||||
{
|
||||
if (_cachedInstance != null)
|
||||
if (_cachedInstance is IDisposable disposable)
|
||||
disposable.Dispose();
|
||||
}
|
||||
|
||||
public void NonLazy() => IsNonLazy = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user