예제 #1
0
        public void ModelSchema_Entities_Test()
        {
            var modelSchema = new ModelSchema();
            var ctGna       = new ModelComplexType()
            {
                Name = "gna"
            };
            var etGna = new ModelEntity()
            {
                Name = "gna", EntityTypeName = "gna"
            };

            modelSchema.ComplexTypes.Add(ctGna.AsFrozen());
            modelSchema.Entities.Add(etGna);
            Assert.True(ReferenceEquals(etGna.Owner, modelSchema));
            {
                var lstGna = modelSchema.FindEntity("gna");
                Assert.Equal(1, lstGna.Count);
                Assert.True(ReferenceEquals(lstGna.Single(), etGna));
            }
            {
                modelSchema.Freeze();
                ctGna.ThrowIfNotFrozen();
            }
            {
                var lstGna = modelSchema.FindEntity("gna");
                Assert.Equal(1, lstGna.Count);
                Assert.True(ReferenceEquals(lstGna.Single(), etGna));
            }
        }
        public void MappingModelBuilder_1_Test()
        {
            var testCfg = TestCfg.Get();
            var srcPath = System.IO.Path.Combine(testCfg.SolutionFolder, @"test\ProjectOnlineModelSchema.json");

            IServiceCollection services = new ServiceCollection();

            services.AddLogging((builder) => { builder.AddDebug(); });
            services.AddOfaSchlupferModel();
            services.AddOfaSchlupferEntity();
            //services.AddHttpClient((builder) => { });
            var serviceProvider = services.BuildServiceProvider();

            using (var scope = serviceProvider.CreateScope()) {
                var scopedServiceProvider = scope.ServiceProvider;
                var modelRoot             = scopedServiceProvider.GetRequiredService <ModelRoot>();
                var modelRepositorySource = modelRoot.CreateRepository("Source", null);
                var modelRepositoryTarget = modelRoot.CreateRepository("Target", null);

                var mappingModelRepository = new MappingModelRepository();
                mappingModelRepository = modelRoot.CreateMapping("SourceTarget", modelRepositorySource, modelRepositoryTarget);

                var jsonContent       = System.IO.File.ReadAllText(srcPath);
                var modelSchemaSource = ModelJsonUtility.DeserializeModelFromJson <ModelSchema>(jsonContent, null);
                Assert.NotNull(modelSchemaSource);
                modelRepositorySource.ModelSchema = modelSchemaSource;

                var modelSchemaTarget = new ModelSchema();
                modelRepositoryTarget.ModelSchema = modelSchemaTarget;

                var mappingModelBuilder = new MappingModelBuilder();
                mappingModelBuilder.MappingModelRepository = mappingModelRepository;

                var errors = new ModelErrors();
                mappingModelBuilder.Build(errors);

                if (errors.HasErrors())
                {
                    output.WriteLine(errors.ToString());
                }
                Assert.False(errors.HasErrors());

                {
                    var serializeSettings = new Newtonsoft.Json.JsonSerializerSettings();
                    serializeSettings.TypeNameAssemblyFormatHandling = Newtonsoft.Json.TypeNameAssemblyFormatHandling.Simple;
                    serializeSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto;
                    var schemaAsJson = Newtonsoft.Json.JsonConvert.SerializeObject(modelRoot, Newtonsoft.Json.Formatting.Indented, serializeSettings);
                    try {
                        string outputPath = System.IO.Path.Combine(testCfg.SolutionFolder, @"test\temp\MappingModelBuilder_1_Test.json");
                        System.IO.File.WriteAllText(outputPath, schemaAsJson);
                    } catch {
                        throw;
                    }
                }
            }
        }
예제 #3
0
        public void ModelSchema_ModelComplexType_Test()
        {
            var modelSchema = new ModelSchema();
            var ctGna       = new ModelComplexType()
            {
                Name = "gna"
            };

            modelSchema.ComplexTypes.Add(ctGna);
            ctGna.Freeze();
            Assert.True(ReferenceEquals(ctGna.Owner, modelSchema));
            var lstGna = modelSchema.FindComplexType("gna");

            Assert.Equal(1, lstGna.Count);
            Assert.True(ReferenceEquals(lstGna.Single(), ctGna));
        }
