Exemple #1
0
        private static TypeConfiguration CreateConfigurationWithPropertiesOnly(Type baseType)
        {
            var tc = new TypeConfiguration(baseType);

            foreach (var pi in baseType.GetTypeInfo().DeclaredProperties)
            {
                tc.Properties.Add(
                    new APropertyInfo(pi, TypeInfoResolver.ResolveTypeInfo(pi.PropertyType), null));
            }
            return(tc);
        }
        public void Known()
        {
            var model1TypeConfiguration = new TypeConfiguration(typeof(TestModel1));
            var list = new List <TypeConfiguration>
            {
                model1TypeConfiguration
            };
            var builder = new TypeConfigurationBuilder(list);

            var result = builder.Build(model1TypeConfiguration, typeof(TestModel1));

            result.Should().Be(model1TypeConfiguration);
        }
Exemple #3
0
        public void Remove_AddsAugmentToTypeConfiguration()
        {
            var tc = new TypeConfiguration <TestModel1>();

            tc.Remove("Foo");

            tc.Augments.First().Invoking(o =>
            {
                o.Name.Should().Be("Foo");
                o.Kind.Should().Be(AugmentKind.Remove);
                o.ValueFunc.Should().BeNull();
            });
        }
Exemple #4
0
        public void Add_AddsAugmentToTypeConfiguration()
        {
            var tc = new TypeConfiguration <TestModel1>();

            tc.Add("Foo", (x, _) => $"({x.Id})");

            tc.Augments.First().Invoking(o =>
            {
                o.Name.Should().Be("Foo");
                o.Kind.Should().Be(AugmentKind.Add);
                o.ValueFunc.Should().NotBeNull();
            });
        }
        public void TypeIsDifferentThanTypeConfigurationType_Throws()
        {
            var model1TypeConfiguration = new TypeConfiguration(typeof(TestModel1));
            var list = new List <TypeConfiguration>
            {
                model1TypeConfiguration
            };
            var builder = new TypeConfigurationBuilder(list);

            Assert.Throws <ArgumentException>(() =>
            {
                builder.Build(model1TypeConfiguration, typeof(TestModelA));
            });
        }
        public void IgnoresStaticReferences()
        {
            var modelTypeConfiguration = new TypeConfiguration <TestModelWithStaticReference>();

            modelTypeConfiguration.Add("Some", (x, state) => "some");
            var list = new List <TypeConfiguration>
            {
                modelTypeConfiguration
            };
            var builder = new TypeConfigurationBuilder(list);
            var result  = builder.Build(modelTypeConfiguration, typeof(TestModelWithStaticReference));

            modelTypeConfiguration.Properties.Should().HaveCount(1);
        }
Exemple #7
0
        public void AddIf_False()
        {
            var tc = new TypeConfiguration <TestModel1>();

            tc.AddIf("Foo", "IsAdmin", (x, s) => x.Id);

            var model = new TestModel1();
            var state = new State();

            state["IsAdmin"] = Boxed.False;
            var result = tc.Augments.First().ValueFunc(model, state);

            result.Should().Be(AugmentationValue.Ignore);
        }
