2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-07 12:02:59 +00:00

ResourceProxy

This commit is contained in:
Ahmed Zamil 2019-07-24 02:52:03 +03:00
parent a2f4238933
commit 2caae61910
10 changed files with 63 additions and 61 deletions

View File

@ -11,7 +11,7 @@
<PackageProjectUrl>http://www.esiur.com</PackageProjectUrl> <PackageProjectUrl>http://www.esiur.com</PackageProjectUrl>
<RepositoryUrl>https://github.com/esiur/esiur-dotnet/</RepositoryUrl> <RepositoryUrl>https://github.com/esiur/esiur-dotnet/</RepositoryUrl>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.2.2</Version> <Version>1.2.3</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -23,8 +23,8 @@ namespace Esiur.Stores.MongoDB
MongoClient client; MongoClient client;
IMongoDatabase database; IMongoDatabase database;
IMongoCollection<BsonDocument> resourcesCollection; IMongoCollection<BsonDocument> resourcesCollection;
string collectionName; //string collectionName;
string dbName; //string dbName;
Dictionary<string, IResource> resources = new Dictionary<string, IResource>(); Dictionary<string, IResource> resources = new Dictionary<string, IResource>();
@ -52,7 +52,7 @@ namespace Esiur.Stores.MongoDB
{"property", propertyName}, {"age", BsonValue.Create(age) }, {"date", date}, {"value", Compose(value) } {"property", propertyName}, {"age", BsonValue.Create(age) }, {"date", date}, {"value", Compose(value) }
}); });
var col = this.database.GetCollection<BsonDocument>(collectionName); //var col = this.database.GetCollection<BsonDocument>(collectionName);
@ -61,27 +61,11 @@ namespace Esiur.Stores.MongoDB
.Set("values." + propertyName, new BsonDocument { { "age", BsonValue.Create(age) }, .Set("values." + propertyName, new BsonDocument { { "age", BsonValue.Create(age) },
{ "modification", date }, { "modification", date },
{ "value", Compose(value) } }); { "value", Compose(value) } });
resourcesCollection.UpdateOne(filter, update);
col.UpdateOne(filter, update);
return true; 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<BsonDocument>(collection);
}
public bool Remove(IResource resource) public bool Remove(IResource resource)
{ {
var objectId = resource.Instance.Attributes["objectId"].ToString(); var objectId = resource.Instance.Attributes["objectId"].ToString();
@ -103,6 +87,9 @@ namespace Esiur.Stores.MongoDB
var type = Type.GetType(document["classname"].AsString); var type = Type.GetType(document["classname"].AsString);
if (type == null)
return new AsyncReply<IResource>(null);
IResource resource = (IResource)Activator.CreateInstance(ResourceProxy.GetProxy(type)); IResource resource = (IResource)Activator.CreateInstance(ResourceProxy.GetProxy(type));
resources.Add(document["_id"].AsObjectId.ToString(), resource); 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 // load managers
foreach(var m in managers) foreach(var m in managers)
@ -278,10 +269,12 @@ namespace Esiur.Stores.MongoDB
return true; return true;
} }
var type = ResourceProxy.GetBaseType(resource);
// insert the document // insert the document
var document = new BsonDocument 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 }, { "name", resource.Instance.Name },
}; };
@ -313,12 +306,7 @@ namespace Esiur.Stores.MongoDB
foreach (var pt in template.Properties) foreach (var pt in template.Properties)
{ {
#if NETSTANDARD1_5 var rt = pt.Info.GetValue(resource, null);
var pi = resource.GetType().GetTypeInfo().GetProperty(pt.Name);
#else
var pi = resource.GetType().GetProperty(pt.Name);
#endif
var rt = pi.GetValue(resource, null);
values.Add(pt.Name, values.Add(pt.Name,
new BsonDocument { { "age", BsonValue.Create(resource.Instance.GetAge(pt.Index)) }, new BsonDocument { { "age", BsonValue.Create(resource.Instance.GetAge(pt.Index)) },
@ -458,6 +446,16 @@ namespace Esiur.Stores.MongoDB
if (trigger == ResourceTrigger.Initialize) 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<BsonDocument>(collectionName);
// return new AsyncReply<bool>(true);
var filter = new BsonDocument(); var filter = new BsonDocument();
var list = resourcesCollection.Find(filter).ToList(); var list = resourcesCollection.Find(filter).ToList();
@ -514,12 +512,14 @@ namespace Esiur.Stores.MongoDB
foreach (var pt in template.Properties) foreach (var pt in template.Properties)
{ {
/*
#if NETSTANDARD1_5 #if NETSTANDARD1_5
var pi = resource.GetType().GetTypeInfo().GetProperty(pt.Name); var pi = resource.GetType().GetTypeInfo().GetProperty(pt.Name);
#else #else
var pi = resource.GetType().GetProperty(pt.Name); var pi = resource.GetType().GetProperty(pt.Name);
#endif #endif
var rt = pi.GetValue(resource, null); */
var rt = pt.Info.GetValue(resource, null);
values.Add(pt.Name, values.Add(pt.Name,
new BsonDocument { { "age", BsonValue.Create(resource.Instance.GetAge(pt.Index)) }, new BsonDocument { { "age", BsonValue.Create(resource.Instance.GetAge(pt.Index)) },
@ -699,9 +699,5 @@ namespace Esiur.Stores.MongoDB
} }
public AsyncReply<bool> Open(Structure settings)
{
return new AsyncReply<bool>(true);
}
} }
} }

View File

@ -7,7 +7,7 @@
<PackageLicenseUrl>https://github.com/esiur/esiur-dotnet/blob/master/LICENSE</PackageLicenseUrl> <PackageLicenseUrl>https://github.com/esiur/esiur-dotnet/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>http://www.esiur.com</PackageProjectUrl> <PackageProjectUrl>http://www.esiur.com</PackageProjectUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.2.5</Version> <Version>1.2.6</Version>
<RepositoryUrl>https://github.com/esiur/esiur-dotnet</RepositoryUrl> <RepositoryUrl>https://github.com/esiur/esiur-dotnet</RepositoryUrl>
<Authors>Ahmed Kh. Zamil</Authors> <Authors>Ahmed Kh. Zamil</Authors>
<AssemblyVersion>1.2.5.0</AssemblyVersion> <AssemblyVersion>1.2.5.0</AssemblyVersion>

View File

@ -735,11 +735,6 @@ namespace Esiur.Net.IIP
// nothing to do // nothing to do
return true; return true;
} }
public AsyncReply<bool> Open(Structure settings)
{
return new AsyncReply<bool>(true);
}
} }
} }

