Exemple #1
0
        public async Task Populate(SuperModel model, string property)
        {
            var modelType    = model.GetType();
            var propertyInfo = modelType.GetProperty(property);

            if (propertyInfo?.GetCustomAttributes(typeof(HasManyAttribute), true).FirstOrDefault() is HasManyAttribute hasMany)
            {
                await processHasMany(model, modelType, propertyInfo !, hasMany);
            }
            else if (propertyInfo?.GetCustomAttributes(typeof(BelongsToAttribute), true).FirstOrDefault() is BelongsToAttribute belongsTo)
            {
                await processBelongsTo(model, modelType, propertyInfo !, belongsTo);
            }
        }
Exemple #2
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);
        }