Exemple #8
0
        public void ExposeIf_True()
        {
            var tc = new TypeConfiguration <TestModel1>();

            tc.ExposeIf("Foo", "IsAdmin");

            var model = new TestModel1();
            var state = new State();

            state["IsAdmin"] = Boxed.True;
            var result = tc.Augments.First().ValueFunc(model, state);

            result.Should().Be(AugmentationValue.Ignore);
        }
            public void CollectsOnlyDeclaredProperties()
            {
                var modelBTypeConfiguration = new TypeConfiguration(typeof(TestModelB));
                var list = new List <TypeConfiguration>()
                {
                    modelBTypeConfiguration
                };
                var builder = new TypeConfigurationBuilder(list);

                var result = builder.Build(modelBTypeConfiguration, typeof(TestModelB));

                var prop = result.Properties.Should().HaveCount(1).And.Subject.First();

                prop.PropertyInfo.Name.Should().Be(nameof(TestModelB.Foo));
            }
        public void DetectsSelfReferencing()
        {
            var modelTypeConfiguration = new TypeConfiguration <TestModelSelfReferencing>();

            modelTypeConfiguration.Add("Some", (x, state) => "some");
            var list = new List <TypeConfiguration>
            {
                modelTypeConfiguration
            };
            var builder = new TypeConfigurationBuilder(list);
            var result  = builder.Build(modelTypeConfiguration, typeof(TestModelSelfReferencing));

            modelTypeConfiguration.Properties.Should().HaveCount(2);
            modelTypeConfiguration.Properties[1].TypeConfiguration.Should().Be(modelTypeConfiguration);
        }
 public void Combine(TypeConfiguration another)
 {
     Augments.AddRange(another.Augments);
     foreach (var thunk in another.CustomThunks)
     {
         AddCustomThunk(thunk);
     }
     if (another.NestedConfigurations.IsValueCreated)
     {
         foreach (var pair in another.NestedConfigurations.Value)
         {
             NestedConfigurations.Value.Add(pair.Key, pair.Value);
         }
     }
 }
        private static NestedTypeConfiguration GetNestedTypeConfiguration(
            TypeConfiguration typeConfiguration, APropertyInfo property)
        {
            if (!typeConfiguration.NestedConfigurations.IsValueCreated)
            {
                return(null);
            }

            if (!typeConfiguration.NestedConfigurations.Value.TryGetValue(
                    property.PropertyInfo, out var ntc))
            {
                return(null);
            }

            return(ntc);
        }
            public void CorrectlyResolvesRawComplexTypes()
            {
                var modelNestedTypeConfiguration = new TypeConfiguration(typeof(TestModelNested));
                var list = new List <TypeConfiguration>
                {
                    modelNestedTypeConfiguration
                };
                var builder = new TypeConfigurationBuilder(list);

                var result = builder.Build(modelNestedTypeConfiguration, typeof(TestModelNested));

                result.Properties.Should()
                .ContainSingle(p => p.PropertyInfo.Name == nameof(TestModelNested.Id)).And
                .ContainSingle(p =>
                               p.PropertyInfo.Name == nameof(TestModelNested.Nested) &&
                               p.TypeConfiguration == null);
            }
        private static List <TypeConfiguration> BuildList(TypeConfiguration typeConfiguration, TypeConfiguration ephemeral)
        {
            var list = new List <TypeConfiguration>();

            if (typeConfiguration != null)
            {
                list.AddRange(typeConfiguration.BaseTypeConfigurations);
                list.Add(typeConfiguration);
            }

            if (ephemeral != null)
            {
                list.Add(ephemeral);
            }

            return(list);
        }
            public void EnsureCurrent()
            {
                if (Current != null)
                {
                    Current.Built = true;
                }

                if (!Empty)
                {
                    return;
                }

                Current = new TypeConfiguration(Type)
                {
                    Built = true
                };
                Current.BaseTypeConfigurations.AddRange(_typeConfigurations);
            }
            public void CollectsBaseTypesRegardlessOfTheirConfiguration()
            {
                var modelCTypeConfiguration = new TypeConfiguration(typeof(TestModelC));
                var list = new List <TypeConfiguration>
                {
                    modelCTypeConfiguration
                };
                var builder = new TypeConfigurationBuilder(list);

                var result = builder.Build(modelCTypeConfiguration, typeof(TestModelC));

                var baseTypeConfigurations = result.BaseTypeConfigurations.Should().HaveCount(2).And.Subject;

                baseTypeConfigurations.ElementAt(0).Type.Should().Be(typeof(TestModelA));
                baseTypeConfigurations.ElementAt(0).Properties.Should().HaveCount(1);
                baseTypeConfigurations.ElementAt(1).Type.Should().Be(typeof(TestModelB));
                baseTypeConfigurations.ElementAt(1).Properties.Should().HaveCount(1);
            }
            public void CollectsBaseTypes_Discontinuous_ButWithNested()
            {
                var modelATypeConfiguration = new TypeConfiguration(typeof(TestModelA));
                var modelCTypeConfiguration = new TypeConfiguration(typeof(TestModelC2));
                var list = new List <TypeConfiguration>
                {
                    modelATypeConfiguration,
                    modelCTypeConfiguration
                };
                var builder = new TypeConfigurationBuilder(list);

                var result = builder.Build(modelCTypeConfiguration, typeof(TestModelC2));

                var baseTypeConfigurations = result.BaseTypeConfigurations.Should().HaveCount(2).And.Subject;

                baseTypeConfigurations.ElementAt(0).Type.Should().Be(typeof(TestModelA));
                baseTypeConfigurations.ElementAt(1).Type.Should().Be(typeof(TestModelB2));
            }
            public void NestedEnumerable()
            {
                var modelATypeConfiguration = new TypeConfiguration(typeof(TestModelA));
                var list = new List <TypeConfiguration>()
                {
                    modelATypeConfiguration
                };
                var builder = new TypeConfigurationBuilder(list);
                var anon    = new { Models = new[] { new TestModelA(), new TestModelA() } };

                var result = builder.Build(null, anon.GetType());

                result.Should().NotBeNull();

                var nested = result.Properties.Single(p => p.Type == typeof(TestModelA));

                nested.TypeInfoWrapper.IsArray.Should().Be(true);
            }
            public void Unknown_WithNested()
            {
                var modelATypeConfiguration = new TypeConfiguration(typeof(TestModelA));
                var list = new List <TypeConfiguration>
                {
                    modelATypeConfiguration
                };
                var builder = new TypeConfigurationBuilder(list);
                var anon    = new { Model = new TestModelA() };

                var result = builder.Build(null, anon.GetType());

                result.Should().NotBeNull();

                var nested = result.Properties.Single(p => p.Type == typeof(TestModelA));

                nested.TypeConfiguration.Should().Be(modelATypeConfiguration);
            }
