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

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 HostId { get; set; }
int ResisterObject<T>(object o, IEndPointContext context); int ResisterObject<T>(object o, IEndPointContext context);
void ClearObject(ComplexObjectIdentifier id); void ClearObject(ComplexObjectIdentifier id);
T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell); T GetObject<T>(ComplexObjectIdentifier id);
T? GetObject<T>(ComplexObjectIdentifier id);
object GetSingleObject(Type type); object GetSingleObject(Type type);
int GetObjectIndex<T>(object o, IEndPointContext context); int GetObjectIndex<T>(object o, IEndPointContext context);
} }
@@ -7,7 +7,7 @@ namespace mROA.Implementation.Backend
public class MultiClientContextRepository : IContextRepository, IContextRepositoryHub public class MultiClientContextRepository : IContextRepository, IContextRepositoryHub
{ {
private readonly Func<int, IContextRepository> _produceRepository; 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) public MultiClientContextRepository(Func<int, IContextRepository> produceRepository)
{ {
@@ -31,14 +31,8 @@ namespace mROA.Implementation.Backend
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
repository.ClearObject(id); repository.ClearObject(id);
} }
public T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell) public T GetObject<T>(ComplexObjectIdentifier id)
{
var repository = GetRepository(sharedObjectShellShell.Identifier.OwnerId);
return repository.GetObject<T>(sharedObjectShellShell.Identifier)!;
}
public T? GetObject<T>(ComplexObjectIdentifier id)
{ {
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.GetObject<T>(id); return repository.GetObject<T>(id);
@@ -12,6 +12,7 @@ namespace mROA.Implementation
{ {
ContextId = contextId; ContextId = contextId;
OwnerId = ownerId; OwnerId = ownerId;
} }
public static ComplexObjectIdentifier Null = new ComplexObjectIdentifier { ContextId = -2, OwnerId = -1 }; public static ComplexObjectIdentifier Null = new ComplexObjectIdentifier { ContextId = -2, OwnerId = -1 };
+1 -6
View File
@@ -21,12 +21,7 @@ namespace mROA.Implementation
throw new NotImplementedException(); throw new NotImplementedException();
} }
public T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell) public T GetObject<T>(ComplexObjectIdentifier id)
{
throw new NotImplementedException();
}
public T? GetObject<T>(ComplexObjectIdentifier id)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
+2 -14
View File
@@ -21,19 +21,7 @@ namespace mROA.Implementation
throw new NotSupportedException(); throw new NotSupportedException();
} }
public T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell) public T GetObject<T>(ComplexObjectIdentifier id)
{
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)
{ {
if (_representationProducer == null) if (_representationProducer == null)
throw new NullReferenceException("representation producer is not initialized"); 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(); if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException();
var representationModule = var representationModule =
_representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()); _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId());
var remote = (T)Activator.CreateInstance(remoteType, id, var remote = (T)Activator.CreateInstance(remoteType, id.ContextId,
representationModule)!; representationModule)!;
return remote; return remote;
} }
+18
View File
@@ -10,6 +10,24 @@ namespace mROA.Implementation
{ {
public abstract class RemoteObjectBase : IDisposable 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 ComplexObjectIdentifier _identifier;
private readonly IRepresentationModule _representationModule; private readonly IRepresentationModule _representationModule;
+3 -1
View File
@@ -28,6 +28,7 @@ namespace mROA.Implementation
} }
// ReSharper disable once UnusedMember.Global // ReSharper disable once UnusedMember.Global
// ReSharper disable once MemberCanBePrivate.Global
public SharedObjectShellShell(T value) public SharedObjectShellShell(T value)
{ {
Value = value; Value = value;
@@ -35,6 +36,7 @@ namespace mROA.Implementation
[JsonIgnore] [JsonIgnore]
[SerializationIgnore] [SerializationIgnore]
// ReSharper disable once MemberCanBePrivate.Global
public T Value public T Value
{ {
get => _value; get => _value;
@@ -74,7 +76,7 @@ namespace mROA.Implementation
set set
{ {
_identifier = value; _identifier = value;
Value = GetDefaultContextRepository().GetObjectByShell(this); Value = GetDefaultContextRepository().GetObject<T>(Identifier);
} }
} }