2025-02-20 16:10:09 +03:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2025-01-12 14:17:19 +03:00
|
|
|
using System.Reflection;
|
2025-02-07 16:53:12 +03:00
|
|
|
using mROA.Abstract;
|
|
|
|
|
using mROA.Implementation.Attributes;
|
2025-01-12 14:17:19 +03:00
|
|
|
|
2025-02-20 16:10:09 +03:00
|
|
|
namespace mROA.Implementation.Backend
|
2025-01-11 22:44:29 +03:00
|
|
|
{
|
2025-07-11 23:18:24 +03:00
|
|
|
public class InstanceRepository : IRealStoreInstanceRepository
|
2025-02-20 16:10:09 +03:00
|
|
|
{
|
2025-03-11 14:43:16 +03:00
|
|
|
public static object[] EventBinders = { };
|
2025-03-10 12:34:04 +03:00
|
|
|
|
2025-07-18 13:49:36 +03:00
|
|
|
private readonly IRepresentationModuleProducer _representationModuleProducer;
|
2025-03-10 12:34:04 +03:00
|
|
|
|
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
|
|
|
|
2025-07-11 23:18:24 +03:00
|
|
|
public InstanceRepository(IRepresentationModuleProducer representationModuleProducer)
|
2025-02-24 09:57:35 +03:00
|
|
|
{
|
2025-07-11 23:18:24 +03:00
|
|
|
_representationModuleProducer = representationModuleProducer;
|
2025-03-11 19:42:10 +03:00
|
|
|
_storage = new ExtensibleStorage<object>();
|
2025-02-24 09:57:35 +03:00
|
|
|
}
|
|
|
|
|
|
2025-03-10 12:34:04 +03:00
|
|
|
public int ResisterObject<T>(object o, IEndPointContext context)
|
2025-02-20 16:10:09 +03:00
|
|
|
{
|
2025-03-11 19:42:10 +03:00
|
|
|
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)
|
2025-07-11 23:18:24 +03:00
|
|
|
((IEventBinder)binder).BindEvents(o, context, _representationModuleProducer, last);
|
2025-01-12 14:17:19 +03:00
|
|
|
|
2025-02-20 16:10:09 +03:00
|
|
|
return last;
|
|
|
|
|
}
|
2025-01-11 22:44:29 +03:00
|
|
|
|
2025-05-05 16:30:23 +03:00
|
|
|
public void ClearObject(ComplexObjectIdentifier id, IEndPointContext context)
|
2025-02-20 16:10:09 +03:00
|
|
|
{
|
2025-03-11 19:42:10 +03:00
|
|
|
_storage.Free(id.ContextId);
|
2025-02-20 16:10:09 +03:00
|
|
|
}
|
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
|
2025-02-20 16:10:09 +03:00
|
|
|
{
|
2025-03-11 19:42:10 +03:00
|
|
|
var value = _storage.GetValue(id.ContextId);
|
2025-03-13 20:01:17 +03:00
|
|
|
|
2025-03-11 19:42:10 +03:00
|
|
|
if (value == null)
|
|
|
|
|
{
|
|
|
|
|
throw new NullReferenceException("Cannot find that object. It is null");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (T)value;
|
2025-02-20 16:10:09 +03:00
|
|
|
}
|
2025-02-03 10:06:05 +03:00
|
|
|
|
2025-05-15 08:20:32 +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)
|
2025-02-20 16:10:09 +03:00
|
|
|
{
|
2025-03-10 12:34:04 +03:00
|
|
|
return _singletons.GetValueOrDefault(type.GetHashCode()) ??
|
|
|
|
|
throw new ArgumentException("Unregistered singleton type");
|
2025-02-20 16:10:09 +03:00
|
|
|
}
|
2025-01-12 14:17:19 +03:00
|
|
|
|
2025-03-10 12:34:04 +03:00
|
|
|
public int GetObjectIndex<T>(object o, IEndPointContext context)
|
2025-02-20 16:10:09 +03:00
|
|
|
{
|
2025-03-11 19:42:10 +03:00
|
|
|
var index = _storage.GetIndex(o);
|
2025-03-10 12:34:04 +03:00
|
|
|
return index == -1 ? ResisterObject<T>(o, context) : index;
|
2025-02-20 16:10:09 +03:00
|
|
|
}
|
2025-01-13 23:14:22 +03:00
|
|
|
|
2025-03-10 16:35:56 +03:00
|
|
|
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
|
|
|
}
|