Exemple #20
0
        public void ConfigureNestedArray()
        {
            var tc = new TypeConfiguration <TestModelWithNestedArray>();

            tc.ConfigureNested(x => x.NestedArray, ntc =>
            {
                ntc.SetAddState((x, s1, s2) =>
                {
                    s2["ParentId"] = x.Id;
                });
            });

            tc.NestedConfigurations.Value.Should().HaveCount(1);
            tc.NestedConfigurations.Value.First().Invoking(c =>
            {
                c.Key.Name.Should().Be(nameof(TestModelWithNested.Nested));
                c.Value.Should().NotBeNull();
            });
        }
            public void CorrectlyResolvesPrimitiveLists()
            {
                var modelPrimitiveCollectionTypeConfiguration = new TypeConfiguration(typeof(TestModelWithPrimitiveList));
                var list = new List <TypeConfiguration>
                {
                    modelPrimitiveCollectionTypeConfiguration
                };
                var builder = new TypeConfigurationBuilder(list);

                var result = builder.Build(modelPrimitiveCollectionTypeConfiguration, typeof(TestModelWithPrimitiveList));

                result.Properties.Should().HaveCount(1);
                result.Properties.Should()
                .ContainSingle(p =>
                               p.PropertyInfo.Name == nameof(TestModelWithPrimitiveList.Strings) &&
                               p.TypeInfoWrapper.IsArray &&
                               p.TypeConfiguration == null &&
                               p.Type == typeof(string));
            }
            public void CollectsAllProperties()
            {
                var modelWithNestedTypeConfiguration = new TypeConfiguration(typeof(TestModelWithNested));
                var modelNestedTypeConfiguration     = new TypeConfiguration(typeof(TestModelNested));
                var list = new List <TypeConfiguration>
                {
                    modelWithNestedTypeConfiguration,
                    modelNestedTypeConfiguration
                };
                var builder = new TypeConfigurationBuilder(list);

                var result = builder.Build(modelWithNestedTypeConfiguration, typeof(TestModelWithNested));

                result.Properties.Should().HaveCount(4);
                result.Properties.Should()
                .ContainSingle(p => p.PropertyInfo.Name == nameof(TestModelWithNested.Id) && p.Type == typeof(int)).And
                .ContainSingle(p => p.PropertyInfo.Name == nameof(TestModelWithNested.Some1) && p.Type == typeof(string)).And
                .ContainSingle(p => p.PropertyInfo.Name == nameof(TestModelWithNested.Some2) && p.Type == typeof(string));
            }
        public void EnsureConfigurationIsBuiltOnlyOnce()
        {
            var modelATypeConfiguration = new TypeConfiguration <TestModelA>();

            modelATypeConfiguration.Add("Some", (x, state) => "some");
            var modelBTypeConfiguration = new TypeConfiguration(typeof(TestModelB));
            var list = new List <TypeConfiguration>
            {
                modelATypeConfiguration,
                modelBTypeConfiguration
            };
            var builder = new TypeConfigurationBuilder(list);
            var result  = builder.Build(modelBTypeConfiguration, typeof(TestModelB));

            var anon = new { Model = new TestModelB() };

            result = builder.Build(null, anon.GetType());

            modelBTypeConfiguration.BaseTypeConfigurations.Should().HaveCount(1);
        }
