Client useful method in instance repository added

This commit is contained in:
2025-05-15 08:20:32 +03:00
parent 4638692305
commit 8e37eda0ae
10 changed files with 40 additions and 89 deletions
+3 -3
View File
@@ -46,8 +46,8 @@ class Program
var context = builder.GetModule<RemoteInstanceRepository>(); var context = builder.GetModule<RemoteInstanceRepository>();
var factory = var factory =
context.GetSingleObject(typeof(IPrinterFactory), context.GetSingletonObject<IPrinterFactory>(
builder.GetModule<IEndPointContext>()) as IPrinterFactory; builder.GetModule<IEndPointContext>());
using (var disposingPrinter = factory.Create("Test")) using (var disposingPrinter = factory.Create("Test"))
{ {
@@ -115,7 +115,7 @@ class Program
DemoCheck.Dispose = true; DemoCheck.Dispose = true;
var loadSingleton = context.GetSingleObject(typeof(ILoadTest), builder.GetModule<IEndPointContext>()) as ILoadTest; var loadSingleton = context.GetSingletonObject<ILoadTest>(builder.GetModule<IEndPointContext>());
var cts = new CancellationTokenSource(); var cts = new CancellationTokenSource();
+1 -1
View File
@@ -17,7 +17,7 @@
<RepositoryUrl>https://github.com/YaslePoy/mROA</RepositoryUrl> <RepositoryUrl>https://github.com/YaslePoy/mROA</RepositoryUrl>
<RepositoryType>git</RepositoryType> <RepositoryType>git</RepositoryType>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>2.0.1</Version> <Version>2.0.3</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
+11 -8
View File
@@ -411,9 +411,11 @@ namespace mROA.Codegen
var parametersDeclaration = string.Join(", ", var parametersDeclaration = string.Join(", ",
JoinWithComa(Enumerable.Range(0, parameters.Count).Select(i => "p" + i))); JoinWithComa(Enumerable.Range(0, parameters.Count).Select(i => "p" + i)));
int pi = 0;
var transferParameters = var transferParameters =
JoinWithComa(parameters.Where(i => !ParameterFilterForType(i)) JoinWithComa(parameters.Select(i => (i, pi++)).Where(i => !ParameterFilterForType(i.i))
.Select(i => "p" + parameters.IndexOf(i))); .Select(i => "p" + i.Item2));
var requestIndex = parameters.FindIndex(i => i.Name == "RequestContext"); var requestIndex = parameters.FindIndex(i => i.Name == "RequestContext");
if (requestIndex != -1) if (requestIndex != -1)
@@ -440,15 +442,16 @@ namespace mROA.Codegen
{ {
var level = "\t\t\t"; var level = "\t\t\t";
var parameters = ((INamedTypeSymbol)eventSymbol.Type).TypeArguments; int pi = 0;
var parsingParameters = parameters.RemoveAll(ParameterFilterForType).ToList(); var parameters = ((INamedTypeSymbol)eventSymbol.Type).TypeArguments.Select(i => (i, pi++)).ToImmutableArray();
var parsingParameters = parameters.RemoveAll(i => ParameterFilterForType(i.i)).ToList();
var parameterTypes = string.Join(", ", var parameterTypes = string.Join(", ",
$"{string.Join(", ", parsingParameters.Select(p => $"typeof({p.ToUnityString()})"))}"); $"{string.Join(", ", parsingParameters.Select(p => $"typeof({p.i.ToUnityString()})"))}");
var parametersInsertList = new List<string>(); var parametersInsertList = new List<string>();
foreach (var parameter in parameters) foreach (var parameter in parameters)
switch (parameter.Name) switch (parameter.i.Name)
{ {
case "CancellationToken": case "CancellationToken":
parametersInsertList.Add("(CancellationToken)special[1]"); parametersInsertList.Add("(CancellationToken)special[1]");
@@ -457,8 +460,8 @@ namespace mROA.Codegen
parametersInsertList.Add("special[0] as RequestContext"); parametersInsertList.Add("special[0] as RequestContext");
break; break;
default: default:
parametersInsertList.Add(Caster(parameter, parametersInsertList.Add(Caster(parameter.i,
$"parameters[{parameters.IndexOf(parameter)}]")); $"parameters[{parameter.Item2}]"));
break; break;
} }
+2 -1
View File
@@ -8,7 +8,8 @@ namespace mROA.Abstract
int ResisterObject<T>(object o, IEndPointContext context); int ResisterObject<T>(object o, IEndPointContext context);
void ClearObject(ComplexObjectIdentifier id, IEndPointContext context); void ClearObject(ComplexObjectIdentifier id, IEndPointContext context);
T GetObject<T>(ComplexObjectIdentifier id, IEndPointContext context); T GetObject<T>(ComplexObjectIdentifier id, IEndPointContext context);
object GetSingleObject(Type type, IEndPointContext context); T GetSingletonObject<T>(IEndPointContext context) where T : class, IShared;
object GetSingletonObject(Type type, IEndPointContext context);
int GetObjectIndex<T>(object o, IEndPointContext context); int GetObjectIndex<T>(object o, IEndPointContext context);
} }
} }
@@ -91,7 +91,7 @@ namespace mROA.Implementation.Backend
{ {
var context = command.ObjectId.ContextId != -1 var context = command.ObjectId.ContextId != -1
? instanceRepository.GetObject<object>(command.ObjectId, endPointContext) ? instanceRepository.GetObject<object>(command.ObjectId, endPointContext)
: instanceRepository.GetSingleObject(invoker.SuitableType, endPointContext); : instanceRepository.GetSingletonObject(invoker.SuitableType, endPointContext);
return context; return context;
} }
@@ -61,7 +61,13 @@ namespace mROA.Implementation.Backend
return (T)value; return (T)value;
} }
public object GetSingleObject(Type type, IEndPointContext context) 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()) ?? return _singletons.GetValueOrDefault(type.GetHashCode()) ??
throw new ArgumentException("Unregistered singleton type"); throw new ArgumentException("Unregistered singleton type");
@@ -38,10 +38,16 @@ namespace mROA.Implementation.Backend
return repository.GetObject<T>(id, context); return repository.GetObject<T>(id, context);
} }
public object GetSingleObject(Type type, IEndPointContext context) public T GetSingletonObject<T>(IEndPointContext context) where T : class, IShared
{ {
var repository = GetRepositoryByClientId(context.OwnerId); var repository = GetRepositoryByClientId(context.OwnerId);
return repository.GetSingleObject(type, context); return repository.GetSingletonObject<T>(context);
}
public object GetSingletonObject(Type type, IEndPointContext context)
{
var repository = GetRepositoryByClientId(context.OwnerId);
return repository.GetSingletonObject(type, context);
} }
public int GetObjectIndex<T>(object o, IEndPointContext context) public int GetObjectIndex<T>(object o, IEndPointContext context)
@@ -1,70 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using mROA.Abstract;
namespace mROA.Implementation
{
public class ComplexInstanceRepository : IInstanceRepository
{
private List<KeyValuePair<int, ExtensibleStorage<object>>> _storages = new();
public static object[] EventBinders = { };
private IRemoteObjectFactory? _remoteObjectFactory;
private IRepresentationModuleProducer? _representationModuleProducer;
public void Inject<T>(T dependency)
{
if (dependency is IRemoteObjectFactory remoteObjectFactory)
{
_remoteObjectFactory = remoteObjectFactory;
}
if (dependency is IRepresentationModuleProducer moduleProducer)
{
_representationModuleProducer = moduleProducer;
}
}
public int HostId { get; set; }
public int ResisterObject<T>(object o, IEndPointContext context)
{
var storageIndex = _storages.FindIndex(i => i.Key == context.OwnerId);
if (storageIndex == -1)
{
_storages.Add(
new KeyValuePair<int, ExtensibleStorage<object>>(context.OwnerId, new ExtensibleStorage<object>()));
storageIndex = _storages.Count - 1;
}
var storage = _storages[storageIndex].Value;
var placedIndex = storage.Place(o);
EventBinders.OfType<IEventBinder<T>>().FirstOrDefault()
?.BindEvents((T)o, context, _representationModuleProducer!, placedIndex);
return placedIndex;
}
public void ClearObject(ComplexObjectIdentifier id, IEndPointContext context)
{
_storages.Find(i => i.Key == id.OwnerId).Value.Free(id.ContextId);
}
public T GetObject<T>(ComplexObjectIdentifier id, IEndPointContext context)
{
throw new NotImplementedException();
}
public object GetSingleObject(Type type, IEndPointContext context)
{
throw new NotImplementedException();
}
public int GetObjectIndex<T>(object o, IEndPointContext context)
{
throw new NotImplementedException();
}
}
}
@@ -42,7 +42,12 @@ namespace mROA.Implementation
return remote; return remote;
} }
public object GetSingleObject(Type type, IEndPointContext context) public T GetSingletonObject<T>(IEndPointContext context) where T : class, IShared
{
return GetSingletonObject(typeof(T), context) as T;
}
public object GetSingletonObject(Type type, IEndPointContext context)
{ {
if (_representationProducer == null) if (_representationProducer == null)
throw new NullReferenceException("representation producer is not initialized"); throw new NullReferenceException("representation producer is not initialized");
+1 -1
View File
@@ -4,7 +4,7 @@
<TargetFramework>netstandard2.1</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Title>mROA</Title> <Title>mROA</Title>
<Version>2.0.0</Version> <Version>2.0.1</Version>
<Authors>YaslePoy</Authors> <Authors>YaslePoy</Authors>
<Description>Fast and easy RPC with contex</Description> <Description>Fast and easy RPC with contex</Description>
<RepositoryUrl>https://github.com/YaslePoy/mROA</RepositoryUrl> <RepositoryUrl>https://github.com/YaslePoy/mROA</RepositoryUrl>