mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-05-06 11:32:59 +00:00
ResourceProxy
This commit is contained in:
parent
a2f4238933
commit
2caae61910
@ -11,7 +11,7 @@
|
||||
<PackageProjectUrl>http://www.esiur.com</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/esiur/esiur-dotnet/</RepositoryUrl>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
<Version>1.2.2</Version>
|
||||
<Version>1.2.3</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -23,8 +23,8 @@ namespace Esiur.Stores.MongoDB
|
||||
MongoClient client;
|
||||
IMongoDatabase database;
|
||||
IMongoCollection<BsonDocument> resourcesCollection;
|
||||
string collectionName;
|
||||
string dbName;
|
||||
//string collectionName;
|
||||
//string dbName;
|
||||
|
||||
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) }
|
||||
});
|
||||
|
||||
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) },
|
||||
{ "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<BsonDocument>(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<IResource>(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<BsonDocument>(collectionName);
|
||||
|
||||
// return new AsyncReply<bool>(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<bool> Open(Structure settings)
|
||||
{
|
||||
return new AsyncReply<bool>(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
<PackageLicenseUrl>https://github.com/esiur/esiur-dotnet/blob/master/LICENSE</PackageLicenseUrl>
|
||||
<PackageProjectUrl>http://www.esiur.com</PackageProjectUrl>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Version>1.2.5</Version>
|
||||
<Version>1.2.6</Version>
|
||||
<RepositoryUrl>https://github.com/esiur/esiur-dotnet</RepositoryUrl>
|
||||
<Authors>Ahmed Kh. Zamil</Authors>
|
||||
<AssemblyVersion>1.2.5.0</AssemblyVersion>
|
||||
|
@ -735,11 +735,6 @@ namespace Esiur.Net.IIP
|
||||
// nothing to do
|
||||
return true;
|
||||
}
|
||||
|
||||
public AsyncReply<bool> Open(Structure settings)
|
||||
{
|
||||
|
||||
return new AsyncReply<bool>(true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -35,7 +35,6 @@ namespace Esiur.Resource
|
||||
{
|
||||
public interface IStore:IResource
|
||||
{
|
||||
AsyncReply<bool> Open(Structure settings);
|
||||
AsyncReply<IResource> Get(string path);
|
||||
AsyncReply<IResource> Retrieve(uint iid);
|
||||
bool Put(IResource resource);
|
||||
|
@ -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
|
||||
|
||||
|
@ -236,7 +236,7 @@ namespace Esiur.Resource
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <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('/');
|
||||
@ -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
|
||||
/// <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="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);
|
||||
|
||||
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<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
|
||||
{
|
||||
var type = ResourceProxy.GetProxy<T>();
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -75,10 +75,6 @@ namespace Esiur.Stores
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public AsyncReply<bool> Open(Structure settings)
|
||||
{
|
||||
return new AsyncReply<bool>(true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -135,7 +135,7 @@ namespace Test
|
||||
}
|
||||
|
||||
[ResourceProperty]
|
||||
public int Level3
|
||||
public int Level3
|
||||
{
|
||||
get => 0;
|
||||
set => Instance?.Modified();
|
||||
|
Loading…
x
Reference in New Issue
Block a user