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,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Infrastructure.DI
|
||||
{
|
||||
public class DIContainer
|
||||
{
|
||||
private readonly Dictionary<Type, Registration> _container = new();
|
||||
|
||||
private readonly List<Type> _requests = new();
|
||||
|
||||
private readonly DIContainer _parent;
|
||||
|
||||
public DIContainer() { }
|
||||
|
||||
public DIContainer(DIContainer parent) => _parent = parent;
|
||||
|
||||
public IRegistrationOptions RegisterAsSingle<T>(Func<DIContainer, T> creator)
|
||||
{
|
||||
if (IsAlreadyRegister<T>())
|
||||
throw new InvalidOperationException($"{typeof(T)} already register");
|
||||
|
||||
Registration registration = new Registration(container => creator.Invoke(container));
|
||||
_container.Add(typeof(T), registration);
|
||||
return registration;
|
||||
}
|
||||
|
||||
public bool IsAlreadyRegister<T>()
|
||||
{
|
||||
if (_container.ContainsKey(typeof(T)))
|
||||
return true;
|
||||
|
||||
if (_parent != null)
|
||||
return _parent.IsAlreadyRegister<T>();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public T Resolve<T>()
|
||||
{
|
||||
if (_requests.Contains(typeof(T)))
|
||||
throw new InvalidOperationException($"Cycle resolve for {typeof(T)}");
|
||||
|
||||
_requests.Add(typeof(T));
|
||||
|
||||
try
|
||||
{
|
||||
if (_container.TryGetValue(typeof(T), out Registration registration))
|
||||
return (T)registration.CreateInstanceFrom(this);
|
||||
|
||||
if (_parent != null)
|
||||
return _parent.Resolve<T>();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_requests.Remove(typeof(T));
|
||||
}
|
||||
|
||||
throw new InvalidOperationException($"Registration for {typeof(T)} not exists");
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
foreach (Registration registration in _container.Values)
|
||||
{
|
||||
if (registration.IsNonLazy)
|
||||
registration.CreateInstanceFrom(this);
|
||||
|
||||
registration.OnInitialize();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (Registration registration in _container.Values)
|
||||
registration.OnDispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8344455a6210cd942a42f35b05dd49b6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Assets._Project.Develop.Runtime.Infrastructure.DI
|
||||
{
|
||||
public interface IInitializable
|
||||
{
|
||||
void Initialize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d11727eaee46452e981b458b50cf78ff
|
||||
timeCreated: 1770823156
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Assets._Project.Develop.Runtime.Infrastructure.DI
|
||||
{
|
||||
public interface IRegistrationOptions
|
||||
{
|
||||
void NonLazy();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b9015cc75cdb4bb4b853d2da12436925
|
||||
timeCreated: 1770823170
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 98360bf9a58f7d64885a7620c49a3692
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user