コード例 #1
0
ファイル: TsqlReader.cs プロジェクト: joachim-heck/generator
        public void Read(ConfigurationBase configurationBase, List <ITransferObject> transferObjects)
        {
            TsqlReadConfiguration configuration = (TsqlReadConfiguration)configurationBase;

            this.Validate(configuration);
            TsqlTypeReader typeReader = new TsqlTypeReader(configuration.Connection);

            foreach (TsqlReadEntity readEntity in configuration.Entities)
            {
                ModelTransferObject model;
                if (!string.IsNullOrEmpty(readEntity.Table))
                {
                    List <TsqlColumn> columns = typeReader.GetColumns(readEntity.Schema ?? configuration.Schema, readEntity.Table);
                    model = new ModelTransferObject
                    {
                        Name      = readEntity.Name ?? readEntity.Table,
                        Namespace = readEntity.Namespace ?? configuration.Namespace,
                        Language  = TsqlLanguage.Instance
                    };
                    foreach (TsqlColumn column in columns)
                    {
                        model.Properties.Add(new PropertyTransferObject
                        {
                            Name = column.Name,
                            Type = new TypeTransferObject {
                                Name = column.Type, IsNullable = column.IsNullable
                            }
                        });
                    }
                    transferObjects.Add(model);
                }
                else
                {
                    //TODO: Implement for StoredProcedure
                    model = new ModelTransferObject
                    {
                        Name      = readEntity.Name ?? readEntity.StoredProcedure,
                        Namespace = readEntity.Namespace ?? configuration.Namespace,
                        Language  = TsqlLanguage.Instance
                    };
                }
                EntityTransferObject entity = new EntityTransferObject
                {
                    Name   = model.Name,
                    Model  = model,
                    Table  = readEntity.Table,
                    Schema = readEntity.Schema ?? configuration.Schema
                };
                if (!string.IsNullOrEmpty(readEntity.Table))
                {
                    typeReader.GetPrimaryKeys(readEntity.Schema ?? configuration.Schema, readEntity.Table)
                    .Select(x => new EntityKeyTransferObject {
                        Name = x.Name
                    })
                    .ForEach(entity.Keys.Add);
                    List <TsqlNavigationProperty> navigationProperties = typeReader.GetNavigationProperties(readEntity.Schema ?? configuration.Schema, readEntity.Table);
                }
                foreach (TsqlReadEntityKeyAction action in readEntity.KeyActions)
                {
                    switch (action.Action.ToLowerInvariant())
                    {
                    case "remove":
                    case "delete":
                        if (action.All)
                        {
                            entity.Keys.Clear();
                        }
                        else
                        {
                            entity.Keys.Remove(entity.Keys.FirstOrDefault(x => x.Name.Equals(action.Name, StringComparison.InvariantCultureIgnoreCase)));
                        }
                        break;

                    case "add":
                    case "insert":
                        entity.Keys.Add(new EntityKeyTransferObject {
                            Name = action.Name
                        });
                        break;

                    default:
                        throw new InvalidOperationException($"Unknown entity key action {action.Action} found");
                    }
                }
                foreach (EntityKeyTransferObject key in entity.Keys)
                {
                    key.Property = entity.Model.Properties.FirstOrDefault(x => x.Name == key.Name).AssertIsNotNull(key.Name, $"Key {key.Name} has no matching property");
                    key.Type     = key.Property.Type;
                }
                transferObjects.Add(entity);
            }
            foreach (TsqlReadStoredProcedure readStoredProcedure in configuration.StoredProcedures)
            {
                string schema = readStoredProcedure.Schema ?? configuration.Schema;
                //List<TsqlColumn> columns = typeReader.GetColumnsFromStoredProcedure(schema, readStoredProcedure.Name);
                StoredProcedureTransferObject storedProcedure = new StoredProcedureTransferObject {
                    Schema = schema, Name = readStoredProcedure.Name
                };
                storedProcedure.ReturnType = new TypeTransferObject {
                    Name = "void", FromSystem = true
                };
                transferObjects.Add(storedProcedure);
            }
        }
