From 2caae619108c1d53330623ada93f168baa46f1d9 Mon Sep 17 00:00:00 2001 From: Ahmed Zamil Date: Wed, 24 Jul 2019 02:52:03 +0300 Subject: [PATCH] ResourceProxy --- .../Esiur.Stores.MongoDB.csproj | 2 +- Esiur.Stores.MongoDB/MongoDBStore.cs | 60 +++++++++---------- Esiur/Esiur.csproj | 2 +- Esiur/Net/IIP/DistributedConnection.cs | 7 +-- Esiur/Proxy/ResourceProxy.cs | 23 +++++-- Esiur/Resource/IStore.cs | 1 - Esiur/Resource/Template/ResourceTemplate.cs | 4 +- Esiur/Resource/Warehouse.cs | 17 +++--- Esiur/Stores/MemoryStore.cs | 6 +- Test/MyObject.cs | 2 +- 10 files changed, 63 insertions(+), 61 deletions(-) diff --git a/Esiur.Stores.MongoDB/Esiur.Stores.MongoDB.csproj b/Esiur.Stores.MongoDB/Esiur.Stores.MongoDB.csproj index 72a67d6..979d441 100644 --- a/Esiur.Stores.MongoDB/Esiur.Stores.MongoDB.csproj +++ b/Esiur.Stores.MongoDB/Esiur.Stores.MongoDB.csproj @@ -11,7 +11,7 @@ http://www.esiur.com https://github.com/esiur/esiur-dotnet/ True - 1.2.2 + 1.2.3 diff --git a/Esiur.Stores.MongoDB/MongoDBStore.cs b/Esiur.Stores.MongoDB/MongoDBStore.cs index 18fa2ea..451871e 100644 --- a/Esiur.Stores.MongoDB/MongoDBStore.cs +++ b/Esiur.Stores.MongoDB/MongoDBStore.cs @@ -23,8 +23,8 @@ namespace Esiur.Stores.MongoDB MongoClient client; IMongoDatabase database; IMongoCollection resourcesCollection; - string collectionName; - string dbName; + //string collectionName; + //string dbName; Dictionary resources = new Dictionary(); @@ -52,7 +52,7 @@ namespace Esiur.Stores.MongoDB {"property", propertyName}, {"age", BsonValue.Create(age) }, {"date", date}, {"value", Compose(value) } }); - var col = this.database.GetCollection(collectionName); + //var col = this.database.GetCollection(collectionName); @@ -61,27 +61,11 @@ namespace Esiur.Stores.MongoDB .Set("values." + propertyName, new BsonDocument { { "age", BsonValue.Create(age) }, { "modification", date }, { "value", Compose(value) } }); - - col.UpdateOne(filter, update); + resourcesCollection.UpdateOne(filter, update); return true; } - public MongoDBStore() : this("mongodb://localhost", "esiur", "resources") - { - - } - - public MongoDBStore(string connectionString, string database, string collection) - { - collectionName = collection; - dbName = database; - - client = new MongoClient(connectionString); - this.database = client.GetDatabase(database); - this.resourcesCollection = this.database.GetCollection(collection); - } - public bool Remove(IResource resource) { var objectId = resource.Instance.Attributes["objectId"].ToString(); @@ -103,6 +87,9 @@ namespace Esiur.Stores.MongoDB var type = Type.GetType(document["classname"].AsString); + if (type == null) + return new AsyncReply(null); + IResource resource = (IResource)Activator.CreateInstance(ResourceProxy.GetProxy(type)); resources.Add(document["_id"].AsObjectId.ToString(), resource); @@ -142,6 +129,10 @@ namespace Esiur.Stores.MongoDB }); } + // Apply store managers + foreach (var m in this.Instance.Managers) + resource.Instance.Managers.Add(m); + /* // load managers foreach(var m in managers) @@ -278,10 +269,12 @@ namespace Esiur.Stores.MongoDB return true; } + var type = ResourceProxy.GetBaseType(resource); + // insert the document var document = new BsonDocument { - { "classname", resource.GetType().FullName + "," + resource.GetType().GetTypeInfo().Assembly.GetName().Name }, + { "classname", type.FullName + "," + type.GetTypeInfo().Assembly.GetName().Name }, { "name", resource.Instance.Name }, }; @@ -313,12 +306,7 @@ namespace Esiur.Stores.MongoDB foreach (var pt in template.Properties) { -#if NETSTANDARD1_5 - var pi = resource.GetType().GetTypeInfo().GetProperty(pt.Name); -#else - var pi = resource.GetType().GetProperty(pt.Name); -#endif - var rt = pi.GetValue(resource, null); + var rt = pt.Info.GetValue(resource, null); values.Add(pt.Name, new BsonDocument { { "age", BsonValue.Create(resource.Instance.GetAge(pt.Index)) }, @@ -458,6 +446,16 @@ namespace Esiur.Stores.MongoDB if (trigger == ResourceTrigger.Initialize) { + + var collectionName = Instance.Attributes["Collection"] as string ?? "resources"; + var dbName = Instance.Attributes["Database"] as string ?? "esiur"; + client = new MongoClient(Instance.Attributes["Connection"] as string ?? "mongodb://localhost"); + database = client.GetDatabase(dbName); + + resourcesCollection = this.database.GetCollection(collectionName); + + // return new AsyncReply(true); + var filter = new BsonDocument(); var list = resourcesCollection.Find(filter).ToList(); @@ -514,12 +512,14 @@ namespace Esiur.Stores.MongoDB foreach (var pt in template.Properties) { + /* #if NETSTANDARD1_5 var pi = resource.GetType().GetTypeInfo().GetProperty(pt.Name); #else var pi = resource.GetType().GetProperty(pt.Name); #endif - var rt = pi.GetValue(resource, null); +*/ + var rt = pt.Info.GetValue(resource, null); values.Add(pt.Name, new BsonDocument { { "age", BsonValue.Create(resource.Instance.GetAge(pt.Index)) }, @@ -699,9 +699,5 @@ namespace Esiur.Stores.MongoDB } - public AsyncReply Open(Structure settings) - { - return new AsyncReply(true); - } } } diff --git a/Esiur/Esiur.csproj b/Esiur/Esiur.csproj index 489fd08..f0eb67c 100644 --- a/Esiur/Esiur.csproj +++ b/Esiur/Esiur.csproj @@ -7,7 +7,7 @@ https://github.com/esiur/esiur-dotnet/blob/master/LICENSE http://www.esiur.com true - 1.2.5 + 1.2.6 https://github.com/esiur/esiur-dotnet Ahmed Kh. Zamil 1.2.5.0 diff --git a/Esiur/Net/IIP/DistributedConnection.cs b/Esiur/Net/IIP/DistributedConnection.cs index d5982e1..027d743 100644 --- a/Esiur/Net/IIP/DistributedConnection.cs +++ b/Esiur/Net/IIP/DistributedConnection.cs @@ -735,11 +735,6 @@ namespace Esiur.Net.IIP // nothing to do return true; } - - public AsyncReply Open(Structure settings) - { - - return new AsyncReply(true); - } + } } diff --git a/Esiur/Proxy/ResourceProxy.cs b/Esiur/Proxy/ResourceProxy.cs index 9549f8f..45300f6 100644 --- a/Esiur/Proxy/ResourceProxy.cs +++ b/Esiur/Proxy/ResourceProxy.cs @@ -21,6 +21,23 @@ namespace Esiur.Proxy #endif + public static Type GetBaseType(object resource) + { + return GetBaseType(resource.GetType()); + } + + public static Type GetBaseType(Type type) + { + if (type.FullName.Contains("Esiur.Proxy.T")) +#if NETSTANDARD + return type.GetTypeInfo().BaseType; +#else + return type.BaseType; +#endif + else + return type; + } + public static Type GetProxy(Type type) { @@ -95,23 +112,19 @@ namespace Esiur.Proxy Label exitMethod = g.DefineLabel(); /* - IL_0000: ldarg.0 + IL_0000: ldarg.0 IL_0001: call instance class [Esiur]Esiur.Resource.Instance [Esiur]Esiur.Resource.Resource::get_Instance() // (no C# code) IL_0006: dup IL_0007: brtrue.s IL_000c - IL_0009: pop // } IL_000a: br.s IL_0017 - // (no C# code) IL_000c: ldstr "Level3" IL_0011: call instance void [Esiur]Esiur.Resource.Instance::Modified(string) IL_0016: nop - IL_0017: ret - */ // Add IL code for set method diff --git a/Esiur/Resource/IStore.cs b/Esiur/Resource/IStore.cs index 1b03ea1..96df8af 100644 --- a/Esiur/Resource/IStore.cs +++ b/Esiur/Resource/IStore.cs @@ -35,7 +35,6 @@ namespace Esiur.Resource { public interface IStore:IResource { - AsyncReply Open(Structure settings); AsyncReply Get(string path); AsyncReply Retrieve(uint iid); bool Put(IResource resource); diff --git a/Esiur/Resource/Template/ResourceTemplate.cs b/Esiur/Resource/Template/ResourceTemplate.cs index 06fcc6d..dceb89d 100644 --- a/Esiur/Resource/Template/ResourceTemplate.cs +++ b/Esiur/Resource/Template/ResourceTemplate.cs @@ -7,6 +7,7 @@ using Esiur.Misc; using Esiur.Data; using Esiur.Engine; using System.Security.Cryptography; +using Esiur.Proxy; namespace Esiur.Resource.Template { @@ -127,8 +128,7 @@ namespace Esiur.Resource.Template public ResourceTemplate(Type type) { - if (type.Namespace.Contains("Esiur.Proxy.T")) - type = type.GetTypeInfo().BaseType; + type = ResourceProxy.GetBaseType(type); // set guid diff --git a/Esiur/Resource/Warehouse.cs b/Esiur/Resource/Warehouse.cs index a95bb42..eb0b931 100644 --- a/Esiur/Resource/Warehouse.cs +++ b/Esiur/Resource/Warehouse.cs @@ -236,7 +236,7 @@ namespace Esiur.Resource /// /// /// Resource instance. - public static AsyncReply Get(string path, Structure settings = null, IResource parent = null, IPermissionsManager manager = null) + public static AsyncReply Get(string path, Structure attributes = null, IResource parent = null, IPermissionsManager manager = null) { var p = path.Split('/'); @@ -282,16 +282,16 @@ namespace Esiur.Resource var handler = protocols[url[0]]; var store = Activator.CreateInstance(handler.GetType()) as IStore; - Put(store, url[0] + "://" + hostname, null, parent, null, 0, manager); + Put(store, url[0] + "://" + hostname, null, parent, null, 0, manager, attributes); - store.Open(settings).Then(x => { + + store.Trigger(ResourceTrigger.Initialize).Then(x => { if (pathname.Length > 0 && pathname != "") store.Get(pathname).Then(r => { rt.Trigger(r); }).Error(e => rt.TriggerError(e)); else rt.Trigger(store); - }).Error(e => { rt.TriggerError(e); Warehouse.Remove(store); @@ -312,10 +312,13 @@ namespace Esiur.Resource /// Resource name. /// IStore that manages the resource. Can be null if the resource is a store. /// Parent resource. if not presented the store becomes the parent for the resource. - public static void Put(IResource resource, string name, IStore store = null, IResource parent = null, ResourceTemplate customTemplate = null, ulong age = 0, IPermissionsManager manager = null) + public static void Put(IResource resource, string name, IStore store = null, IResource parent = null, ResourceTemplate customTemplate = null, ulong age = 0, IPermissionsManager manager = null, Structure attributes = null) { resource.Instance = new Instance(resourceCounter++, name, resource, store, customTemplate, age); + if (attributes != null) + resource.Instance.SetAttributes(attributes); + if (manager != null) resource.Instance.Managers.Add(manager); @@ -347,12 +350,12 @@ namespace Esiur.Resource } - public static T New(string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null) + public static T New(string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null, Structure attributes = null) where T:IResource { var type = ResourceProxy.GetProxy(); var res = Activator.CreateInstance(type) as IResource; - Put(res, name, store, parent, null, 0, manager); + Put(res, name, store, parent, null, 0, manager, attributes); return (T)res; } diff --git a/Esiur/Stores/MemoryStore.cs b/Esiur/Stores/MemoryStore.cs index 691488d..30a2ea7 100644 --- a/Esiur/Stores/MemoryStore.cs +++ b/Esiur/Stores/MemoryStore.cs @@ -75,10 +75,6 @@ namespace Esiur.Stores { return true; } - - public AsyncReply Open(Structure settings) - { - return new AsyncReply(true); - } + } } diff --git a/Test/MyObject.cs b/Test/MyObject.cs index 0869562..1317bb7 100644 --- a/Test/MyObject.cs +++ b/Test/MyObject.cs @@ -135,7 +135,7 @@ namespace Test } [ResourceProperty] - public int Level3 + public int Level3 { get => 0; set => Instance?.Modified();