/// <see cref="ICustomSerialization"/>
        public virtual EntryPrototype[] Prototypes(Type memberType, ICustomAttributeProvider attributeProvider)
        {
            // Check if it is a list, array or dictionary
            if (EntryConvert.IsCollection(memberType))
            {
                memberType = EntryConvert.ElementType(memberType);
            }

            List <EntryPrototype> prototypes = new List <EntryPrototype>();

            if (memberType == typeof(string))
            {
                prototypes.Add(new EntryPrototype(nameof(String), string.Empty));
            }
            else if (memberType.IsEnum)
            {
                foreach (Enum enumValue in Enum.GetValues(memberType))
                {
                    prototypes.Add(new EntryPrototype(enumValue.ToString("G"), enumValue));
                }
            }
            else
            {
                var prototype = Activator.CreateInstance(memberType);
                if (memberType.IsClass)
                {
                    ValueProviderExecutor.Execute(prototype, new ValueProviderExecutorSettings().AddDefaultValueProvider());
                }
                prototypes.Add(new EntryPrototype(memberType.Name, prototype));
            }

            return(prototypes.ToArray());
        }
Ejemplo n.º 2
0
        /// <see cref="ICustomSerialization"/>
        public virtual EntryPrototype[] Prototypes(Type memberType, ICustomAttributeProvider attributeProvider)
        {
            // Check if it is a list, array or dictionary
            if (EntryConvert.IsCollection(memberType))
            {
                memberType = EntryConvert.ElementType(memberType);
            }

            object prototype;

            if (memberType == typeof(string))
            {
                prototype = string.Empty;
            }
            else
            {
                prototype = Activator.CreateInstance(memberType);
                ValueProviderExecutor.Execute(prototype, new ValueProviderExecutorSettings().AddDefaultValueProvider());
            }

            return(new[]
            {
                new EntryPrototype(memberType.Name, prototype)
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Instantiate the factory and return a populated resource object
        /// </summary>
        public Resource Instantiate(IResourceTypeController typeController, IResourceGraph graph)
        {
            // This looks a little nasty, but it is the best way to prevent castle from
            // interfering with the resource relations
            Instance = typeController.Create(Type);

            // Resources need access to the creator
            Instance.Graph = graph;

            // Copy default properties
            Instance.Id          = Id;
            Instance.Name        = Name;
            Instance.Description = Description;

            // Copy extended data from json
            if (ExtensionData != null)
            {
                JsonConvert.PopulateObject(ExtensionData, Instance, JsonSettings.Minimal);
            }

            // Read everything else from defaults
            ValueProviderExecutor.Execute(Instance, new ValueProviderExecutorSettings()
                                          .AddFilter(new DataMemberAttributeValueProviderFilter(false))
                                          .AddDefaultValueProvider());

            return(Instance);
        }
Ejemplo n.º 4
0
        private TConfig StrategyConfig <TStrategy, TConfig, TBaseType>(Type targetType)
            where TConfig : class, IProductStrategyConfiguation
        {
            var tuple = CreateConfig <TStrategy, TConfig>(targetType);

            if (tuple == null)
            {
                return(null);
            }

            var config = tuple.Item2;

            config.TargetType = targetType.Name;
            config.PluginName = tuple.Item1.GetCustomAttribute <RegistrationAttribute>().Name;

            ValueProviderExecutor.Execute(config, new ValueProviderExecutorSettings().AddDefaultValueProvider());

            // Optionally try to configure property mappers
            var propertyMapperConfig = config as IPropertyMappedConfiguration;

            if (propertyMapperConfig != null)
            {
                var remainingColumns = typeof(IGenericColumns).GetProperties()
                                       .OrderBy(p => p.Name).ToList();
                // TODO: Use type wrapper
                var baseProperties = typeof(TBaseType).GetProperties();
                foreach (var property in targetType.GetProperties().Where(p => baseProperties.All(bp => bp.Name != p.Name))
                         .Where(p => !typeof(IProductPartLink).IsAssignableFrom(p.PropertyType) & !typeof(IEnumerable <IProductPartLink>).IsAssignableFrom(p.PropertyType))
                         .Where(p => !typeof(ProductInstance).IsAssignableFrom(p.PropertyType) & !typeof(IEnumerable <ProductInstance>).IsAssignableFrom(p.PropertyType)))
                {
                    var propertyTuple = CreateConfig <IPropertyMapper, PropertyMapperConfig>(property.PropertyType);
                    if (propertyTuple == null)
                    {
                        continue;
                    }

                    var strategy       = propertyTuple.Item1;
                    var propertyConfig = propertyTuple.Item2;
                    propertyConfig.PropertyName = property.Name;
                    propertyConfig.PluginName   = strategy.GetCustomAttribute <RegistrationAttribute>().Name;

                    var columnType = strategy.GetCustomAttribute <PropertyStrategyConfigurationAttribute>()?.ColumnType ?? typeof(string);
                    var column     = remainingColumns.FirstOrDefault(rc => rc.PropertyType == columnType);
                    if (column == null)
                    {
                        continue;
                    }

                    remainingColumns.Remove(column);
                    propertyConfig.Column = column.Name;

                    ValueProviderExecutor.Execute(propertyConfig, new ValueProviderExecutorSettings().AddDefaultValueProvider());
                    propertyMapperConfig.PropertyConfigs.Add(propertyConfig);
                }
            }

            return(config);
        }
Ejemplo n.º 5
0
        public void SetDefaultsOnlyPropertiesWithDefaultValueAttribute()
        {
            // Arrange
            var config = new TestConfig1();

            // Act
            ValueProviderExecutor.Execute(config, new ValueProviderExecutorSettings().AddDefaultValueProvider());

            //Assert
            Assert.AreEqual(25, config.DummyNumber2);
        }
Ejemplo n.º 6
0
        public void UseNoValueProviderLeadsToNoChanges()
        {
            // Arrange
            var config = new TestConfig1();

            // Act
            ValueProviderExecutor.Execute(config, new ValueProviderExecutorSettings());

            // Assert
            Assert.AreEqual(0, config.DummyNumber);
            Assert.AreEqual(1024, config.DummyNumberReadOnly);
            Assert.AreEqual(25, config.DummyNumber2);
            Assert.IsNull(config.DummyText);
        }
Ejemplo n.º 7
0
        public void ListOfPrimitivesTest()
        {
            // Arrange
            var config = new TestConfig4();

            // Act
            ValueProviderExecutor.Execute(config, new ValueProviderExecutorSettings().AddDefaultValueProvider());

            // Assert
            Assert.NotNull(config.Numbers);
            Assert.NotNull(config.Strings);
            Assert.NotNull(config.ArrayNumbers);
            Assert.IsNull(config.EnumerableNumbers);
        }
        public void SetDefaultsOnlyOnWritableProperties()
        {
            // Arrange
            var config = new TestConfig1();

            // Act
            ValueProviderExecutor.Execute(config, new ValueProviderExecutorSettings().AddDefaultValueProvider());

            // Assert
            Assert.AreEqual(DefaultValues.Number, config.DummyNumber);
            Assert.AreEqual(1024, config.DummyNumberReadOnly);
            Assert.AreEqual(25, config.DummyNumber2);
            Assert.AreEqual(DefaultValues.Text, config.DummyText);
        }
Ejemplo n.º 9
0
        public void SetDefaultsOnComplexConfig()
        {
            // Arrange
            var config = new TestConfig2();

            // Act
            ValueProviderExecutor.Execute(config, new ValueProviderExecutorSettings().AddDefaultValueProvider());

            // Assert
            Assert.IsNotNull(config.Config);
            Assert.AreEqual(DefaultValues.Number, config.DummyNumber);
            Assert.AreEqual(DefaultValues.Number, config.Config.DummyNumber);
            Assert.AreEqual(1024, config.Config.DummyNumberReadOnly);
            Assert.AreEqual(DefaultValues.Text, config.Config.DummyText);
        }
Ejemplo n.º 10
0
        public void AdditionalFilterTest()
        {
            // Arrange
            var config = new TestConfig2();

            // Act
            ValueProviderExecutor.Execute(config,
                                          new ValueProviderExecutorSettings().AddDefaultValueProvider()
                                          .AddFilter(new NoStringValueProviderFilter()));

            // Assert
            Assert.IsNotNull(config.Config);
            Assert.AreEqual(DefaultValues.Number, config.DummyNumber);
            Assert.AreEqual(DefaultValues.Number, config.Config.DummyNumber);
            Assert.AreEqual(1024, config.Config.DummyNumberReadOnly);
            Assert.IsNull(config.Config.DummyText);
        }
Ejemplo n.º 11
0
        public void EnumerableTest()
        {
            // Arrange
            var config = new TestConfig3 {
                Configs = new List <TestConfig1> {
                    new TestConfig1(), new TestConfig1(), new TestConfig1()
                }
            };

            // Act
            ValueProviderExecutor.Execute(config, new ValueProviderExecutorSettings().AddDefaultValueProvider());

            // Assert
            foreach (var subConfig in config.Configs)
            {
                Assert.AreEqual(DefaultValues.Number, subConfig.DummyNumber);
                Assert.AreEqual(1024, subConfig.DummyNumberReadOnly);
                Assert.AreEqual(25, subConfig.DummyNumber2);
                Assert.AreEqual(DefaultValues.Text, subConfig.DummyText);
            }

            Assert.AreEqual(DefaultValues.Number, config.DummyNumber);
        }
 /// <summary>
 /// Fill all available empty properties of the config.
 /// </summary>
 /// <param name="obj">The config for which the fill process should be done.</param>
 public void FillEmpty(object obj)
 {
     ValueProviderExecutor.Execute(obj, new ValueProviderExecutorSettings().AddProviders(ValueProviders));
 }
Ejemplo n.º 13
0
 public void FillEmpty(object obj)
 {
     ValueProviderExecutor.Execute(obj, new ValueProviderExecutorSettings().AddDefaultValueProvider());
 }