示例#1
0
    //public T CreateDB()
    //{

    //}

    //DbContext dbContext;
    //[Attribute]
    //public DbContext DbContext { get; set; }

    public string Link(IResource resource)
    {
        var type = ResourceProxy.GetBaseType(resource.GetType());

        var id = TypesByType[type].PrimaryKey.GetValue(resource);

        //DbContext.Model.FindEntityType(type).DisplayName();


        //            DbContext.Model.FindEntityType(type).DisplayName
        //var entityType = DbContext.Model.FindEntityType(type);
        //var id = entityType.FindPrimaryKey().Properties
        //            .FirstOrDefault()?.PropertyInfo
        //            .GetValue(resource);
        //        var id = Types

        if (id != null)
        {
            return(this.Instance.Name + "/" + type.Name + "/" + id.ToString());
        }
        else
        {
            return(this.Instance.Name + "/" + type.Name);
        }
    }
示例#2
0
    /// <summary>
    /// Get a template by type from the templates warehouse. If not in the warehouse, a new ResourceTemplate is created and added to the warehouse.
    /// </summary>
    /// <param name="type">.Net type.</param>
    /// <returns>Resource template.</returns>
    public static TypeTemplate GetTemplateByType(Type type)
    {
        TemplateType templateType = TemplateType.Unspecified;

        if (Codec.InheritsClass(type, typeof(DistributedResource)))
        {
            templateType = TemplateType.Wrapper;
        }
        if (Codec.ImplementsInterface(type, typeof(IResource)))
        {
            templateType = TemplateType.Resource;
        }
        else if (Codec.ImplementsInterface(type, typeof(IRecord)))
        {
            templateType = TemplateType.Record;
        }
        else if (type.IsEnum)
        {
            templateType = TemplateType.Enum;
        }
        else
        {
            return(null);
        }

        var baseType = ResourceProxy.GetBaseType(type);

        if (baseType == typeof(IResource) ||
            baseType == typeof(IRecord))
        {
            return(null);
        }

        var template = templates[templateType].Values.FirstOrDefault(x => x.DefinedType == type);

        // loaded ?
        if (template == null)
        {
            template = new TypeTemplate(baseType, true);
            TypeTemplate.GetDependencies(template);
        }

        return(template);
    }
示例#3
0
    public AsyncReply <bool> Put(IResource resource)
    {
        if (resource == this)
        {
            return(new AsyncReply <bool>(true));
        }

        var type = ResourceProxy.GetBaseType(resource);


        var eid = TypesByType[type].PrimaryKey.GetValue(resource);

        lock (DBLock)
        {
            if (DB[type].ContainsKey(eid))
            {
                DB[type].Remove(eid);
            }

            DB[type].Add(eid, new WeakReference(resource));
        }

        return(new AsyncReply <bool>(true));
    }
示例#4
0
    public void SaveResource(IResource resource)
    {
        var attrs = resource.Instance.GetAttributes();

        var parents  = new BsonArray();
        var children = new BsonArray();
        var template = resource.Instance.Template;

        //foreach (IResource c in resource.Instance.Children)
        //  children.Add(c.Instance.Link);

        var plist = resource.Instance.Variables["parents"] as string[];

        foreach (var link in plist)// Parents)
        {
            parents.Add(link);
        }


        var values = new BsonDocument();

        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 = pt.PropertyInfo.GetValue(resource, null);

            values.Add(pt.Name,
                       new BsonDocument {
                { "age", BsonValue.Create(resource.Instance.GetAge(pt.Index)) },
                { "modification", resource.Instance.GetModificationDate(pt.Index) },
                { "value", Compose(rt) }
            });
        }

        var attrsDoc = ComposeStructure(attrs);

        var type = ResourceProxy.GetBaseType(resource);

        var document = new BsonDocument
        {
            { "parents", parents },
            { "children", children },
            { "attributes", attrsDoc },
            { "classname", type.FullName + "," + type.GetTypeInfo().Assembly.GetName().Name },
            { "name", resource.Instance.Name },
            { "_id", new BsonObjectId(new ObjectId(resource.Instance.Variables["objectId"].ToString())) },
            { "values", values }
        };



        var filter = Builders <BsonDocument> .Filter.Eq("_id", document["_id"]);

        /*
         * var update = Builders<BsonDocument>.Update
         *  .Set("values", values);
         *
         * var update = Builders<BsonDocument>.Update.Set("values", values).Set("parents", parents;
         *          col.UpdateOne(filter, update);
         *
         */

        resourcesCollection.ReplaceOne(filter, document);
    }
示例#5
0
    public async AsyncReply <bool> Put(IResource resource)
    {
        try
        {
            if (resource == this)
            {
                return(true);
            }

            var attrs = resource.Instance.GetAttributes();

            foreach (var kv in resources)
            {
                if (kv.Value.Target == resource)
                {
                    resource.Instance.Variables.Add("objectId", kv.Key);
                    return(true);
                }
            }

            count++;

            Instance.Modified("Count");

            var type = ResourceProxy.GetBaseType(resource);

            // insert the document
            var document = new BsonDocument
            {
                { "classname", type.FullName + "," + type.GetTypeInfo().Assembly.GetName().Name },
                { "name", resource.Instance.Name },
            };

            resourcesCollection.InsertOne(document);
            resource.Instance.Variables["objectId"] = document["_id"].ToString();


            // now update the document
            // * insert first to get the object id, update values, attributes, children and parents after in case the same resource has a property references self

            var parents  = new BsonArray();
            var children = new BsonArray();

            var template = resource.Instance.Template;

            // setup attributes
            resource.Instance.Variables["children"] = new string[0];
            resource.Instance.Variables["parents"]  = new string[] { this.Instance.Link };

            // copy old children (in case we are moving a resource from a store to another.
            if (resource.Instance.Store != this)
            {
                var resourceChildren = await resource.Instance.Children <IResource>();

                if (resourceChildren != null)
                {
                    foreach (IResource c in resourceChildren)
                    {
                        children.Add(c.Instance.Link);
                    }
                }

                var resourceParents = await resource.Instance.Parents <IResource>();

                if (resourceParents == null)
                {
                    parents.Add(this.Instance.Link);
                }
                else
                {
                    foreach (IResource p in resourceParents)
                    {
                        parents.Add(p.Instance.Link);
                    }
                }
            }
            else
            {
                // just add self
                parents.Add(this.Instance.Link);
            }


            var attrsDoc = ComposeStructure(attrs);


            var values = new BsonDocument();

            foreach (var pt in template.Properties)
            {
                var rt = pt.PropertyInfo.GetValue(resource, null);

                values.Add(pt.Name,
                           new BsonDocument {
                    { "age", BsonValue.Create(resource.Instance.GetAge(pt.Index)) },
                    { "modification", resource.Instance.GetModificationDate(pt.Index) },
                    { "value", Compose(rt) }
                });
            }


            var filter = Builders <BsonDocument> .Filter.Eq("_id", document["_id"]);

            var update = Builders <BsonDocument> .Update
                         .Set("values", values).Set("parents", parents).Set("children", children).Set("attributes", attrsDoc);

            resourcesCollection.UpdateOne(filter, update);


            resources.Add(document["_id"].AsObjectId.ToString(), new WeakReference(resource));

            //resource.Instance.Variables["objectId"] = document["_id"].ToString();

            ResourceAdded?.Invoke(resource);

            return(true);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            return(false);
        }
    }