Files
mROA/mROA/Implementation/Backend/InstanceRepository.cs
T

88 lines
3.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using mROA.Abstract;
using mROA.Implementation.Attributes;
namespace mROA.Implementation.Backend
2025-01-11 22:44:29 +03:00
{
public class InstanceRepository : IRealStoreInstanceRepository
{
public static object[] EventBinders = { };
2025-07-18 13:49:36 +03:00
private readonly IRepresentationModuleProducer _representationModuleProducer;
2025-07-10 18:58:49 +03:00
private Dictionary<int, object?> _singletons = new();
private readonly IStorage<object> _storage;
2025-01-11 22:44:29 +03:00
public InstanceRepository(IRepresentationModuleProducer representationModuleProducer)
{
_representationModuleProducer = representationModuleProducer;
_storage = new ExtensibleStorage<object>();
}
public int ResisterObject<T>(object o, IEndPointContext context)
{
var last = _storage.Place(o);
2025-05-07 09:55:15 +03:00
var sharedType = typeof(IShared);
var interfaces = o.GetType().GetInterfaces();
var generic = interfaces.Where(i => sharedType.IsAssignableFrom(i) && i != sharedType)
.Select(i => typeof(IEventBinder<>).MakeGenericType(i));
2025-07-10 18:58:49 +03:00
2025-05-07 09:55:15 +03:00
var binders = EventBinders.Where(i => generic.Any(g => g.IsAssignableFrom(i.GetType())));
foreach (var binder in binders)
((IEventBinder)binder).BindEvents(o, context, _representationModuleProducer, last);
return last;
}
2025-01-11 22:44:29 +03:00
public void ClearObject(ComplexObjectIdentifier id, IEndPointContext context)
{
_storage.Free(id.ContextId);
}
2025-01-11 22:44:29 +03:00
2025-07-10 18:58:49 +03:00
public T GetObject<T>(ComplexObjectIdentifier id, IEndPointContext context) where T : class
{
var value = _storage.GetValue(id.ContextId);
2025-03-13 20:01:17 +03:00
if (value == null)
{
throw new NullReferenceException("Cannot find that object. It is null");
}
return (T)value;
}
2025-02-03 10:06:05 +03:00
public T GetSingletonObject<T>(IEndPointContext context) where T : class, IShared
{
return GetSingletonObject(typeof(T), context) as T ??
throw new ArgumentException("Unregistered singleton type");
}
public object GetSingletonObject(Type type, IEndPointContext context)
{
return _singletons.GetValueOrDefault(type.GetHashCode()) ??
throw new ArgumentException("Unregistered singleton type");
}
public int GetObjectIndex<T>(object o, IEndPointContext context)
{
var index = _storage.GetIndex(o);
return index == -1 ? ResisterObject<T>(o, context) : index;
}
public void FillSingletons(params Assembly[] assembly)
{
var types = assembly.SelectMany(x => x.GetTypes()).Where(type =>
type is { IsClass: true, IsAbstract: false, IsGenericType: false } &&
type.GetCustomAttributes(typeof(SharedObjectSingletonAttribute), true).Length > 0);
_singletons =
types.ToDictionary(
t => t.GetInterfaces().FirstOrDefault(i =>
i.GetCustomAttributes(typeof(SharedObjectInterfaceAttribute), true).Length > 0)!.GetHashCode(),
Activator.CreateInstance);
}
2025-02-05 10:58:50 +03:00
}
2025-01-11 22:44:29 +03:00
}