示例#1
0
        public static BindingConverterCollection AddStandart(this BindingConverterCollection bindingConverters)
        {
            return(bindingConverters
                   .Add(typeof(bool), new BoolBindingConverter())
                   .Add(typeof(int), new IntBindingConverter())
                   .Add(typeof(double), new DoubleBindingConverter())
                   .Add(typeof(string), new StringBindingConverter())

                   .Add(typeof(bool?), new NullBindingConverter <bool>(new BoolBindingConverter()))
                   .Add(typeof(int?), new NullBindingConverter <int>(new IntBindingConverter()))
                   .Add(typeof(double?), new NullBindingConverter <double>(new DoubleBindingConverter()))

                   .Add(typeof(IEnumerable <bool>), new EnumerableBindingConverter <bool>(",", new BoolBindingConverter()))
                   .Add(typeof(IEnumerable <int>), new EnumerableBindingConverter <int>(",", new IntBindingConverter()))
                   .Add(typeof(IEnumerable <double>), new EnumerableBindingConverter <double>(",", new DoubleBindingConverter()))
                   .Add(typeof(IEnumerable <string>), new EnumerableBindingConverter <string>(",", new StringBindingConverter()))

                   .Add(typeof(ICollection <bool>), new CollectionBindingConverter <bool>(",", new BoolBindingConverter()))
                   .Add(typeof(ICollection <int>), new CollectionBindingConverter <int>(",", new IntBindingConverter()))
                   .Add(typeof(ICollection <double>), new CollectionBindingConverter <double>(",", new DoubleBindingConverter()))
                   .Add(typeof(ICollection <string>), new CollectionBindingConverter <string>(",", new StringBindingConverter()))

                   .Add(typeof(List <bool>), new ListBindingConverter <bool>(",", new BoolBindingConverter()))
                   .Add(typeof(List <int>), new ListBindingConverter <int>(",", new IntBindingConverter()))
                   .Add(typeof(List <double>), new ListBindingConverter <double>(",", new DoubleBindingConverter()))
                   .Add(typeof(List <string>), new ListBindingConverter <string>(",", new StringBindingConverter())));
        }
        public static void Test()
        {
            AttributeMetadataReaderCollection metadataReaders = new AttributeMetadataReaderCollection()
                                                                .Add <CompareAttribute>(new CompareMetadataReader())
                                                                .Add <DataTypeAttribute>(new DataTypeMetadataReader())
                                                                .Add <DefaultValueAttribute>(new DefaultValueMetadataReader())
                                                                .Add <DescriptionAttribute>(new DescriptionMetadataReader())
                                                                .Add <DisplayAttribute>(new DisplayMetadataReader())
                                                                .Add <RequiredAttribute>(new RequiredMetadataReader())
                                                                .Add <StringLengthAttribute>(new StringLengthMetadataReader());

            FieldMetadataValidatorCollection fieldMetadataValidators = new FieldMetadataValidatorCollection()
                                                                       .Add(null, null, "Required", new RequiredMetadataValidator())
                                                                       .Add(null, null, "MatchProperty", new MatchPropertyMetadataValidator());

            ReflectionValueUpdaterCollection valueUpdaters = new ReflectionValueUpdaterCollection()
                                                             .Add <ICollection <int> >(new CollectionItemReflectionValueUpdater <int>());

            BindingConverterCollection bindingConverters = new BindingConverterCollection()
                                                           //.Add(new TypeFieldType(typeof(bool)), new BoolBindingConverter())
                                                           //.Add(new TypeFieldType(typeof(int)), new IntBindingConverter())
                                                           //.Add(new TypeFieldType(typeof(double)), new DoubleBindingConverter())
                                                           //.Add(new TypeFieldType(typeof(string)), new StringBindingConverter());
                                                           .AddStandart();

            TypeModelDefinitionCollection modelDefinitions = new TypeModelDefinitionCollection()
                                                             .AddReflectionSearchHandler(metadataReaders);

            IModelDefinition  modelDefinition = modelDefinitions.Get <RegisterUserModel>();
            RegisterUserModel model           = new RegisterUserModel();

            model.Username      = "******";
            model.Password      = "******";
            model.PasswordAgain = "y";
            IModelValueProvider valueProvider = new ReflectionModelValueProvider(model, valueUpdaters);

            IBindingModelValueStorage storage = new BindingDictionaryValueStorage()
                                                .Add("Username", "Pepa")
                                                .Add("Password", "XxYy")
                                                //.Add("PasswordAgain", "XxYy")
                                                //.Add("Age", "25")
                                                .Add("RoleIDs", "1,2,3,4,5,6");

            IModelValueGetter      bindingGetter = new BindingModelValueGetter(storage, bindingConverters, modelDefinition);
            CopyModelValueProvider copyProvider  = new CopyModelValueProvider(modelDefinition, true);

            Debug("Copy from dictionary", () => copyProvider.Update(valueProvider, bindingGetter));

            Console.WriteLine("RoleIDs: {0}", String.Join(", ", model.RoleIDs));

            IValidationHandler <ModelValidatorContext> modelValidator = new FieldMetadataModelValidator(fieldMetadataValidators);
            Task <IValidationResult> validationResult = Debug("Validate user", () => modelValidator.HandleAsync(new ModelValidatorContext(modelDefinition, valueProvider)));

            if (!validationResult.IsCompleted)
            {
                validationResult.RunSynchronously();
            }

            Console.WriteLine(validationResult.Result);

            validationResult = Debug("Validate user with binding", () => modelValidator.HandleAsync(new ModelValidatorContext(modelDefinition, bindingGetter)));
            if (!validationResult.IsCompleted)
            {
                validationResult.RunSynchronously();
            }

            Console.WriteLine(validationResult.Result);
        }