2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 19:42:58 +00:00

TemplateType.Wrapper Removed

This commit is contained in:
Esiur Project 2022-09-06 23:29:44 +03:00
parent 77f7d1d545
commit 522f23212d
7 changed files with 107 additions and 137 deletions

View File

@ -172,7 +172,7 @@ namespace Esiur.Data
(RepresentationTypeIdentifier.Resource) => typeof(IResource), (RepresentationTypeIdentifier.Resource) => typeof(IResource),
(RepresentationTypeIdentifier.Record) => typeof(IRecord), (RepresentationTypeIdentifier.Record) => typeof(IRecord),
(RepresentationTypeIdentifier.TypedRecord) => Warehouse.GetTemplateByClassId((Guid)GUID, TemplateType.Record)?.DefinedType, (RepresentationTypeIdentifier.TypedRecord) => Warehouse.GetTemplateByClassId((Guid)GUID, TemplateType.Record)?.DefinedType,
(RepresentationTypeIdentifier.TypedResource) => Warehouse.GetTemplateByClassId((Guid)GUID, TemplateType.Unspecified)?.DefinedType, (RepresentationTypeIdentifier.TypedResource) => Warehouse.GetTemplateByClassId((Guid)GUID, TemplateType.Resource)?.DefinedType,
(RepresentationTypeIdentifier.Enum) => Warehouse.GetTemplateByClassId((Guid)GUID, TemplateType.Enum)?.DefinedType, (RepresentationTypeIdentifier.Enum) => Warehouse.GetTemplateByClassId((Guid)GUID, TemplateType.Enum)?.DefinedType,
_ => null _ => null

View File

@ -122,7 +122,7 @@ public static class TemplateGenerator
string name; string name;
if (representationType.Identifier == RepresentationTypeIdentifier.TypedResource)// == DataType.Resource) if (representationType.Identifier == RepresentationTypeIdentifier.TypedResource)// == DataType.Resource)
name = templates.First(x => x.ClassId == representationType.GUID && (x.Type == TemplateType.Resource || x.Type == TemplateType.Wrapper)).ClassName; name = templates.First(x => x.ClassId == representationType.GUID && (x.Type == TemplateType.Resource)).ClassName;
else if (representationType.Identifier == RepresentationTypeIdentifier.TypedRecord) else if (representationType.Identifier == RepresentationTypeIdentifier.TypedRecord)
name = templates.First(x => x.ClassId == representationType.GUID && x.Type == TemplateType.Record).ClassName; name = templates.First(x => x.ClassId == representationType.GUID && x.Type == TemplateType.Record).ClassName;
else if (representationType.Identifier == RepresentationTypeIdentifier.Enum) else if (representationType.Identifier == RepresentationTypeIdentifier.Enum)

View File

