Упрощен интерфейс репозитория контекста

This commit is contained in:
2025-03-10 23:53:36 +03:00
parent d6423136d2
commit 22016f1c81
7 changed files with 29 additions and 32 deletions
+1 -2
View File
@@ -8,8 +8,7 @@ namespace mROA.Abstract
int HostId { get; set; }
int ResisterObject<T>(object o, IEndPointContext context);
void ClearObject(ComplexObjectIdentifier id);
T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell);
T? GetObject<T>(ComplexObjectIdentifier id);
T GetObject<T>(ComplexObjectIdentifier id);
object GetSingleObject(Type type);
int GetObjectIndex<T>(object o, IEndPointContext context);
}
@@ -7,7 +7,7 @@ namespace mROA.Implementation.Backend
public class MultiClientContextRepository : IContextRepository, IContextRepositoryHub
{
private readonly Func<int, IContextRepository> _produceRepository;
private Dictionary<int, IContextRepository> _repositories = new();
private readonly Dictionary<int, IContextRepository> _repositories = new();
public MultiClientContextRepository(Func<int, IContextRepository> produceRepository)
{
@@ -31,14 +31,8 @@ namespace mROA.Implementation.Backend
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
repository.ClearObject(id);
}
public T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell)
{
var repository = GetRepository(sharedObjectShellShell.Identifier.OwnerId);
return repository.GetObject<T>(sharedObjectShellShell.Identifier)!;
}
public T? GetObject<T>(ComplexObjectIdentifier id)
public T GetObject<T>(ComplexObjectIdentifier id)
{
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.GetObject<T>(id);
@@ -12,6 +12,7 @@ namespace mROA.Implementation
{
ContextId = contextId;
OwnerId = ownerId;
}
public static ComplexObjectIdentifier Null = new ComplexObjectIdentifier { ContextId = -2, OwnerId = -1 };
+1 -6
View File
@@ -21,12 +21,7 @@ namespace mROA.Implementation
throw new NotImplementedException();
}
public T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell)
{
throw new NotImplementedException();
}
public T? GetObject<T>(ComplexObjectIdentifier id)
public T GetObject<T>(ComplexObjectIdentifier id)
{
throw new NotImplementedException();
}
+2 -14
View File
@@ -21,19 +21,7 @@ namespace mROA.Implementation
throw new NotSupportedException();
}
public T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell)
{
if (_representationProducer == null)
throw new NullReferenceException("representation producer is not initialized");
if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException();
var representationModule = _representationProducer.Produce(sharedObjectShellShell.Identifier.OwnerId);
var remote = (T)Activator.CreateInstance(remoteType, sharedObjectShellShell.Identifier.ContextId,
representationModule)!;
return remote;
}
public T? GetObject<T>(ComplexObjectIdentifier id)
public T GetObject<T>(ComplexObjectIdentifier id)
{
if (_representationProducer == null)
throw new NullReferenceException("representation producer is not initialized");
@@ -41,7 +29,7 @@ namespace mROA.Implementation
if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException();
var representationModule =
_representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId());
var remote = (T)Activator.CreateInstance(remoteType, id,
var remote = (T)Activator.CreateInstance(remoteType, id.ContextId,
representationModule)!;
return remote;
}
+18
View File
@@ -10,6 +10,24 @@ namespace mROA.Implementation
{
public abstract class RemoteObjectBase : IDisposable
{
protected bool Equals(RemoteObjectBase other)
{
return _identifier.Equals(other._identifier);
}
public override bool Equals(object? obj)
{
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((RemoteObjectBase)obj);
}
public override int GetHashCode()
{
return HashCode.Combine(_identifier.GetHashCode(), _identifier.ContextId);
}
private readonly ComplexObjectIdentifier _identifier;
private readonly IRepresentationModule _representationModule;
+3 -1
View File
@@ -28,6 +28,7 @@ namespace mROA.Implementation
}
// ReSharper disable once UnusedMember.Global
// ReSharper disable once MemberCanBePrivate.Global
public SharedObjectShellShell(T value)
{
Value = value;
@@ -35,6 +36,7 @@ namespace mROA.Implementation
[JsonIgnore]
[SerializationIgnore]
// ReSharper disable once MemberCanBePrivate.Global
public T Value
{
get => _value;
@@ -74,7 +76,7 @@ namespace mROA.Implementation
set
{
_identifier = value;
Value = GetDefaultContextRepository().GetObjectByShell(this);
Value = GetDefaultContextRepository().GetObject<T>(Identifier);
}
}