コード例 #2
0
        private Dictionary <IEdmType, TypeTransferObject> ReadModels(IEdmModel edmModel, List <ITransferObject> list)
        {
            Dictionary <IEdmType, TypeTransferObject>   modelMapping  = new Dictionary <IEdmType, TypeTransferObject>();
            Dictionary <IEdmType, EntityTransferObject> entityMapping = new Dictionary <IEdmType, EntityTransferObject>();

            foreach (IEdmSchemaType schemaType in edmModel.SchemaElements.Select(element => edmModel.FindDeclaredType(element.FullName())).Where(x => x != null))
            {
                if (schemaType.TypeKind == EdmTypeKind.Entity && schemaType is IEdmEntityType entityType)
                {
                    ModelTransferObject model = new ModelTransferObject
                    {
                        Name      = schemaType.Name,
                        Namespace = schemaType.Namespace,
                        Language  = ODataLanguage.Instance
                    };
                    modelMapping[entityType.AsActualType()] = model;
                    EntityTransferObject entity = new EntityTransferObject
                    {
                        Name  = schemaType.Name,
                        Model = model
                    };
                    entityMapping[entityType.AsActualType()] = entity;
                    foreach (IEdmProperty edmProperty in entityType.DeclaredProperties)
                    {
                        model.Properties.Add(new PropertyTransferObject
                        {
                            Name = edmProperty.Name,
                            Type = this.ToTransferObject(edmProperty.Type.Definition, modelMapping)
                        });
                    }
                    if (entityType.DeclaredKey != null)
                    {
                        foreach (IEdmStructuralProperty key in entityType.DeclaredKey)
                        {
                            PropertyTransferObject property = model.Properties.FirstOrDefault(x => x.Name == key.Name);
                            entity.Keys.Add(new EntityKeyTransferObject
                            {
                                Name     = key.Name,
                                Type     = this.ToTransferObject(key.Type.Definition, modelMapping).Clone(),
                                Property = property
                            });
                        }
                    }
                    // TODO: Add Navigation Properties
                    list.Add(model);
                    list.Add(entity);
                }
            }
            foreach (IEdmAction action in edmModel.SchemaElements.OfType <IEdmAction>())
            {
                IEdmType boundTo = action.FindParameter(BindingParameterName)?.Type.Definition;
                if (boundTo != null && modelMapping.ContainsKey(boundTo))
                {
                    EntityTransferObject       entity       = entityMapping[boundTo];
                    EntityActionTransferObject entityAction = new EntityActionTransferObject {
                        Name = action.Name, Namespace = action.Namespace
                    };
                    if (action.ReturnType != null)
                    {
                        entityAction.ReturnType = modelMapping[action.ReturnType.Definition];
                    }
                    entity.Actions.Add(entityAction);
                    foreach (IEdmOperationParameter actionParameter in action.Parameters.Where(parameter => parameter.Name != BindingParameterName))
                    {
                        entityAction.Parameters.Add(new EntityActionParameterTransferObject {
                            Name = actionParameter.Name, Type = modelMapping[actionParameter.Type.Definition]
                        });
                    }
                }
                else
                {
                    // TODO: Unbound actions
                }
            }
            return(modelMapping);
        }
