2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 03:32:57 +00:00
This commit is contained in:
Ahmed Zamil 2023-07-01 15:32:23 +03:00
parent 7ce0037e40
commit b6f5a8d9dd
12 changed files with 73 additions and 27 deletions

View File

@ -33,6 +33,8 @@ using Esiur.Proxy;
using System.Linq; using System.Linq;
using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata;
using System.Reflection; using System.Reflection;
using Esiur.Security.Authority;
using System.Collections;
namespace Esiur.Stores.EntityCore; namespace Esiur.Stores.EntityCore;
public class EntityStore : IStore public class EntityStore : IStore
@ -63,30 +65,42 @@ public class EntityStore : IStore
var id = Convert.ChangeType(p[1], ti.PrimaryKey.PropertyType); var id = Convert.ChangeType(p[1], ti.PrimaryKey.PropertyType);
// Get db // Get db
var db = Getter(); using (var db = Getter())
var res = db.Find(ti.Type.ClrType, id);
if (res == null)
{ {
return new AsyncReply<IResource>(null);
//var rt = new AsyncReply<IResource>(); var res = db.Find(ti.Type.ClrType, id) as IResource;
//rt.TriggerError(new AsyncException(ErrorType.Management,
// (ushort)ExceptionCode.ResourceNotFound, "Resource not found."));
//return rt; if (res == null)
{
return new AsyncReply<IResource>(null);
//var rt = new AsyncReply<IResource>();
//rt.TriggerError(new AsyncException(ErrorType.Management,
// (ushort)ExceptionCode.ResourceNotFound, "Resource not found."));
//return rt;
}
// load navigation properties
var ent = db.Entry(res);
foreach (var rf in ent.References)
{
rf.Load();
}
foreach(var col in ent.Collections)
{
col.Load();
}
//var refs = ent.References.ToList();
db.Dispose();
return new AsyncReply<IResource>(res);
} }
// load navigation properties
var ent = db.Entry(res);
foreach (var rf in ent.References)
rf.Load();
foreach(var nav in ent.Navigations)
nav.Load();
//var refs = ent.References.ToList();
return new AsyncReply<IResource>(res as IResource);
} }
public AsyncReply<bool> Put(IResource resource) public AsyncReply<bool> Put(IResource resource)

View File

@ -22,7 +22,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.8" />
<PackageReference Include="System.Collections" Version="4.3.0" /> <PackageReference Include="System.Collections" Version="4.3.0" />
</ItemGroup> </ItemGroup>

View File

@ -47,6 +47,10 @@ public static class EsiurExtensions
//} //}
public static DbSet<object> SetByType(this DbContext context, Type t)
{
return (DbSet<object>)context.GetType().GetMethod("Set").MakeGenericMethod(t).Invoke(context, new object[0]);
}
public static T AddResource<T>(this DbSet<T> dbSet, T resource) where T : class, IResource public static T AddResource<T>(this DbSet<T> dbSet, T resource) where T : class, IResource
=> AddResourceAsync(dbSet, resource).Wait(); => AddResourceAsync(dbSet, resource).Wait();

View File