View File

@ -21,6 +21,23 @@ namespace Esiur.Proxy
#endif #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) public static Type GetProxy(Type type)
{ {
@ -95,23 +112,19 @@ namespace Esiur.Proxy
Label exitMethod = g.DefineLabel(); 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() IL_0001: call instance class [Esiur]Esiur.Resource.Instance [Esiur]Esiur.Resource.Resource::get_Instance()
// (no C# code) // (no C# code)
IL_0006: dup IL_0006: dup
IL_0007: brtrue.s IL_000c IL_0007: brtrue.s IL_000c
IL_0009: pop IL_0009: pop
// } // }
IL_000a: br.s IL_0017 IL_000a: br.s IL_0017
// (no C# code) // (no C# code)
IL_000c: ldstr "Level3" IL_000c: ldstr "Level3"
IL_0011: call instance void [Esiur]Esiur.Resource.Instance::Modified(string) IL_0011: call instance void [Esiur]Esiur.Resource.Instance::Modified(string)
IL_0016: nop IL_0016: nop
IL_0017: ret IL_0017: ret
*/ */
// Add IL code for set method // Add IL code for set method

View File

@ -35,7 +35,6 @@ namespace Esiur.Resource
{ {
public interface IStore:IResource public interface IStore:IResource
{ {
AsyncReply<bool> Open(Structure settings);
AsyncReply<IResource> Get(string path); AsyncReply<IResource> Get(string path);
AsyncReply<IResource> Retrieve(uint iid); AsyncReply<IResource> Retrieve(uint iid);
bool Put(IResource resource); bool Put(IResource resource);

View File

@ -7,6 +7,7 @@ using Esiur.Misc;
using Esiur.Data; using Esiur.Data;
using Esiur.Engine; using Esiur.Engine;
using System.Security.Cryptography; using System.Security.Cryptography;
using Esiur.Proxy;
namespace Esiur.Resource.Template namespace Esiur.Resource.Template
{ {
@ -127,8 +128,7 @@ namespace Esiur.Resource.Template
public ResourceTemplate(Type type) public ResourceTemplate(Type type)
{ {
if (type.Namespace.Contains("Esiur.Proxy.T")) type = ResourceProxy.GetBaseType(type);
type = type.GetTypeInfo().BaseType;
// set guid // set guid

View File

@ -236,7 +236,7 @@ namespace Esiur.Resource
/// </summary> /// </summary>
/// <param name="path"></param> /// <param name="path"></param>
/// <returns>Resource instance.</returns> /// <returns>Resource instance.</returns>
public static AsyncReply<IResource> Get(string path, Structure settings = null, IResource parent = null, IPermissionsManager manager = null) public static AsyncReply<IResource> Get(string path, Structure attributes = null, IResource parent = null, IPermissionsManager manager = null)
{ {
var p = path.Split('/'); var p = path.Split('/');
@ -282,16 +282,16 @@ namespace Esiur.Resource
var handler = protocols[url[0]]; var handler = protocols[url[0]];
var store = Activator.CreateInstance(handler.GetType()) as IStore; 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 != "") if (pathname.Length > 0 && pathname != "")
store.Get(pathname).Then(r => { store.Get(pathname).Then(r => {
rt.Trigger(r); rt.Trigger(r);
}).Error(e => rt.TriggerError(e)); }).Error(e => rt.TriggerError(e));
else else
rt.Trigger(store); rt.Trigger(store);
}).Error(e => { }).Error(e => {
rt.TriggerError(e); rt.TriggerError(e);
Warehouse.Remove(store); Warehouse.Remove(store);
@ -312,10 +312,13 @@ namespace Esiur.Resource
/// <param name="name">Resource name.</param> /// <param name="name">Resource name.</param>
/// <param name="store">IStore that manages the resource. Can be null if the resource is a store.</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> /// <param name="parent">Parent resource. if not presented the store becomes the parent for the resource.</param>
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); resource.Instance = new Instance(resourceCounter++, name, resource, store, customTemplate, age);
if (attributes != null)
resource.Instance.SetAttributes(attributes);
if (manager != null) if (manager != null)
resource.Instance.Managers.Add(manager); resource.Instance.Managers.Add(manager);
@ -347,12 +350,12 @@ namespace Esiur.Resource
} }
public static T New<T>(string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null) public static T New<T>(string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null, Structure attributes = null)
where T:IResource where T:IResource
{ {
var type = ResourceProxy.GetProxy<T>(); var type = ResourceProxy.GetProxy<T>();
var res = Activator.CreateInstance(type) as IResource; 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; return (T)res;
} }

View File

@ -75,10 +75,6 @@ namespace Esiur.Stores
{ {
return true; return true;
} }
public AsyncReply<bool> Open(Structure settings)
{
return new AsyncReply<bool>(true);
}
} }
} }

View File

@ -135,7 +135,7 @@ namespace Test
} }
[ResourceProperty] [ResourceProperty]
public int Level3 public int Level3
{ {
get => 0; get => 0;
set => Instance?.Modified(); set => Instance?.Modified();