2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 13:33:13 +00:00

EntityCore

This commit is contained in:
2020-02-15 11:21:28 +03:00
parent 3205499747
commit 7a21f6a928
27 changed files with 962 additions and 830 deletions

View File

@ -28,12 +28,13 @@ using System.Linq;
using System.Text;
using Esyur.Data;
using Esyur.Core;
using System.ComponentModel;
namespace Esyur.Resource
{
public delegate bool QueryFilter<T>(T value);
public interface IResource : IDestructible
public interface IResource : IDestructible///, INotifyPropertyChanged
{
AsyncReply<bool> Trigger(ResourceTrigger trigger);

View File

@ -30,6 +30,8 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Esyur.Security.Permissions;
using Esyur.Security.Authority;
namespace Esyur.Resource
{
@ -43,6 +45,13 @@ namespace Esyur.Resource
bool Modify(IResource resource, string propertyName, object value, ulong age, DateTime dateTime);
bool Remove(IResource resource);
//bool RemoveAttributes(IResource resource, string[] attributes = null);
//Structure GetAttributes(IResource resource, string[] attributes = null);
//bool SetAttributes(IResource resource, Structure attributes, bool clearAttributes = false);
AsyncReply<bool> AddChild(IResource parent, IResource child);
AsyncReply<bool> RemoveChild(IResource parent, IResource child);

View File

@ -20,22 +20,14 @@ namespace Esyur.Resource
{
string name;
//IQueryable<IResource> children;//
//AutoList<IResource, Instance> children;// = new AutoList<IResource, Instance>();
WeakReference<IResource> resource;
IStore store;
//AutoList<IResource, Instance> parents;// = new AutoList<IResource>();
//bool inherit;
ResourceTemplate template;
AutoList<IPermissionsManager, Instance> managers;// = new AutoList<IPermissionManager, Instance>();
AutoList<IPermissionsManager, Instance> managers;
public delegate void ResourceModifiedEvent(IResource resource, string propertyName, object newValue);
//public delegate void ResourceEventOccurredEvent(IResource resource, string eventName, string[] users, DistributedConnection[] connections, object[] args);
public delegate void ResourceEventOccurredEvent(IResource resource, object issuer, Session[] receivers, string eventName, object[] args);
public delegate void ResourceDestroyedEvent(IResource resource);
public event ResourceModifiedEvent ResourceModified;
@ -44,7 +36,7 @@ namespace Esyur.Resource
bool loading = false;
KeyList<string, object> attributes;
//KeyList<string, object> attributes;
List<ulong> ages = new List<ulong>();
List<DateTime> modificationDates = new List<DateTime>();
@ -53,17 +45,18 @@ namespace Esyur.Resource
uint id;
public KeyList<string, object> Variables { get; } = new KeyList<string, object>();
/// <summary>
/// Instance attributes are custom properties associated with the instance, a place to store information by IStore.
/// </summary>
public KeyList<string, object> Attributes
{
get
{
return attributes;
}
}
//public KeyList<string, object> Attributes
//{
// get
// {
// return attributes;
// }
//}
public override string ToString()
{
@ -72,6 +65,19 @@ namespace Esyur.Resource
public bool RemoveAttributes(string[] attributes = null)
{
return false;
/*
IResource res;
if (!resource.TryGetTarget(out res))
return false;
return store.RemoveAttributes(res, attributes);
*/
/*
if (attributes == null)
this.attributes.Clear();
else
@ -81,10 +87,31 @@ namespace Esyur.Resource
}
return true;
*/
}
public Structure GetAttributes(string[] attributes = null)
{
// @TODO
Structure rt = new Structure();
if (attributes != null)
{
for (var i = 0; i < attributes.Length; i++)
{
var at = template.GetAttributeTemplate(attributes[i]);
if (at != null)
{
}
}
}
return rt;
/*
var st = new Structure();
if (attributes == null)
@ -132,10 +159,31 @@ namespace Esyur.Resource
}
return st;
*/
}
public bool SetAttributes(Structure attributes, bool clearAttributes = false)
{
// @ TODO
IResource res;
if (resource.TryGetTarget(out res))
{
foreach (var kv in attributes)
{
var at = template.GetAttributeTemplate(kv.Key);
if (at != null)
if (at.Info.CanWrite)
at.Info.SetValue(res, kv.Value);
}
}
return true;
/*
try
{
@ -183,6 +231,7 @@ namespace Esyur.Resource
}
return true;
*/
}
/*
@ -758,12 +807,15 @@ namespace Esyur.Resource
IResource res;
if (this.resource.TryGetTarget(out res))
{
//return store.Applicable(res, session, action, member, inquirer);
foreach (IPermissionsManager manager in managers)
{
var r = manager.Applicable(res, session, action, member, inquirer);
if (r != Ruling.DontCare)
return r;
}
}
return Ruling.DontCare;
@ -790,7 +842,7 @@ namespace Esyur.Resource
this.name = name;
this.instanceAge = age;
this.attributes = new KeyList<string, object>(this);
//this.attributes = new KeyList<string, object>(this);
//children = new AutoList<IResource, Instance>(this);
//parents = new AutoList<IResource, Instance>(this);
managers = new AutoList<IPermissionsManager, Instance>(this);

View File

@ -0,0 +1,42 @@
/*
Copyright (c) 2020 Ahmed Kh. Zamil
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Esyur.Resource
{
[AttributeUsage(AttributeTargets.Property)]
public class ResourceAttribute : System.Attribute
{
public ResourceAttribute()
{
}
}
}

View File

@ -0,0 +1,26 @@
using Esyur.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Esyur.Resource.Template
{
public class AttributeTemplate : MemberTemplate
{
public PropertyInfo Info
{
get;
set;
}
public AttributeTemplate(ResourceTemplate template, byte index, string name)
: base(template, MemberType.Attribute, index, name)
{
}
}
}

View File

@ -14,6 +14,7 @@ namespace Esyur.Resource.Template
Function = 0,
Property = 1,
Event = 2,
Attribute = 3
}
public byte Index => index;

View File

@ -19,6 +19,7 @@ namespace Esyur.Resource.Template
List<FunctionTemplate> functions = new List<FunctionTemplate>();
List<EventTemplate> events = new List<EventTemplate>();
List<PropertyTemplate> properties = new List<PropertyTemplate>();
List<AttributeTemplate> attributes = new List<AttributeTemplate>();
int version;
//bool isReady;
@ -88,6 +89,14 @@ namespace Esyur.Resource.Template
return null;
}
public AttributeTemplate GetAttributeTemplate(string attributeName)
{
foreach (var i in attributes)
if (i.Name == attributeName)
return i;
return null;
}
public Guid ClassId
{
get { return classId; }
@ -156,21 +165,31 @@ namespace Esyur.Resource.Template
foreach (var pi in propsInfo)
{
var ps = (ResourceProperty[])pi.GetCustomAttributes(typeof(ResourceProperty), true);
if (ps.Length > 0)
var rp = pi.GetCustomAttribute<ResourceProperty>(true);
if (rp != null)
{
var pt = new PropertyTemplate(this, i++, pi.Name, ps[0].ReadExpansion, ps[0].WriteExpansion, ps[0].Storage);
var pt = new PropertyTemplate(this, i++, pi.Name, rp.ReadExpansion, rp.WriteExpansion, rp.Storage);
pt.Info = pi;
pt.Serilize = ps[0].Serialize;
pt.Serilize = rp.Serialize;
properties.Add(pt);
}
var ra = pi.GetCustomAttribute<ResourceAttribute>(true);
if (ra != null)
{
var at = new AttributeTemplate(this, i++, pi.Name);
at.Info = pi;
attributes.Add(at);
}
}
i = 0;
foreach (var ei in eventsInfo)
{
var es = (ResourceEvent[])ei.GetCustomAttributes(typeof(ResourceEvent), true);
var es = ei.GetCustomAttributes<ResourceEvent>(true).ToArray();
if (es.Length > 0)
{
var et = new EventTemplate(this, i++, ei.Name, es[0].Expansion);
@ -181,7 +200,7 @@ namespace Esyur.Resource.Template
i = 0;
foreach (MethodInfo mi in methodsInfo)
{
var fs = (ResourceFunction[])mi.GetCustomAttributes(typeof(ResourceFunction), true);
var fs = mi.GetCustomAttributes<ResourceFunction>(true).ToArray();
if (fs.Length > 0)
{
var ft = new FunctionTemplate(this, i++, mi.Name, mi.ReturnType == typeof(void), fs[0].Expansion);

View File

@ -501,10 +501,9 @@ namespace Esyur.Resource
}
public static T New<T>(string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null, Structure attributes = null, Structure arguments = null, Structure properties = null)
where T : IResource
public static IResource New(Type type, string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null, Structure attributes = null, Structure arguments = null, Structure properties = null)
{
var type = ResourceProxy.GetProxy<T>();
type = ResourceProxy.GetProxy(type);
/*
@ -544,7 +543,7 @@ namespace Esyur.Resource
{
foreach (var p in properties)
{
var pi = typeof(T).GetProperty(p.Key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
var pi = type.GetProperty(p.Key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
if (pi != null)
pi.SetValue(res, p.Value);
}
@ -553,7 +552,14 @@ namespace Esyur.Resource
if (store != null || parent != null || res is IStore)
Put(res, name, store, parent, null, 0, manager, attributes);
return (T)res;
return res;
}
public static T New<T>(string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null, Structure attributes = null, Structure arguments = null, Structure properties = null)
where T : IResource
{
return (T)New(typeof(T), name, store, parent, manager, attributes, arguments, properties);
}
/// <summary>