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

@ -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,16 +904,14 @@ public class Instance
modificationDates.Add(DateTime.MinValue); modificationDates.Add(DateTime.MinValue);
} }
// connect events // connect events
if (!(resource is DistributedResource))
{
Type t = ResourceProxy.GetBaseType(resource); Type t = ResourceProxy.GetBaseType(resource);
#if NETSTANDARD var events = t.GetTypeInfo().GetEvents(BindingFlags.Public | BindingFlags.Instance);
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 emitEventByIndexMethod = GetType().GetMethod("EmitResourceEventByIndex", BindingFlags.Instance | BindingFlags.NonPublic);
var emitCustomEventByIndexMethod = GetType().GetMethod("EmitCustomResourceEventByIndex", BindingFlags.Instance | BindingFlags.NonPublic); var emitCustomEventByIndexMethod = GetType().GetMethod("EmitCustomResourceEventByIndex", BindingFlags.Instance | BindingFlags.NonPublic);
@ -980,41 +971,10 @@ public class Instance
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>(),
}; };
@ -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; return template;
//if (template != null)
// look in wrappers //// look in wrappers
template = templates[TemplateType.Wrapper][classId]; //template = templates[TemplateType.Wrapper][classId];
return template; //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);