コード例 #3
0
        public virtual void Write(EntityFrameworkWriteConfiguration configuration, List <ITransferObject> transferObjects, List <FileTemplate> files)
        {
            foreach (EntityFrameworkWriteRepositoryConfiguration repositoryConfiguration in configuration.Repositories)
            {
                EntityTransferObject entity = transferObjects.OfType <EntityTransferObject>().FirstOrDefault(x => x.Name == repositoryConfiguration.Entity)
                                              .AssertIsNotNull(nameof(repositoryConfiguration.Entity), $"Entity {repositoryConfiguration.Entity} not found. Ensure it is read before.");

                ClassTemplate repository = files.AddFile(configuration.RelativePath, configuration.AddHeader)
                                           .AddNamespace(repositoryConfiguration.Namespace ?? configuration.Namespace)
                                           .AddClass(repositoryConfiguration.Name ?? entity.Name + "Repository")
                                           .FormatName(configuration)
                                           .WithUsing("System.Collections.Generic")
                                           .WithUsing("System.Linq");
                if (configuration.IsCore)
                {
                    repository.WithUsing("Microsoft.EntityFrameworkCore");
                }
                else
                {
                    repository.WithUsing("System.Data.Entity");
                }
                if (!string.IsNullOrEmpty(configuration.Namespace) && !string.IsNullOrEmpty(repositoryConfiguration.Namespace) && configuration.Namespace != repositoryConfiguration.Namespace)
                {
                    repository.AddUsing(configuration.Namespace);
                }

                configuration.Usings.ForEach(x => repository.AddUsing(x));
                repositoryConfiguration.Usings.ForEach(x => repository.AddUsing(x));

                TypeTemplate modelType = entity.Model.ToTemplate();

                FieldTemplate dataSetField     = repository.AddField("dataSet", Code.Generic("DbSet", modelType)).Readonly();
                FieldTemplate dataContextField = repository.AddField("dataContext", Code.Type("DataContext")).Readonly();

                TypeTemplate        dataContextType      = Code.Type("DataContext");
                ConstructorTemplate constructor          = repository.AddConstructor();
                ParameterTemplate   dataContextParameter = constructor.AddParameter(dataContextType, "dataContext", Code.Null());
                constructor.Code.AddLine(Code.This().Field(dataContextField).Assign(Code.NullCoalescing(Code.Local(dataContextParameter), Code.New(dataContextType))).Close())
                .AddLine(Code.This().Field(dataSetField).Assign(Code.This().Field(dataContextField).GenericMethod("Set", modelType)).Close());

                repository.AddMethod("Get", Code.Generic("IQueryable", modelType))
                .Code.AddLine(Code.Return(Code.This().Field(dataSetField)));

                repository.AddMethod("Get", modelType)
                .WithParameter(Code.Type("params object[]"), "keys")
                .Code.AddLine(Code.Return(Code.This().Field(dataSetField).Method("Find", Code.Local("keys"))));

                if (configuration.IsCore)
                {
                    repository.AddMethod("Add", modelType)
                    .WithParameter(modelType, "entity")
                    .Code.AddLine(Code.Declare(modelType, "result", Code.This().Field(dataSetField).Method("Add", Code.Local("entity")).Property("Entity")))
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));

                    repository.AddMethod("Add", Code.Generic("IEnumerable", modelType))
                    .WithParameter(Code.Generic("IEnumerable", modelType), "entities")
                    .Code.AddLine(Code.Declare(Code.Generic("IEnumerable", modelType), "result", Code.Local("entities").Method("Select", Code.Lambda("x", Code.This().Field(dataSetField).Method("Add", Code.Local("x")).Property("Entity"))).Method("ToList")))
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));

                    repository.AddMethod("Update", modelType)
                    .WithParameter(modelType, "entity")
                    .Code.AddLine(Code.Declare(modelType, "result", Code.This().Field(dataSetField).Method("Update", Code.Local("entity")).Property("Entity")))
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));

                    repository.AddMethod("Update", Code.Generic("IEnumerable", modelType))
                    .WithParameter(Code.Generic("IEnumerable", modelType), "entities")
                    .Code.AddLine(Code.Declare(Code.Generic("IEnumerable", modelType), "result", Code.Local("entities").Method("Select", Code.Lambda("x", Code.This().Field(dataSetField).Method("Update", Code.Local("x")).Property("Entity"))).Method("ToList")))
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));
                }
                else
                {
                    repository.AddMethod("Add", modelType)
                    .WithParameter(modelType, "entity")
                    .Code.AddLine(Code.Declare(modelType, "result", Code.This().Field(dataSetField).Method("Add", Code.Local("entity"))))
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));

                    repository.AddMethod("Add", Code.Generic("IEnumerable", modelType))
                    .WithParameter(Code.Generic("IEnumerable", modelType), "entities")
                    .Code.AddLine(Code.Declare(Code.Generic("IEnumerable", modelType), "result", Code.Local("entities").Method("Select", Code.Lambda("x", Code.This().Field(dataSetField).Method("Add", Code.Local("x")))).Method("ToList")))
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));

                    repository.WithUsing("System.Data.Entity.Migrations")
                    .AddMethod("Update", modelType)
                    .WithParameter(modelType, "entity")
                    .Code.AddLine(Code.This().Field(dataSetField).Method("AddOrUpdate", Code.Local("entity")).Close())
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("entity")));

                    repository.WithUsing("System.Data.Entity.Migrations")
                    .AddMethod("Update", Code.Generic("IEnumerable", modelType))
                    .WithParameter(Code.Generic("IEnumerable", modelType), "entities")
                    .Code.AddLine(Code.Declare(Code.Generic("List", modelType), "result", Code.Local("entities").Method("ToList")))
                    .AddLine(Code.Local("result").Method("ForEach", Code.Lambda("x", Code.This().Field(dataSetField).Method("AddOrUpdate", Code.Local("x")))).Close())
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));
                }

                //repository.AddMethod("Update", Code.Void())
                //          .WithParameter(Code.Generic("Delta", modelType), "delta")
                //          .WithParameter(Code.Type("object[]"), "keys")
                //          .Code.AddLine(Code.Declare(modelType, "entity", Code.This().Field(dataSetField).Method("Find", Code.Local("keys"))))
                //          .AddLine(Code.If(Code.Local("entity").Equals().Null(), x => x.Code.AddLine(Code.Throw(Code.Type("InvalidOperationException"), Code.String("Can not find any element with this keys, Use Add(...) method instead")))))
                //          .AddLine(Code.Local("delta").Method("Patch", Code.Local("entity")).Close())
                //          .AddLine(Code.This().Method("Update", Code.Local("entity")).Close())
                //          .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close());

                repository.AddMethod("Delete", Code.Void())
                .WithParameter(modelType, "entity")
                .Code.AddLine(Code.This().Field(dataSetField).Method("Remove", Code.Local("entity")).Close())
                .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close());

                repository.AddMethod("Delete", Code.Void())
                .WithParameter(Code.Generic("IEnumerable", modelType), "entities")
                .Code.AddLine(Code.This().Field(dataSetField).Method("RemoveRange", Code.Local("entities")).Close())
                .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close());

                if (configuration.IsCore)
                {
                    repository.AddMethod("Delete", Code.Void())
                    .WithParameter(Code.Type("params object[]"), "keys")
                    .Code.AddLine(Code.This().Field(dataSetField).Method("Remove", Code.This().Field(dataSetField).Method("Find", Code.Local("keys"))).Close())
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close());
                }
                else
                {
                    repository.AddMethod("Delete", Code.Void())
                    .WithParameter(Code.Type("params object[]"), "keys")
                    .Code.AddLine(Code.This().Field(dataSetField).Method("Remove", Code.This().Field(dataSetField).Method("Find", Code.Local("keys"))).Close())
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close());
                }

                //foreach (string key in entity.Keys)
                //{
                //    PropertyTransferObject property = entity.Model.Properties.First(x => x.Name.Equals(key, StringComparison.InvariantCultureIgnoreCase));
                //    delete.AddParameter(property.Type.ToTemplate(), property.Name)
                //          .FormatName(configuration.Language, configuration.FormatNames);
                //}
            }
        }