@ -46,7 +46,7 @@ public class Instance
//KeyList<string, object> attributes; //KeyList<string, object> attributes;
List<ulong?> ages = new(); List<ulong?> ages = new();
List<DateTime?> modificationDates = new (); List<DateTime?> modificationDates = new();
private ulong instanceAge; private ulong instanceAge;
private DateTime instanceModificationDate; private DateTime instanceModificationDate;
@ -97,10 +97,10 @@ public class Instance
*/ */
} }
public Map<string,object> GetAttributes(string[] attributes = null) public Map<string, object> GetAttributes(string[] attributes = null)
{ {
// @TODO // @TODO
var rt = new Map<string,object>(); var rt = new Map<string, object>();
if (attributes != null) if (attributes != null)
{ {
@ -169,7 +169,7 @@ public class Instance
*/ */
} }
public bool SetAttributes(Map<string,object> attributes, bool clearAttributes = false) public bool SetAttributes(Map<string, object> attributes, bool clearAttributes = false)
{ {
// @ TODO // @ TODO
@ -305,7 +305,7 @@ public class Instance
{ {
modificationDates[index] = value; modificationDates[index] = value;
if (value > instanceModificationDate) if (value > instanceModificationDate)
instanceModificationDate = (DateTime) value; instanceModificationDate = (DateTime)value;
} }
} }
@ -866,13 +866,6 @@ public class Instance
/// </summary> /// </summary>
public AutoList<IPermissionsManager, Instance> Managers => managers; public AutoList<IPermissionsManager, Instance> Managers => managers;
public void CallMeTest(Instance ins, int? val) =>
ins.EmitResourceEventByIndex(201, val);
public void CallMeTest2(Instance instance, object issuer, Func<Session, bool> receivers, int? val) =>
instance.EmitCustomResourceEventByIndex(issuer, receivers, 201, val);
/// <summary> /// <summary>
/// Create new instance. /// Create new instance.
/// </summary> /// </summary>
@ -911,110 +904,77 @@ public class Instance
modificationDates.Add(DateTime.MinValue); modificationDates.Add(DateTime.MinValue);
} }
// connect events // connect events
Type t = ResourceProxy.GetBaseType(resource); if (!(resource is DistributedResource))
#if NETSTANDARD
var events = t.GetTypeInfo().GetEvents(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly);
#else
var events = t.GetEvents(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly);
#endif
var emitEventByIndexMethod = GetType().GetMethod("EmitResourceEventByIndex", BindingFlags.Instance | BindingFlags.NonPublic);
var emitCustomEventByIndexMethod = GetType().GetMethod("EmitCustomResourceEventByIndex", BindingFlags.Instance | BindingFlags.NonPublic);
foreach (var evt in template.Events)
{ {
if (evt.EventInfo == null) Type t = ResourceProxy.GetBaseType(resource);
continue;
var eventGenericType = evt.EventInfo.EventHandlerType.GetGenericTypeDefinition(); var events = t.GetTypeInfo().GetEvents(BindingFlags.Public | BindingFlags.Instance);
if (eventGenericType == typeof(ResourceEventHandler<>)) var emitEventByIndexMethod = GetType().GetMethod("EmitResourceEventByIndex", BindingFlags.Instance | BindingFlags.NonPublic);
var emitCustomEventByIndexMethod = GetType().GetMethod("EmitCustomResourceEventByIndex", BindingFlags.Instance | BindingFlags.NonPublic);
foreach (var evt in template.Events)
{ {
var dm = new DynamicMethod("_", null, if (evt.EventInfo == null)
new Type[] {typeof(Instance), evt.EventInfo.EventHandlerType.GenericTypeArguments[0] }, continue;
typeof(Instance).Module, true);
var eventGenericType = evt.EventInfo.EventHandlerType.GetGenericTypeDefinition();
if (eventGenericType == typeof(ResourceEventHandler<>))
{
var dm = new DynamicMethod("_", null,
new Type[] { typeof(Instance), evt.EventInfo.EventHandlerType.GenericTypeArguments[0] },
typeof(Instance).Module, true);
var il = dm.GetILGenerator(); var il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldc_I4, (int)evt.Index); il.Emit(OpCodes.Ldc_I4, (int)evt.Index);
il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Box, evt.EventInfo.EventHandlerType.GenericTypeArguments[0]); il.Emit(OpCodes.Box, evt.EventInfo.EventHandlerType.GenericTypeArguments[0]);
il.Emit(OpCodes.Callvirt, emitEventByIndexMethod); il.Emit(OpCodes.Callvirt, emitEventByIndexMethod);
il.Emit(OpCodes.Nop); il.Emit(OpCodes.Nop);
il.Emit(OpCodes.Ret); il.Emit(OpCodes.Ret);
var proxyDelegate= dm.CreateDelegate(evt.EventInfo.EventHandlerType, this); var proxyDelegate = dm.CreateDelegate(evt.EventInfo.EventHandlerType, this);
//ResourceEventHandler<object> proxyDelegate = new ResourceEventHandler<object>((args) => EmitResourceEvent(evt, args)); //ResourceEventHandler<object> proxyDelegate = new ResourceEventHandler<object>((args) => EmitResourceEvent(evt, args));
evt.EventInfo.AddEventHandler(resource, proxyDelegate); evt.EventInfo.AddEventHandler(resource, proxyDelegate);
} }
else if (eventGenericType == typeof(CustomResourceEventHandler<>)) else if (eventGenericType == typeof(CustomResourceEventHandler<>))
{ {
var dm = new DynamicMethod("_", null, var dm = new DynamicMethod("_", null,
new Type[] { typeof(Instance), typeof(object), typeof(Func<Session, bool>), new Type[] { typeof(Instance), typeof(object), typeof(Func<Session, bool>),
evt.EventInfo.EventHandlerType.GenericTypeArguments[0] }, evt.EventInfo.EventHandlerType.GenericTypeArguments[0] },
typeof(Instance).Module, true); typeof(Instance).Module, true);
var il = dm.GetILGenerator(); var il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_2); il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Ldc_I4, (int)evt.Index); il.Emit(OpCodes.Ldc_I4, (int)evt.Index);
il.Emit(OpCodes.Ldarg_3); il.Emit(OpCodes.Ldarg_3);
il.Emit(OpCodes.Box, evt.EventInfo.EventHandlerType.GenericTypeArguments[0]); il.Emit(OpCodes.Box, evt.EventInfo.EventHandlerType.GenericTypeArguments[0]);
il.Emit(OpCodes.Callvirt, emitCustomEventByIndexMethod); il.Emit(OpCodes.Callvirt, emitCustomEventByIndexMethod);
il.Emit(OpCodes.Nop); il.Emit(OpCodes.Nop);
il.Emit(OpCodes.Ret); il.Emit(OpCodes.Ret);
var proxyDelegate = dm.CreateDelegate(evt.EventInfo.EventHandlerType, this); var proxyDelegate = dm.CreateDelegate(evt.EventInfo.EventHandlerType, this);
//CustomResourceEventHandler<object> proxyDelegate = (issuer, receivers, args) => EmitCustomResourceEvent(issuer, receivers, evt, args); evt.EventInfo.AddEventHandler(resource, proxyDelegate);
evt.EventInfo.AddEventHandler(resource, proxyDelegate); }
}
/*
else if (evt.EventHandlerType == typeof(CustomUsersEventHanlder))
{
var ca = (ResourceEvent[])evt.GetCustomAttributes(typeof(ResourceEvent), true);
if (ca.Length == 0)
continue;
CustomUsersEventHanlder proxyDelegate = (users, args) => EmitResourceEvent(evt.Name, users, null, args);
evt.AddEventHandler(resource, proxyDelegate);
}
else if (evt.EventHandlerType == typeof(CustomConnectionsEventHanlder))
{
var ca = (ResourceEvent[])evt.GetCustomAttributes(typeof(ResourceEvent), true);
if (ca.Length == 0)
continue;
CustomConnectionsEventHanlder proxyDelegate = (connections, args) => EmitResourceEvent(evt.Name, null, connections, args);
evt.AddEventHandler(resource, proxyDelegate);
}
else if (evt.EventHandlerType == typeof(CustomReceiversEventHanlder))
{
var ca = (ResourceEvent[])evt.GetCustomAttributes(typeof(ResourceEvent), true);
if (ca.Length == 0)
continue;
CustomReceiversEventHanlder proxyDelegate = (users, connections, args) => EmitResourceEvent(evt.Name, users, connections, args);
evt.AddEventHandler(resource, proxyDelegate);
} }
*/
} }
} }

