Exemple #1
0
        public string Validate(IDictionaryAdapter dictionaryAdapter)
        {
            List <string> errors      = new List <string>();
            var           globalRules = AttributesUtil.GetTypeAttributes <ValidationRuleAttribute>(
                dictionaryAdapter.Meta.Type
                );

            foreach (var property in dictionaryAdapter.This.Properties.Values)
            {
                var propertyRules = AttributesUtil
                                    .GetAttributes <ValidationRuleAttribute>(property.Property)
                                    .Select(x => (IValidationRule)x);
                var propertyValue = dictionaryAdapter.GetProperty(property.PropertyName, true);
                ApplyValidationRules(
                    dictionaryAdapter,
                    propertyRules,
                    property,
                    propertyValue,
                    errors
                    );
                ApplyValidationRules(
                    dictionaryAdapter,
                    globalRules,
                    property,
                    propertyValue,
                    errors
                    );
            }

            return(string.Join(Environment.NewLine, errors.ToArray()));
        }
Exemple #2
0
        public string Validate(IDictionaryAdapter dictionaryAdapter, PropertyDescriptor property)
        {
            List <String> errors      = new List <string>();
            var           globalRules = AttributesUtil.GetTypeAttributes <IValidationRule>(dictionaryAdapter.Meta.Type);

            var propertyRules = AttributesUtil.GetAttributes <IValidationRule>(property.Property);
            var propertyValue = dictionaryAdapter.GetProperty(property.PropertyName, true);

            ApplyValidationRules(dictionaryAdapter, propertyRules, property, propertyValue, errors);
            ApplyValidationRules(dictionaryAdapter, globalRules, property, propertyValue, errors);

            return(String.Join(Environment.NewLine, errors.ToArray()));
        }
 private static IEnumerable <T> GetPropertyBehaviors <T>(MemberInfo member) where T : class
 {
     return(AttributesUtil.GetAttributes <T>(member));
 }
        private static Dictionary <String, PropertyDescriptor> GetPropertyDescriptors(Type type, PropertyDescriptor initializers, out object[] typeBehaviors)
        {
            var propertyMap        = new Dictionary <String, PropertyDescriptor>();
            var interfaceBehaviors = typeBehaviors = ExpandBehaviors(AttributesUtil.GetInterfaceAttributes(type)).ToArray();
            var defaultFetch       = typeBehaviors.OfType <FetchAttribute>().Select(b => (bool?)b.Fetch).FirstOrDefault().GetValueOrDefault();

#if DOTNET40
            initializers.AddBehaviors(typeBehaviors.OfType <IDictionaryMetaInitializer>())
            .AddBehaviors(typeBehaviors.OfType <IDictionaryInitializer>());
#else
            initializers.AddBehaviors(typeBehaviors.OfType <IDictionaryMetaInitializer>().Cast <IDictionaryBehavior>())
            .AddBehaviors(typeBehaviors.OfType <IDictionaryInitializer>    ().Cast <IDictionaryBehavior>());
#endif

            CollectProperties(type, property =>
            {
                var propertyBehaviors  = ExpandBehaviors(AttributesUtil.GetAttributes <object>(property)).ToArray();
                var propertyDescriptor = new PropertyDescriptor(property, propertyBehaviors)
                                         .AddBehaviors(propertyBehaviors.OfType <IDictionaryBehavior>())
                                         .AddBehaviors(interfaceBehaviors.OfType <IDictionaryBehavior>().Where(b => b is IDictionaryKeyBuilder == false))
#if DOTNET40
                                         .AddBehaviors(ExpandBehaviors(AttributesUtil
                                                                       .GetInterfaceAttributes(property.ReflectedType))
                                                       .OfType <IDictionaryKeyBuilder>());
#else
                                         .AddBehaviors(ExpandBehaviors(AttributesUtil
                                                                       .GetInterfaceAttributes(property.ReflectedType))
                                                       .OfType <IDictionaryKeyBuilder>()
                                                       .Cast <IDictionaryBehavior>());
#endif

                AddDefaultGetter(propertyDescriptor);

                var propertyFetch           = propertyBehaviors.OfType <FetchAttribute>().Select(b => (bool?)b.Fetch).FirstOrDefault();
                propertyDescriptor.IfExists = propertyBehaviors.OfType <IfExistsAttribute>().Any();
                propertyDescriptor.Fetch    = propertyFetch.GetValueOrDefault(defaultFetch);

                foreach (var descriptorInitializer in propertyDescriptor.Behaviors.OfType <IPropertyDescriptorInitializer>())
                {
                    descriptorInitializer.Initialize(propertyDescriptor, propertyBehaviors);
                }

#if DOTNET40
                initializers.AddBehaviors(propertyBehaviors.OfType <IDictionaryMetaInitializer>());
#else
                initializers.AddBehaviors(propertyBehaviors.OfType <IDictionaryMetaInitializer>().Cast <IDictionaryBehavior>());
#endif

                PropertyDescriptor existingDescriptor;
                if (propertyMap.TryGetValue(property.Name, out existingDescriptor))
                {
                    var existingProperty = existingDescriptor.Property;
                    if (existingProperty.PropertyType == property.PropertyType)
                    {
                        if (property.CanRead && property.CanWrite)
                        {
                            propertyMap[property.Name] = propertyDescriptor;
                        }
                        return;
                    }
                }

                propertyMap.Add(property.Name, propertyDescriptor);
            });
Exemple #5
0
        private static Dictionary <String, PropertyDescriptor> GetPropertyDescriptors(
            Type type, out IDictionaryInitializer[] typeInitializers,
            out IDictionaryMetaInitializer[] metaInitializers, out object[] typeBehaviors)
        {
            var propertyMap        = new Dictionary <String, PropertyDescriptor>();
            var interfaceBehaviors = typeBehaviors = ExpandBehaviors(AttributesUtil.GetTypeAttributes <object>(type)).ToArray();

            typeInitializers = typeBehaviors.OfType <IDictionaryInitializer>().Prioritize().ToArray();
            metaInitializers = typeBehaviors.OfType <IDictionaryMetaInitializer>().Prioritize().ToArray();
            var defaultFetch = typeBehaviors.OfType <FetchAttribute>().Select(b => b.Fetch).FirstOrDefault();

            CollectProperties(type, property =>
            {
                var propertyBehaviors  = ExpandBehaviors(AttributesUtil.GetAttributes <object>(property)).ToArray();
                var propertyDescriptor = new PropertyDescriptor(property, propertyBehaviors);

                var descriptorInitializers = propertyBehaviors.OfType <IPropertyDescriptorInitializer>();
                foreach (var descriptorInitializer in descriptorInitializers.OrderBy(b => b.ExecutionOrder))
                {
                    descriptorInitializer.Initialize(propertyDescriptor, propertyBehaviors);
                }

                propertyDescriptor.AddKeyBuilders(
                    propertyBehaviors.OfType <IDictionaryKeyBuilder>().Prioritize(
                        ExpandBehaviors(AttributesUtil.GetTypeAttributes <object>(property.ReflectedType)).OfType <IDictionaryKeyBuilder>())
                    );

                propertyDescriptor.AddGetters(
                    propertyBehaviors.OfType <IDictionaryPropertyGetter>().Prioritize(
                        interfaceBehaviors.OfType <IDictionaryPropertyGetter>())
                    );
                AddDefaultGetter(propertyDescriptor);

                propertyDescriptor.AddSetters(
                    propertyBehaviors.OfType <IDictionaryPropertySetter>().Prioritize(
                        interfaceBehaviors.OfType <IDictionaryPropertySetter>())
                    );

                bool?propertyFetch          = (from b in propertyBehaviors.OfType <FetchAttribute>() select b.Fetch).FirstOrDefault();
                propertyDescriptor.IfExists = propertyBehaviors.OfType <IfExistsAttribute>().Any();
                propertyDescriptor.Fetch    = propertyFetch.GetValueOrDefault(defaultFetch);

                PropertyDescriptor existingDescriptor;
                if (propertyMap.TryGetValue(property.Name, out existingDescriptor))
                {
                    var existingProperty = existingDescriptor.Property;
                    if (existingProperty.PropertyType == property.PropertyType)
                    {
                        if (property.CanRead && property.CanWrite)
                        {
                            propertyMap[property.Name] = propertyDescriptor;
                        }
                        return;
                    }
                }

                propertyMap.Add(property.Name, propertyDescriptor);
            });

            return(propertyMap);
        }