コード例 #4
0
        public virtual void Write(AspDotNetWriteConfiguration configuration, List <ITransferObject> transferObjects, List <FileTemplate> files)
        {
            if (!configuration.Language.IsCsharp())
            {
                throw new InvalidOperationException($"Can not generate ASP.net Controller for language {configuration.Language?.Name ?? "Empty"}. Only Csharp is currently implemented");
            }
            foreach (AspDotNetWriteEntityControllerConfiguration controllerConfiguration in configuration.Controllers)
            {
                EntityTransferObject entity = transferObjects.OfType <EntityTransferObject>().FirstOrDefault(x => x.Name == controllerConfiguration.Entity)
                                              .AssertIsNotNull(nameof(controllerConfiguration.Entity), $"Entity {controllerConfiguration.Entity} not found. Ensure it is read before.");

                string        nameSpace  = (controllerConfiguration.Namespace ?? configuration.Namespace).AssertIsNotNull(nameof(configuration.Namespace), "asp writer requires a namespace");
                ClassTemplate controller = files.AddFile(configuration.RelativePath, configuration.AddHeader)
                                           .AddNamespace(nameSpace)
                                           .AddClass(controllerConfiguration.Name ?? entity.Name + "Controller", Code.Type(configuration.Template.ControllerBase))
                                           .FormatName(configuration)
                                           .WithAttribute("Route", Code.String(controllerConfiguration.Route ?? "[controller]"));

                controller.Usings.AddRange(configuration.Template.Usings);

                TypeTemplate modelType = entity.Model.ToTemplate();

                configuration.Usings.ForEach(x => controller.AddUsing(x));
                controllerConfiguration.Usings.ForEach(x => controller.AddUsing(x));

                FieldTemplate repositoryField = controller.AddField("repository", Code.Type(entity.Name + "Repository")).Readonly();
                controller.AddConstructor().Code.AddLine(Code.This().Field(repositoryField).Assign(Code.New(repositoryField.Type)).Close());

                if (controllerConfiguration.Get != null)
                {
                    controller.AddUsing("System.Linq");
                    MethodTemplate method = controller.AddMethod("Get", Code.Generic("IEnumerable", modelType));
                    if (configuration.Template.UseAttributes)
                    {
                        method.WithAttribute("HttpGet", Code.String(controllerConfiguration.Get.Name ?? "[action]"));
                    }
                    DeclareTemplate queryable = Code.Declare(Code.Generic("IQueryable", modelType), "queryable", Code.This().Field(repositoryField).Method("Get"));
                    method.Code.AddLine(queryable);
                    foreach (PropertyTransferObject property in entity.Model.Properties)
                    {
                        ParameterTemplate parameter = method.AddParameter(property.Type.ToTemplate(), property.Name, Code.Local("default")).FormatName(configuration);
                        method.Code.AddLine(Code.If(Code.Local(parameter).NotEquals().Local("default"), x => x.Code.AddLine(Code.Local(queryable).Assign(Code.Local(queryable).Method("Where", Code.Lambda("x", Code.Local("x").Property(property.Name).Equals().Local(parameter)))).Close())));
                    }
                    method.Code.AddLine(Code.Return(Code.Local(queryable)));
                }
                if (controllerConfiguration.Post != null)
                {
                    MethodTemplate method = controller.AddMethod("Post", Code.Void());
                    if (configuration.Template.UseAttributes)
                    {
                        method.WithAttribute("HttpPost", Code.String(controllerConfiguration.Post.Name ?? "[action]"));
                    }
                    ParameterTemplate parameter = method.AddParameter(modelType, "entity")
                                                  .WithAttribute("FromBody");

                    method.Code.AddLine(Code.This().Field(repositoryField).Method("Add", Code.Local(parameter)).Close());
                }
                if (controllerConfiguration.Patch != null)
                {
                    MethodTemplate method = controller.AddMethod("Patch", Code.Void());
                    if (configuration.Template.UseAttributes)
                    {
                        method.WithAttribute("HttpPatch", Code.String(controllerConfiguration.Patch.Name ?? "[action]"));
                    }
                    ParameterTemplate parameter = method.AddParameter(modelType, "entity")
                                                  .WithAttribute("FromBody");

                    method.Code.AddLine(Code.This().Field(repositoryField).Method("Update", Code.Local(parameter)).Close());
                }
                if (controllerConfiguration.Put != null)
                {
                    MethodTemplate method = controller.AddMethod("Put", Code.Void());
                    if (configuration.Template.UseAttributes)
                    {
                        method.WithAttribute("HttpPut", Code.String(controllerConfiguration.Put.Name ?? "[action]"));
                    }
                    ParameterTemplate parameter = method.AddParameter(modelType, "entity")
                                                  .WithAttribute("FromBody");

                    method.Code.AddLine(Code.This().Field(repositoryField).Method("Update", Code.Local(parameter)).Close());
                }
                if (controllerConfiguration.Delete != null)
                {
                    MethodTemplate method = controller.AddMethod("Delete", Code.Void());
                    if (configuration.Template.UseAttributes)
                    {
                        method.WithAttribute("HttpDelete", Code.String(controllerConfiguration.Delete.Name ?? "[action]"));
                    }
                    List <ParameterTemplate> parameters = new List <ParameterTemplate>();
                    foreach (EntityKeyTransferObject key in entity.Keys)
                    {
                        PropertyTransferObject property = entity.Model.Properties.First(x => x.Name.Equals(key.Name, StringComparison.InvariantCultureIgnoreCase));
                        parameters.Add(method.AddParameter(property.Type.ToTemplate(), property.Name)
                                       .FormatName(configuration));
                    }
                    method.Code.AddLine(Code.This().Field(repositoryField).Method("Delete", parameters.Select(x => Code.Local(x))).Close());
                }
            }
        }