Esempio n. 1
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var objectType  = value.GetType();
            var dbModelType = objectType.GetGenericArguments()[0];

            var ravenId = value as RavenId;
            var fullId  = ravenId.Value;
            var shortId = IdHelper.ForDto(dbModelType, fullId);

            writer.WriteValue(shortId);
        }
Esempio n. 2
0
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }

            var modelName = bindingContext.ModelName;

            var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);

            bindingContext.ModelState.SetModelValue(modelName, valueProviderResult);

            var value = valueProviderResult.FirstValue;

            if (string.IsNullOrEmpty(value))
            {
                return(Task.CompletedTask);
            }

            if (!int.TryParse(value, out var id))
            {
                bindingContext.ModelState.TryAddModelError(
                    modelName, "Id must be an integer.");

                return(Task.CompletedTask);
            }

            var dbModelType = bindingContext.ModelType.GetGenericArguments()[0];

            var outputModelType = typeof(RavenId <>).MakeGenericType(dbModelType);

            var model = Activator.CreateInstance(outputModelType, IdHelper.ForModel(dbModelType, value));

            bindingContext.Result = ModelBindingResult.Success(model);
            return(Task.CompletedTask);
        }