35 lines
833 B
C#
35 lines
833 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Code.DI
|
|
{
|
|
public class DIContainer : MonoBehaviour
|
|
{
|
|
private readonly Dictionary<Type, object> _services = new();
|
|
|
|
private void Awake()
|
|
{
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
public void Register<T>(T service)
|
|
{
|
|
var type = typeof(T);
|
|
if (!_services.TryAdd(type, service))
|
|
{
|
|
Debug.LogWarning($"Service {type} is already registered.");
|
|
}
|
|
}
|
|
|
|
public T Resolve<T>()
|
|
{
|
|
var type = typeof(T);
|
|
if (_services.TryGetValue(type, out var service))
|
|
{
|
|
return (T)service;
|
|
}
|
|
throw new Exception($"Service {type} not found.");
|
|
}
|
|
}
|
|
} |