Exemplo n.º 1
0
 public static RequestOp CreateOp(object model, long timeMs)
 {
     return(new RequestOp(
                timeMs,
                model.GetType(),
                RequestVerb.Create,
                CascadeTypeUtils.GetCascadeId(model),
                model
                ));
 }
Exemplo n.º 2
0
        public async Task <OpResponse> Fetch(RequestOp requestOp)
        {
            if (requestOp.Type != typeof(Model))
            {
                throw new Exception("requestOp.Type != typeof(Model)");
            }
            switch (requestOp.Verb)
            {
            case RequestVerb.Get:
                var id = (IdType?)CascadeTypeUtils.ConvertTo(typeof(IdType), requestOp.Id);                           //  ((IdType)requestOp.Id)!;
                if (id == null)
                {
                    throw new Exception("Unable to get right value for Id");
                }

                if (models.ContainsKey(id))
                {
                    return(new OpResponse(
                               requestOp,
                               Cascade.NowMs,
                               connected: true,
                               exists: true,
                               result: models[id].Item1,
                               arrivedAtMs: models[id].Item2
                               ));
                }
                else
                {
                    return(new OpResponse(
                               requestOp,
                               Cascade.NowMs,
                               connected: true,
                               exists: false,
                               result: null,
                               arrivedAtMs: null
                               ));
                }
                break;

            case RequestVerb.Query:
                if (collections.ContainsKey(requestOp.Key !))
                {
                    return(new OpResponse(
                               requestOp,
                               Cascade.NowMs,
                               connected: true,
                               exists: true,
                               result: collections[requestOp.Key !].Item1,
Exemplo n.º 3
0
        public async Task TestCollections()
        {
            var path = System.IO.Path.GetTempFileName();
            var conn = new SQLite.SQLiteAsyncConnection(path);
            var db   = new TestDatabase(conn);
            await db.Reset();

            var sqliteThingCache = new SqliteClassCache <Parent, long>(db);
            await sqliteThingCache.Setup();

            var sqliteGadgetCache = new SqliteClassCache <Child, string>(db);
            await sqliteGadgetCache.Setup();

            var cache = new ModelCache(aClassCache: new Dictionary <Type, IModelClassCache>()
            {
                { typeof(Parent), sqliteThingCache },
                { typeof(Child), sqliteGadgetCache }
            });
            var cascade = new CascadeDataLayer(origin, new ICascadeCache[] { cache }, new CascadeConfig()
            {
                DefaultFreshnessSeconds = 1
            });

            var collection_key = "my_things";
            var ids            = ImmutableArray.Create <object>(1, 2, 3);
            var response       = await cache.Fetch(RequestOp.QueryOp <Parent>(collection_key, new JsonObject(), 0));

            Assert.AreEqual(false, response.Exists);
            Assert.AreEqual(null, response.Result);
            await cache.StoreCollection(typeof(Parent), collection_key, ids, 0);

            response = await cache.Fetch(RequestOp.QueryOp <Parent>(collection_key, null, 0));

            Assert.IsTrue(CascadeTypeUtils.IsEqualEnumerable(ids, response.ResultIds));

            response = await cache.Fetch(RequestOp.QueryOp <Parent>("not_my_key", null, 0));

            Assert.IsFalse(response.Exists);

            response = await cache.Fetch(RequestOp.QueryOp <Child>(collection_key, null, 0));

            Assert.IsFalse(response.Exists);
        }
Exemplo n.º 4
0
        //
        //
        // // special case for enums
        // if (targetType.IsEnum) {
        //  // we could be going from an int -> enum so specifically let
        //  // the Enum object take care of this conversion
        //  if (value != null) {
        //      value = Enum.ToObject(targetType, value);
        //  }
        // }
        // else {
        //  // returns an System.Object with the specified System.Type and whose value is
        //  // equivalent to the specified object.
        //  value = Convert.ChangeType(value, targetType);
        // }

        // set the value of the property
        //  propertyInfo.SetValue(target, value, null);
        // }



        private async Task processHasMany(SuperModel model, Type modelType, PropertyInfo propertyInfo, HasManyAttribute attribute)
        {
            var propertyType = CascadeTypeUtils.DeNullType(propertyInfo.PropertyType);
            var isEnumerable = (propertyType?.Implements <IEnumerable>() ?? false) && propertyType != typeof(string);
            var foreignType  = isEnumerable ? CascadeTypeUtils.InnerType(propertyType !) : null;

            foreignType = foreignType != null?CascadeTypeUtils.DeNullType(foreignType) : null;

            if (foreignType == null)
            {
                throw new ArgumentException("Unable to get foreign model type. Property should be of type ImmutableArray<ChildModel>");
            }

            // var typeLayers = GetTypeLayers(propertyType);
            // var nonNullableType = DeNullType(propertyType);
            //
            //  typeLayers.FirstOrDefault(t => t.Name != "Nullable`1");
            //
            // var foreignType = isEnumerable ? propertyType.GetGenericArguments().FirstOrDefault() : null;
            object modelId = CascadeTypeUtils.GetCascadeId(model);
            var    key     = $"HasMany__{foreignType.Name}__{attribute.ForeignIdProperty}__{modelId}";

            var requestOp = new RequestOp(
                NowMs,
                foreignType,
                RequestVerb.Query,
                null,
                null,
                0,
                new Dictionary <string, object>()
            {
                [attribute.ForeignIdProperty] = modelId
            },
                key
                );
            var opResponse = await ProcessRequest(requestOp);

            CascadeTypeUtils.SetModelCollectionProperty(model, propertyInfo, opResponse.Results);
            //propertyInfo.SetValue(model,opResponse.Results);
        }
Exemplo n.º 5
0
        private async Task processBelongsTo(object model, Type modelType, PropertyInfo propertyInfo, BelongsToAttribute attribute)
        {
            var foreignModelType = CascadeTypeUtils.DeNullType(propertyInfo.PropertyType);
            var idProperty       = modelType.GetProperty(attribute.IdProperty);
            var id = idProperty.GetValue(model);

            if (id == null)
            {
                return;
            }

            var requestOp = new RequestOp(
                NowMs,
                foreignModelType,
                RequestVerb.Get,
                id,
                freshnessSeconds: Config.DefaultFreshnessSeconds
                );
            var opResponse = await ProcessRequest(requestOp);

            SetModelProperty(model, propertyInfo, opResponse.Result);
        }
Exemplo n.º 6
0
        public async Task <OpResponse> Fetch(RequestOp requestOp)
        {
            if (requestOp.Type != typeof(Model))
            {
                throw new Exception("requestOp.Type != typeof(Model)");
            }
            switch (requestOp.Verb)
            {
            case RequestVerb.Get:
                var id = (IdType?)CascadeTypeUtils.ConvertTo(typeof(IdType), requestOp.Id);                         //  ((IdType)requestOp.Id)!;
                if (id == null)
                {
                    throw new Exception("Unable to get right value for Id");
                }

                if (await _database.Exists <Model>(id))
                {
                    var meta = await _database.Get <CascadeModelMeta>(CascadeModelMeta.GenerateId <Model>(id));

                    return(new OpResponse(
                               requestOp,
                               Cascade !.NowMs,
                               connected: true,
                               exists: true,
                               result: await _database.Get <Model>(id),
                               arrivedAtMs: meta?.arrived_at
                               ));
                }
                else
                {
                    return(new OpResponse(
                               requestOp,
                               Cascade !.NowMs,
                               connected: true,
                               exists: false,
                               result: null,
                               arrivedAtMs: null
                               ));
                }
                break;

            case RequestVerb.Query:
                var collection = await _database.Get <CascadeCollection>(CascadeCollection.GenerateId <Model>(requestOp.Key !));

                if (collection != null)
                {
                    return(new OpResponse(
                               requestOp,
                               Cascade !.NowMs,
                               connected: true,
                               exists: true,
                               result: collection.objectIds,
                               arrivedAtMs: collection.arrived_at
                               ));
                }
                else
                {
                    return(new OpResponse(
                               requestOp,
                               Cascade !.NowMs,
                               connected: true,
                               exists: false,
                               result: null,
                               arrivedAtMs: null
                               ));
                }
                break;

            default:
                throw new NotImplementedException($"Unsupported {requestOp.Verb}");
            }
        }