2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2026-06-13 14:38:43 +00:00
This commit is contained in:
2026-05-26 16:18:57 +03:00
parent e4a54ffbe8
commit a91fc7d262
29 changed files with 206 additions and 148 deletions
+1 -2
View File
@@ -35,8 +35,7 @@ public delegate bool QueryFilter<T>(T value);
public interface IResource : IDestructible
{
AsyncReply<bool> Handle(ResourceOperation trigger);
AsyncReply<bool> Initialize(ResourceContext resourceContext);
AsyncReply<bool> Handle(ResourceOperation operation, IResourceContext? context = null);
[NotMapped]
[JsonIgnore]
@@ -0,0 +1,15 @@
using Esiur.Data;
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Resource
{
public interface IResourceContext
{
public Map<string, object> Attributes { get; }
public Map<string, object> Properties { get; }
public ulong Age { get; }
}
}
+2 -2
View File
@@ -38,9 +38,9 @@ public class Resource : IResource
OnDestroy?.Invoke(this);
}
public virtual AsyncReply<bool> Trigger(ResourceOperation trigger)
public virtual AsyncReply<bool> Handle(ResourceOperation operation, IResourceContext context = null)
{
if (trigger == ResourceOperation.Initialize)
if (operation == ResourceOperation.Initialize)
return new AsyncReply<bool>(this.Create());
else
return new AsyncReply<bool>(true);
+6 -6
View File
@@ -8,13 +8,13 @@ using System.Text;
namespace Esiur.Resource
{
public class ResourceContext
public class ResourceContext:IResourceContext
{
public ulong Age { get; }
public Map<string, object> Attributes { get; }
public Map<string, object> Properties { get; }
public IPermissionsManager PermissionsManager { get; }
public ResourceContext(ulong age, Map<string, object> attributes, Map<string, object> properties, IPermissionsManager permissionsManager)
{
Age = age;
@@ -23,9 +23,9 @@ namespace Esiur.Resource
PermissionsManager = permissionsManager;
}
public virtual void Build()
{
// update the context based on the current state of the resource and its environment
}
//public virtual void Build()
//{
// // update the context based on the current state of the resource and its environment
//}
}
}
+6 -4
View File
@@ -35,10 +35,12 @@ public abstract class Store<T> : IStore where T : IResource
public abstract bool Record(IResource resource, string propertyName, object value, ulong? age, DateTime? dateTime);
public abstract AsyncReply<bool> Trigger(ResourceOperation trigger);
public abstract AsyncReply<bool> Handle(ResourceOperation operation, IResourceContext context = null);
//public async AsyncReply<T> New(string name = null, object attributes = null, object properties = null)
//{
+13 -13
View File
@@ -92,7 +92,7 @@ public class Warehouse
public event StoreEvent StoreConnected;
public event StoreEvent StoreDisconnected;
public delegate AsyncReply<IStore> ProtocolInstance(string name, ResourceContext resourceContext);
public delegate AsyncReply<IStore> ProtocolInstance(string name, IResourceContext resourceContext);
public KeyList<string, ProtocolInstance> Protocols { get; } = new KeyList<string, ProtocolInstance>();
@@ -226,7 +226,7 @@ public class Warehouse
//IResource r;
//if (rk.Value.TryGetTarget(out r))
//{
var rt = await r.Trigger(ResourceOperation.Initialize);
var rt = await r.Handle(ResourceOperation.Initialize);
//if (!rt)
// return false;
@@ -239,7 +239,7 @@ public class Warehouse
foreach (var r in resSnap)
{
var rt = await r.Trigger(ResourceOperation.SystemInitialized);
var rt = await r.Handle(ResourceOperation.SystemReady);
if (!rt)
{
Global.Log("Warehouse", LogType.Warning, $"Resource failed at SystemInitialized {r.Instance.Name} [{r.Instance.Definition.Name}]");
@@ -267,13 +267,13 @@ public class Warehouse
if (resource.TryGetTarget(out r))
{
if (!(r is IStore))
bag.Add(r.Trigger(ResourceOperation.Terminate));
bag.Add(r.Handle(ResourceOperation.Terminate));
}
}
foreach (var store in _stores)
bag.Add(store.Key.Trigger(ResourceOperation.Terminate));
bag.Add(store.Key.Handle(ResourceOperation.Terminate));
foreach (var resource in _resources.Values)
@@ -282,13 +282,13 @@ public class Warehouse
if (resource.TryGetTarget(out r))
{
if (!(r is IStore))
bag.Add(r.Trigger(ResourceOperation.SystemTerminated));
bag.Add(r.Handle(ResourceOperation.SystemTerminated));
}
}
foreach (var store in _stores)
bag.Add(store.Key.Trigger(ResourceOperation.SystemTerminated));
bag.Add(store.Key.Handle(ResourceOperation.SystemTerminated));
bag.Seal();
@@ -337,7 +337,7 @@ public class Warehouse
/// </summary>
/// <param name="path"></param>
/// <returns>Resource instance.</returns>
public async AsyncReply<T> Get<T>(string path, ResourceContext resourceContext = null)
public async AsyncReply<T> Get<T>(string path, IResourceContext resourceContext = null)
where T : IResource
{
@@ -385,7 +385,7 @@ public class Warehouse
/// <param name="resource">Resource instance.</param>
/// <param name="store">IStore that manages the resource. Can be null if the resource is a store.</param>
/// <param name="parent">Parent resource. if not presented the store becomes the parent for the resource.</param>
public async AsyncReply<T> Put<T>(string path, T resource, ResourceContext resourceContext = null) where T : IResource
public async AsyncReply<T> Put<T>(string path, T resource, IResourceContext resourceContext = null) where T : IResource
{
if (resource.Instance != null)
throw new Exception("Resource already initialized.");
@@ -448,9 +448,9 @@ public class Warehouse
if (_warehouseIsOpen)
{
await resource.Handle(ResourceOperation.Initialize);
await resource.Handle(ResourceOperation.Initialize, resourceContext);
if (resource is IStore)
await resource.Handle(ResourceOperation.Open);
await resource.Handle(ResourceOperation.Open, resourceContext);
}
if (resource is IStore)
@@ -551,13 +551,13 @@ public class Warehouse
return res;
}
public async AsyncReply<IResource> New(Type type, string path, ResourceContext resourceContext)
public async AsyncReply<IResource> New(Type type, string path, IResourceContext resourceContext)
{
var res = Create(type, resourceContext?.Properties);
return await Put(path, res, resourceContext);
}
public async AsyncReply<T> New<T>(string path, ResourceContext resourceContext = null)
public async AsyncReply<T> New<T>(string path, IResourceContext resourceContext = null)
where T : IResource
{
return (T)(await New(typeof(T), path, resourceContext));