예제 #1
0
        public void Test_dynamic()
        {
            //Arrange
            var     json  = "{ Name: \"John\", Age: 31, City: \"New York\"}";
            JObject stuff = JObject.Parse(json);


            var properties   = stuff.Properties();
            var listOfFields = properties.Select(p => new CustomFieldDefinition
            {
                FieldName = p.Name,
                FieldType = GetType(p.Value.Type)
            }).ToList();

            var type = CustomTypeBuilder.CompileResultType(listOfFields);

            var prop = type.GetProperties();

            var account    = JsonConvert.DeserializeObject(json, type);
            var expression = "account.Age > 10";
            var parameters = new Dictionary <string, object>
            {
                { "account", account },
            };


            //Act
            var result = _expressionEvaluator.Evaluate <bool>(expression, parameters);

            //Assert
            result.Should().Be(true);
        }
예제 #2
0
        public Context(DbContextOptions options) : base(options)
        {
            _entityTypes = CustomTypeBuilder.GetAllCustomTypes();

            //Database.EnsureDeleted();
            Database.EnsureCreated();
        }
예제 #3
0
        private Type GetType(string inputType, bool isNullable)
        {
            var mapper = new Dictionary <string, string>
            {
                { "Text", "string" },
                { "Number", "int" },
                { "DateTime", "System.DateTime" }
            };

            string name = String.Empty;

            if (mapper.ContainsKey(inputType))
            {
                name = mapper[inputType];
            }
            else
            {
                name = inputType;
            }

            var type = CustomTypeBuilder.GetType(name);

            if (isNullable)
            {
                type = typeof(Nullable <>).MakeGenericType(type);
            }

            return(type);
        }
        public void CompileResultType_UnderValidCircumstances_ExpectSuccess()
        {
            // Arrange:
            var properties = new Dictionary<string, Type> { { "prop", typeof(int) } };

            // Act:
            var type = CustomTypeBuilder.CompileResultType("TestClass", properties);

            // Assert:
            Assert.IsNotNull(type);
            Assert.IsNotNull(type.AsType());

            var obj = Activator.CreateInstance(type.AsType());
            var prop = type.GetProperty("prop");
            prop.SetValue(obj, 1, null);

            Assert.AreEqual(1, prop.GetValue(obj, null));
        }
예제 #5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            var deserializer = new YamlDeserializer();
            var settings     = deserializer.DeserializeConfiguration <Settings>();

            settings.DynamicObjects.AddDefaultFields();

            var objectGenerator = new ObjectGenerator();

            objectGenerator.CreateObjects(settings.DynamicObjects);

            services.AddAllServices(settings);

            var entityTypes = CustomTypeBuilder.GetAllCustomTypes();
            var schemaTypes = new List <Type>();

            foreach (var type in entityTypes)
            {
                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var service    = serviceProvider.GetRequiredService <DynamicObjectService>();
                var schemaType = Activator.CreateInstance(typeof(DynamicObjectType <>).MakeGenericType(type), new object[] { service }).GetType();
                schemaTypes.Add(schemaType);
                services.AddSingleton(schemaType);
            }

            IServiceProvider schemaServiceProvider = services.BuildServiceProvider();
            var schemaGenerator = new SchemaGenerator(schemaServiceProvider);
            var schema          = schemaGenerator.CreateSchema(schemaTypes.ToArray());

            SchemaPrinter printer = new SchemaPrinter(schema);
            string        wat     = printer.Print();

            services.AddSingleton <IDocumentExecuter, DocumentExecuter>();
            services.AddSingleton <ISchema>(schema);
        }
예제 #6
0
        public static IEnumerable <dynamic> ListServicesDiscounts()
        {
            var services = _dummyServices.Select(service => new
            {
                service.ID,
                service.Name,
                PropName = service.Name
                           .Replace(" ", string.Empty)
                           .ToLower()
            });

            var companies = _dummyCompanies.Select(company => new
            {
                company.ID,
                company.Name,
                Discounts = from service in services
                            join discount in _dummyDiscounts on new
                {
                    serviceId = service.ID,
                    companyId = company.ID
                }
                equals new
                {
                    serviceId = discount.ServiceId,
                    companyId = discount.CompanyId
                }
                into discountJoin
                from discount in discountJoin.DefaultIfEmpty()
                select new
                {
                    ServiceName = service.Name,
                    service.PropName,
                    Discount = discount != null ? discount.Discount : 0
                }
            });

            // Combine properties into a dictionary:
            var properties = new Dictionary <string, Type>
            {
                { "companyID", typeof(int) },
                { "companyName", typeof(string) }
            };

            foreach (var item in services)
            {
                properties.Add(item.PropName, typeof(float));
            }

            // Create a new class:
            var rowType = CustomTypeBuilder.CompileResultType("ServicesDiscountsRow", properties);

            // Create a table representation based on dynamic objects:
            var servicesDiscounts = companies.Select(company =>
            {
                var obj = Activator.CreateInstance(rowType.AsType());

                var companyID = rowType.GetProperty("companyID");
                companyID.SetValue(obj, company.ID, null);

                var companyName = rowType.GetProperty("companyName");
                companyName.SetValue(obj, company.Name, null);

                foreach (var discount in company.Discounts)
                {
                    var propName = discount.PropName;
                    var prop     = rowType.GetProperty(propName);
                    prop.SetValue(obj, discount.Discount, null);
                }

                return(obj);
            });

            return(servicesDiscounts);
        }