View File

@ -5,9 +5,7 @@ using System.Text;
namespace Esiur.Resource.Template; namespace Esiur.Resource.Template;
public enum TemplateType : byte public enum TemplateType : byte
{ {
Unspecified,
Resource, Resource,
Record, Record,
Wrapper,
Enum Enum
} }

View File

@ -350,7 +350,7 @@ public class TypeTemplate
}; };
getDependenciesFunc(template, list); getDependenciesFunc(template, list);
return list.ToArray(); return list.Distinct().ToArray();
} }
@ -394,12 +394,12 @@ public class TypeTemplate
} }
public bool IsWrapper { get; private set; }
public TypeTemplate(Type type, bool addToWarehouse = false) public TypeTemplate(Type type, bool addToWarehouse = false)
{ {
if (Codec.InheritsClass(type, typeof(DistributedResource)))
templateType = TemplateType.Wrapper; if (Codec.ImplementsInterface(type, typeof(IResource)))
else if (Codec.ImplementsInterface(type, typeof(IResource)))
templateType = TemplateType.Resource; templateType = TemplateType.Resource;
else if (Codec.ImplementsInterface(type, typeof(IRecord))) else if (Codec.ImplementsInterface(type, typeof(IRecord)))
templateType = TemplateType.Record; templateType = TemplateType.Record;
@ -408,6 +408,8 @@ public class TypeTemplate
else else
throw new Exception("Type must implement IResource, IRecord or inherit from DistributedResource."); throw new Exception("Type must implement IResource, IRecord or inherit from DistributedResource.");
IsWrapper = Codec.InheritsClass(type, typeof(DistributedResource));
//if (isRecord && isResource) //if (isRecord && isResource)
// throw new Exception("Type can't have both IResource and IRecord interfaces"); // throw new Exception("Type can't have both IResource and IRecord interfaces");
@ -457,8 +459,7 @@ public class TypeTemplate
} }
} }
if (templateType == TemplateType.Resource if (templateType == TemplateType.Resource)
|| templateType == TemplateType.Wrapper)
{ {
if (hierarchy.ContainsKey(MemberTypes.Method)) if (hierarchy.ContainsKey(MemberTypes.Method))
{ {

View File

@ -57,10 +57,10 @@ public static class Warehouse
static KeyList<TemplateType, KeyList<Guid, TypeTemplate>> templates static KeyList<TemplateType, KeyList<Guid, TypeTemplate>> templates
= new KeyList<TemplateType, KeyList<Guid, TypeTemplate>>() = new KeyList<TemplateType, KeyList<Guid, TypeTemplate>>()
{ {
[TemplateType.Unspecified] = new KeyList<Guid, TypeTemplate>(), //[TemplateType.Unspecified] = new KeyList<Guid, TypeTemplate>(),
[TemplateType.Resource] = new KeyList<Guid, TypeTemplate>(), [TemplateType.Resource] = new KeyList<Guid, TypeTemplate>(),
[TemplateType.Record] = new KeyList<Guid, TypeTemplate>(), [TemplateType.Record] = new KeyList<Guid, TypeTemplate>(),
[TemplateType.Wrapper] = new KeyList<Guid, TypeTemplate>(), //[TemplateType.Wrapper] = new KeyList<Guid, TypeTemplate>(),
[TemplateType.Enum] = new KeyList<Guid, TypeTemplate>(), [TemplateType.Enum] = new KeyList<Guid, TypeTemplate>(),
}; };
@ -595,7 +595,7 @@ public static class Warehouse
resource.Instance = new Instance(resourceCounter++, instanceName, resource, store, customTemplate, age); resource.Instance = new Instance(resourceCounter++, instanceName, resource, store, customTemplate, age);
if (attributes != null) if (attributes != null)
resource.Instance.SetAttributes(Map<string,object>.FromObject(attributes)); resource.Instance.SetAttributes(Map<string, object>.FromObject(attributes));
if (manager != null) if (manager != null)
resource.Instance.Managers.Add(manager); resource.Instance.Managers.Add(manager);
@ -691,7 +691,7 @@ public static class Warehouse
if (properties != null) if (properties != null)
{ {
var ps = Map<string,object>.FromObject(properties); var ps = Map<string, object>.FromObject(properties);
foreach (var p in ps) foreach (var p in ps)
{ {
@ -753,6 +753,9 @@ public static class Warehouse
/// <param name="template">Resource template.</param> /// <param name="template">Resource template.</param>
public static void PutTemplate(TypeTemplate template) public static void PutTemplate(TypeTemplate template)
{ {
if (templates[template.Type].ContainsKey(template.ClassId))
throw new Exception($"Template with same class Id already exists. {templates[template.Type][template.ClassId].ClassName} -> {template.ClassName}");
templates[template.Type][template.ClassId] = template; templates[template.Type][template.ClassId] = template;
} }
@ -765,10 +768,12 @@ public static class Warehouse
public static TypeTemplate GetTemplateByType(Type type) public static TypeTemplate GetTemplateByType(Type type)
{ {
TemplateType templateType = TemplateType.Unspecified; //TemplateType templateType = TemplateType.Unspecified;
if (Codec.InheritsClass(type, typeof(DistributedResource))) //if (Codec.InheritsClass(type, typeof(DistributedResource)))
templateType = TemplateType.Wrapper; // templateType = TemplateType.Wrapper;
TemplateType templateType;
if (Codec.ImplementsInterface(type, typeof(IResource))) if (Codec.ImplementsInterface(type, typeof(IResource)))
templateType = TemplateType.Resource; templateType = TemplateType.Resource;
else if (Codec.ImplementsInterface(type, typeof(IRecord))) else if (Codec.ImplementsInterface(type, typeof(IRecord)))
@ -801,32 +806,33 @@ public static class Warehouse
/// </summary> /// </summary>
/// <param name="classId">Class Id.</param> /// <param name="classId">Class Id.</param>
/// <returns>Resource template.</returns> /// <returns>Resource template.</returns>
public static TypeTemplate GetTemplateByClassId(Guid classId, TemplateType templateType = TemplateType.Unspecified) public static TypeTemplate GetTemplateByClassId(Guid classId, TemplateType? templateType = null)
{ {
if (templateType == TemplateType.Unspecified) if (templateType == null)
{ {
// look in resources // look into resources
var template = templates[TemplateType.Resource][classId]; var template = templates[TemplateType.Resource][classId];
if (template != null) if (template != null)
return template; return template;
// look in records // look into records
template = templates[TemplateType.Record][classId]; template = templates[TemplateType.Record][classId];
if (template != null) if (template != null)
return template; return template;
// look in enums // look into enums
template = templates[TemplateType.Enum][classId]; template = templates[TemplateType.Enum][classId];
if (template != null)
return template;
// look in wrappers
template = templates[TemplateType.Wrapper][classId];
return template; return template;
//if (template != null)
//// look in wrappers
//template = templates[TemplateType.Wrapper][classId];
//return template;
} }
else else
return templates[templateType][classId]; return templates[templateType.Value][classId];
} }
/// <summary> /// <summary>
@ -834,27 +840,32 @@ public static class Warehouse
/// </summary> /// </summary>
/// <param name="className">Class name.</param> /// <param name="className">Class name.</param>
/// <returns>Resource template.</returns> /// <returns>Resource template.</returns>
public static TypeTemplate GetTemplateByClassName(string className, TemplateType templateType = TemplateType.Unspecified) public static TypeTemplate GetTemplateByClassName(string className, TemplateType? templateType = null)
{ {
if (templateType == TemplateType.Unspecified) if (templateType == null)
{ {
// look in resources // look into resources
var template = templates[TemplateType.Resource].Values.FirstOrDefault(x => x.ClassName == className); var template = templates[TemplateType.Resource].Values.FirstOrDefault(x => x.ClassName == className);
if (template != null) if (template != null)
return template; return template;
// look in records // look into records
template = templates[TemplateType.Record].Values.FirstOrDefault(x => x.ClassName == className); template = templates[TemplateType.Record].Values.FirstOrDefault(x => x.ClassName == className);
if (template != null) if (template != null)
return template; return template;
// look in wrappers // look into enums
template = templates[TemplateType.Wrapper].Values.FirstOrDefault(x => x.ClassName == className); template = templates[TemplateType.Enum].Values.FirstOrDefault(x => x.ClassName == className);
//if (template != null)
return template; return template;
//// look in wrappers
//template = templates[TemplateType.Wrapper].Values.FirstOrDefault(x => x.ClassName == className);
//return template;
} }
else else
{ {
return templates[templateType].Values.FirstOrDefault(x => x.ClassName == className); return templates[templateType.Value].Values.FirstOrDefault(x => x.ClassName == className);
} }
} }

View File

@ -111,7 +111,7 @@ namespace Test
var temp = await con.Call("temp"); var temp = await con.Call("temp");
Console.WriteLine("Temp: " + temp.GetHashCode()); Console.WriteLine("Temp: " + temp.GetHashCode());
var template = await con.GetTemplateByClassName("Test.MyResource"); //var template = await con.GetTemplateByClassName("Test.MyResource");
TestObjectProps(local, remote); TestObjectProps(local, remote);