public SqlitePropertyTransferObject(PropertyTransferObject property) : base(property) { this.IsNotNull = this.Attributes.IsNotNull() || !property.Type.IsNullable; this.IsPrimaryKey = this.Attributes.IsPrimaryKey(); this.IsAutoIncrement = this.Attributes.IsAutoIncrement(); }
private TypeTransferObject Read(OpenApiReadConfiguration configuration, OpenApiSchema schema) { if (schema.Reference == null) { return(new TypeTransferObject { Name = schema.Type }); } ModelTransferObject model = transferObjects.OfType <ModelTransferObject>().FirstOrDefault(x => x.Name == schema.Reference.Id); if (model != null) { return(model); } if (schema.Type == "array") { return(new TypeTransferObject { Name = schema.Type, Generics = { new GenericAliasTransferObject { Type = this.Read(configuration, schema.Items) } } }); } if (schema.Type == "object") { model = new ModelTransferObject { Name = schema.Reference.Id, Namespace = configuration.Namespace, Language = OpenApiLanguage.Instance }; foreach (KeyValuePair <string, OpenApiSchema> propertyPair in schema.Properties) { PropertyTransferObject property = new PropertyTransferObject { Name = propertyPair.Key, Type = this.Read(configuration, propertyPair.Value) }; if (configuration.DataAnnotations && propertyPair.Value.MaxLength.HasValue) { property.Attributes.Add(new AttributeTransferObject { Name = "MaxLength", Namespace = "System.ComponentModel.DataAnnotations", Value = propertyPair.Value.MaxLength.Value }); } model.Properties.Add(property); } transferObjects.Add(model); return(model); } throw new InvalidOperationException($"Unknown schema type {schema.Type}"); }
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()); } } }
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); }
private void ReadClass(Type type, ModelTransferObject model, List <ITransferObject> transferObjects) { Logger.Trace($"Reflection read type {type.Name} ({type.Namespace})"); if (type.BaseType != typeof(object) && type.BaseType != null) { model.BasedOn = this.Read(type.BaseType, transferObjects); } Dictionary <Type, string> genericMapping = new Dictionary <Type, string>(); if (type.IsGenericType) { model.IsGeneric = true; model.Generics.Clear(); foreach (Type argument in type.GenericTypeArguments) { string alias = genericMapping.Count > 1 ? $"T{genericMapping.Count}" : "T"; genericMapping.Add(argument, alias); model.Generics.Add(new GenericAliasTransferObject { Alias = alias, Type = this.Read(argument, transferObjects) }); } } model.IsInterface = type.IsInterface; model.IsAbstract = type.IsAbstract; foreach (Type interFace in type.GetInterfaces(false)) { model.Interfaces.Add(this.Read(interFace, transferObjects)); } FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (FieldInfo field in fields) { FieldTransferObject fieldTransferObject = new FieldTransferObject { Name = field.Name, Type = genericMapping.ContainsKey(field.FieldType) ? new TypeTransferObject { Name = genericMapping[field.FieldType] } : this.Read(field.FieldType, transferObjects) }; model.Fields.Add(fieldTransferObject); } PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (PropertyInfo property in properties) { PropertyTransferObject propertyTransferObject = new PropertyTransferObject { Name = property.Name, Type = genericMapping.ContainsKey(property.PropertyType) ? new TypeTransferObject { Name = genericMapping[property.PropertyType] } : this.Read(property.PropertyType, transferObjects), CanRead = property.CanRead, CanWrite = property.CanWrite }; model.Properties.Add(propertyTransferObject); } }