예제 #4
0
        public void BuildEntities(
            MappingModelSchema mappingModelSchema,
            ModelErrors errors
            )
        {
            ModelSchema modelSchemaSource = mappingModelSchema.Source;
            ModelSchema modelSchemaTarget = mappingModelSchema.Target;

            var lstEntityMappings = mappingModelSchema.EntityMappings.Where(_ => _.Enabled).ToList();

            foreach (var entityMapping in lstEntityMappings)
            {
                if (entityMapping.Target is null)
                {
                    var    targetName  = entityMapping.TargetName;
                    string extenalName = targetName;
                    string typeName    = null;
#warning SOON thinkof what's in the mapping.
                    if (this.NamingServiceTarget.EntitiesHaveFakeComplexTypes)
                    {
                        typeName = targetName;
                    }
                    modelSchemaTarget.CreateEntity(targetName, extenalName, typeName);
                }
            }

            foreach (var entitySource in modelSchemaSource.Entities)
            {
                if (this._KnownEntityMappingSourceName.Contains(entitySource.Name))
                {
                    continue;
                }

                (string targetName, string extenalName, string typeName, string typeExternalName) = this.NamingServiceTarget.SuggestEntityName(entitySource.Name);

                if (typeName is null && this.NamingServiceTarget.EntitiesHaveFakeComplexTypes)
                {
                    typeName = targetName;
                }

                var entityMapping = mappingModelSchema.CreateEntityMapping(
                    null,
                    entitySource.Name,
                    targetName,
                    this.EnabledForCreatedMappings || this.EnabledForCreatedMappingModelEntity,
                    true,
                    this.GetComment()
                    );
                if (entityMapping.Enabled)
                {
                    if (entityMapping.Target is null)
                    {
                        modelSchemaTarget.CreateEntity(entityMapping.TargetName, extenalName, typeName);
                    }
                }
                if (this._KnownComplexTypeMappingSourceName.Contains(entitySource.EntityTypeName))
                {
                    // if there is a rule ... ok
                }
                else
                {
                    var complexTypeSource = entitySource.EntityType;
                    if (complexTypeSource is null)
                    {
#warning SOON error??
                        errors.AddErrorOrThrow($"EntityType {entitySource.EntityTypeName} does not exists.", entitySource.Name);
                    }
                    else
                    {
                        var complexTypeMapping = mappingModelSchema.CreateComplexTypeMapping(
                            null,
                            complexTypeSource.Name,
                            typeName,
                            this.EnabledForCreatedMappings || this.EnabledForCreatedMappingModelComplexType,
                            true,
                            this.GetComment()
                            );
                        this._KnownComplexTypeMappingSourceName.Add(complexTypeSource.Name);
                        if (complexTypeMapping.Enabled && !string.IsNullOrEmpty(complexTypeMapping.TargetName))
                        {
                            if (complexTypeMapping.Target is null)
                            {
                                modelSchemaTarget.CreateComplexType(complexTypeMapping.TargetName ?? typeName, typeExternalName);
                            }
                            if ((!(complexTypeMapping.Source is null)) &&
                                (!(complexTypeMapping.Target is null))
                                )
                            {
                                this.BuildComplexTypeProperties(complexTypeMapping, errors);
                                this.BuildComplexTypeIndexes(complexTypeMapping, errors);
                            }
                            else
                            {
                                if (complexTypeMapping.Source is null)
                                {
                                    errors.AddErrorOrThrow($"complexTypeMapping.Source {complexTypeMapping.SourceName} is null", $"complexTypeMapping.Name: {complexTypeMapping.Name}");
                                }
                                if (complexTypeMapping.Target is null)
                                {
                                    errors.AddErrorOrThrow($"complexTypeMapping.Target {complexTypeMapping.TargetName} is null", $"complexTypeMapping.Name: {complexTypeMapping.Name}");
                                }
                            }
                        }
                    }
                }
            }