@ -61,8 +61,10 @@ public class EsiurProxyRewrite : IModelFinalizingConvention
var cache = options.Store.GetById(entityType.ClrType, id); var cache = options.Store.GetById(entityType.ClrType, id);
if (cache != null) if (cache != null && cache.Instance != null)
{
return cache; return cache;
}
if (Codec.ImplementsInterface(entityType.ClrType, typeof(IResource))) if (Codec.ImplementsInterface(entityType.ClrType, typeof(IResource)))
{ {

View File

@ -97,7 +97,7 @@ public class AutoList<T, ST> : IEnumerable<T>, ICollection, ICollection<T>
/// Create a new instance of AutoList /// Create a new instance of AutoList
/// </summary> /// </summary>
/// <param name="state">State object to be included when an event is raised.</param> /// <param name="state">State object to be included when an event is raised.</param>
public AutoList(ST state) public AutoList(ST state = default(ST))
{ {
State = state; State = state;
#if NETSTANDARD #if NETSTANDARD

View File

@ -203,14 +203,19 @@ public static class Codec
// Special // Special
[typeof(object[])] = DataSerializer.ListComposer,// DataSerializer.ListComposerFromArray, [typeof(object[])] = DataSerializer.ListComposer,// DataSerializer.ListComposerFromArray,
[typeof(List<object>)] = DataSerializer.ListComposer,// DataSerializer.ListComposerFromList, [typeof(List<object>)] = DataSerializer.ListComposer,// DataSerializer.ListComposerFromList,
[typeof(VarList<object>)] = DataSerializer.ListComposer,// DataSerializer.ListComposerFromList,
[typeof(IResource[])] = DataSerializer.ResourceListComposer,// (value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)), [typeof(IResource[])] = DataSerializer.ResourceListComposer,// (value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)),
[typeof(IResource?[])] = DataSerializer.ResourceListComposer,// (value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)), [typeof(IResource?[])] = DataSerializer.ResourceListComposer,// (value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)),
[typeof(List<IResource>)] = DataSerializer.ResourceListComposer, //(value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)), [typeof(List<IResource>)] = DataSerializer.ResourceListComposer, //(value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)),
[typeof(List<IResource?>)] = DataSerializer.ResourceListComposer, //(value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)), [typeof(List<IResource?>)] = DataSerializer.ResourceListComposer, //(value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)),
[typeof(VarList<IResource>)] = DataSerializer.ResourceListComposer, //(value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)),
[typeof(VarList<IResource?>)] = DataSerializer.ResourceListComposer, //(value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)),
[typeof(IRecord[])] = DataSerializer.RecordListComposer,// (value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)), [typeof(IRecord[])] = DataSerializer.RecordListComposer,// (value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)),
[typeof(IRecord?[])] = DataSerializer.RecordListComposer,// (value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)), [typeof(IRecord?[])] = DataSerializer.RecordListComposer,// (value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)),
[typeof(List<IRecord>)] = DataSerializer.RecordListComposer, //(value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)), [typeof(List<IRecord>)] = DataSerializer.RecordListComposer, //(value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)),
[typeof(List<IRecord?>)] = DataSerializer.RecordListComposer, //(value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)), [typeof(List<IRecord?>)] = DataSerializer.RecordListComposer, //(value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)),
[typeof(VarList<IRecord>)] = DataSerializer.RecordListComposer, //(value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)),
[typeof(VarList<IRecord?>)] = DataSerializer.RecordListComposer, //(value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)),
[typeof(Map<object, object>)] = DataSerializer.MapComposer, [typeof(Map<object, object>)] = DataSerializer.MapComposer,
[typeof(Map<object?, object>)] = DataSerializer.MapComposer, [typeof(Map<object?, object>)] = DataSerializer.MapComposer,
[typeof(Map<object, object?>)] = DataSerializer.MapComposer, [typeof(Map<object, object?>)] = DataSerializer.MapComposer,
@ -297,7 +302,7 @@ public static class Codec
else if (type.IsGenericType) else if (type.IsGenericType)
{ {
var genericType = type.GetGenericTypeDefinition(); var genericType = type.GetGenericTypeDefinition();
if (genericType == typeof(List<>)) if (genericType == typeof(List<>) || genericType == typeof(VarList<>))
{ {
var args = type.GetGenericArguments(); var args = type.GetGenericArguments();
//if (Composers.ContainsKey(args[0])) //if (Composers.ContainsKey(args[0]))

View File

@ -317,6 +317,11 @@ public static class DataSerializer
var resource = (IResource)value; var resource = (IResource)value;
var rt = new byte[4]; var rt = new byte[4];
if (resource.Instance == null || resource.Instance.IsDestroyed)
{
return (TransmissionTypeIdentifier.Null, new byte[0]);
}
if (Codec.IsLocalResource(resource, connection)) if (Codec.IsLocalResource(resource, connection))
{ {
@ -327,9 +332,13 @@ public static class DataSerializer
} }
else else
{ {
//rt.Append((value as IResource).Instance.Template.ClassId, (value as IResource).Instance.Id); //rt.Append((value as IResource).Instance.Template.ClassId, (value as IResource).Instance.Id);
connection.cache.Add(value as IResource, DateTime.UtcNow); connection.cache.Add(value as IResource, DateTime.UtcNow);
Console.WriteLine("Adding to cache " + resource.Instance.Id);
fixed (byte* ptr = rt) fixed (byte* ptr = rt)
*((uint*)ptr) = resource.Instance.Id; *((uint*)ptr) = resource.Instance.Id;

View File

@ -232,7 +232,7 @@ namespace Esiur.Data
else if (type.IsGenericType) else if (type.IsGenericType)
{ {
var genericType = type.GetGenericTypeDefinition(); var genericType = type.GetGenericTypeDefinition();
if (genericType == typeof(List<>)) if (genericType == typeof(List<>) || genericType == typeof(VarList<>))
{ {
var args = type.GetGenericArguments(); var args = type.GetGenericArguments();
if (args[0] == typeof(object)) if (args[0] == typeof(object))

View File

@ -8,7 +8,7 @@ using System.Text;
namespace Esiur.Data namespace Esiur.Data
{ {
public class VarList<T> : IEnumerable<T>, ICollection, ICollection<T> public class VarList<T> : IEnumerable<T>, ICollection, ICollection<T>
{ {
string propertyName; string propertyName;
IResource resource; IResource resource;
@ -21,6 +21,11 @@ namespace Esiur.Data
this.propertyName = propertyName; this.propertyName = propertyName;
} }
public VarList()
{
Console.WriteLine("New varlist");
}
public int Count => list.Count; public int Count => list.Count;
public bool IsReadOnly => false; public bool IsReadOnly => false;

View File

@ -598,6 +598,8 @@ partial class DistributedConnection
{ {
// reply failed // reply failed
//SendParams(0x80, r.Instance.Id, r.Instance.Age, r.Instance.Serialize(false, this)); //SendParams(0x80, r.Instance.Id, r.Instance.Age, r.Instance.Serialize(false, this));
Console.WriteLine("Not found " + resourceId);
SendError(ErrorType.Management, callback, (ushort)ExceptionCode.ResourceNotFound); SendError(ErrorType.Management, callback, (ushort)ExceptionCode.ResourceNotFound);
} }
}); });

View File

@ -704,6 +704,8 @@ public class Instance
get { return store; } get { return store; }
} }
public bool IsDestroyed { get; private set; }
/// <summary> /// <summary>
/// List of children. /// List of children.
/// </summary> /// </summary>
@ -1009,6 +1011,7 @@ public class Instance
private void Resource_OnDestroy(object sender) private void Resource_OnDestroy(object sender)
{ {
IsDestroyed = true;
Destroyed?.Invoke((IResource)sender); Destroyed?.Invoke((IResource)sender);
} }
} }

View File

@ -914,6 +914,8 @@ public static class Warehouse
resource.Destroy(); resource.Destroy();
resource.Instance = null;
return true; return true;
} }
} }