Exemple #24
0
        public void ConfigureNested_AddState()
        {
            var tc = new TypeConfiguration <TestModelWithNested>();

            tc.ConfigureNested(x => x.Nested, ntc =>
            {
                ntc.SetAddState((x, s1, s2) =>
                {
                    s2["ParentId"] = x.Id;
                });
            });

            var nested = tc.NestedConfigurations.Value.First();
            var state1 = new State();
            var state2 = new State();
            var model  = new TestModelWithNested();

            nested.Value.AddState(model, state1, state2);

            state2["ParentId"].Should().Be(model.Id);
        }
Exemple #25
0
        public void Configure <T>(Action <TypeConfiguration <T> > configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            if (Built)
            {
                throw new InvalidOperationException("The configuration has already been built.");
            }

            var type = typeof(T);
            var typeConfiguration = TypeConfigurations.FirstOrDefault(c => c.Type == type) as TypeConfiguration <T>;

            if (typeConfiguration == null)
            {
                typeConfiguration = new TypeConfiguration <T>();
                TypeConfigurations.Add(typeConfiguration);
            }

            configure(typeConfiguration);
        }
            public void CollectsNestedTypes()
            {
                var modelWithNestedTypeConfiguration   = new TypeConfiguration(typeof(TestModelWithNested));
                var modelNestedTypeConfiguration       = new TypeConfiguration(typeof(TestModelNested));
                var modelNestedNestedTypeConfiguration = new TypeConfiguration(typeof(TestModelNestedNested));
                var list = new List <TypeConfiguration>
                {
                    modelWithNestedTypeConfiguration,
                    modelNestedTypeConfiguration,
                    modelNestedNestedTypeConfiguration
                };
                var builder = new TypeConfigurationBuilder(list);

                var result = builder.Build(modelWithNestedTypeConfiguration, typeof(TestModelWithNested));

                var nested = result.Properties.Single(p => p.Type == typeof(TestModelNested));

                nested.TypeConfiguration.Should().Be(modelNestedTypeConfiguration);

                var nestedNested = nested.TypeConfiguration.Properties.Single(p => p.Type == typeof(TestModelNestedNested));

                nestedNested.TypeConfiguration.Should().Be(modelNestedNestedTypeConfiguration);
            }
        private Task <object> AugmentCommonAsync <T>(
            object obj,
            Action <TypeConfiguration <T> > configure,
            Action <IState> addState)
        {
            if (obj == null)
            {
                return(_nullResultTask);
            }

            var type = obj.GetType();

            if (configure == null)
            {
                return(AugmentInternal(
                           obj, type,
                           addState,
                           null,
                           null));
            }
            else
            {
                return(AugmentInternal(
                           obj, type,
                           addState,
                           (context, state) =>
                {
                    var c = state as Action <TypeConfiguration <T> >;
                    var ephemeralTypeConfigration = new TypeConfiguration <T>();
                    Debug.Assert(c != null, "c != null");
                    c(ephemeralTypeConfigration);
                    context.EphemeralTypeConfiguration = ephemeralTypeConfigration;
                },
                           configure));
            }
        }
        public TypeConfiguration Build(TypeConfiguration typeConfiguration, Type type, bool alwaysBuild = false)
        {
            if (typeConfiguration == null && type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (typeConfiguration != null && typeConfiguration.Type != type)
            {
                throw new ArgumentException("type should be the same as typeConfiguration.Type.");
            }

            if (ReflectionHelper.IsPrimitive(type))
            {
                return(null);
            }

            var context = new Context(
                alwaysBuild ? (typeConfiguration ?? new TypeConfiguration(type)) : typeConfiguration,
                type);

            BuildOne(context, type);
            return(context.Current);
        }
 public Context(TypeConfiguration current, Type type)
 {
     Current = current;
     Type    = type;
 }
 public AugmentationContext(object obj, TypeConfiguration typeConfiguration, IReadOnlyState state)
 {
     Object            = obj;
     TypeConfiguration = typeConfiguration;
     State             = state;
 }