private Contracts.IGeneratedItem CreateModelFromContract(Type type, Common.UnitType unitType, Common.ItemType itemType) { type.CheckArgument(nameof(type)); var modelName = CreateModelNameFromInterface(type); var typeProperties = ContractHelper.GetAllProperties(type); var interfaces = GetInterfaces(type); var result = new Models.GeneratedItem(unitType, itemType) { FullName = CreateModelFullNameFromInterface(type), FileExtension = StaticLiterals.CSharpFileExtension, }; result.SubFilePath = $"{result.FullName}{result.FileExtension}"; CreateModelAttributes(type, result.Source); result.Add($"public partial class {modelName} : {type.FullName}"); result.Add("{"); result.AddRange(CreatePartialStaticConstrutor(modelName)); result.AddRange(CreatePartialConstrutor("public", modelName)); foreach (var item in ContractHelper.FilterPropertiesForGeneration(typeProperties)) { CreateModelPropertyAttributes(type, item, result.Source); result.AddRange(CreateProperty(item)); } result.AddRange(CreateCopyProperties(type)); foreach (var item in interfaces.Where(e => ContractHelper.HasCopyable(e))) { result.AddRange(CreateCopyProperties(item)); } result.AddRange(CreateFactoryMethods(type, false)); result.Add("}"); result.EnvelopeWithANamespace(CreateModelsNamespace(type), "using System;"); result.FormatCSharpCode(); return(result); }
#pragma warning disable IDE0051 // Remove unused private members private Contracts.IGeneratedItem CreateOverrideToString(Type type, Common.UnitType unitType) #pragma warning restore IDE0051 // Remove unused private members { type.CheckArgument(nameof(type)); var result = new Models.GeneratedItem(unitType, Common.ItemType.Model) { FullName = CreateModelFullNameFromInterface(type), FileExtension = StaticLiterals.CSharpFileExtension, }; result.SubFilePath = $"{result.FullName}PartA{result.FileExtension}"; result.Source.Add($"partial class {CreateModelNameFromInterface(type)}"); result.Source.Add("{"); result.Source.Add("public override string ToString()"); result.Source.Add("{"); result.Source.Add("var result = string.Empty;"); result.Source.Add("var handled = false;"); result.Source.Add("BeforeToString(ref result, ref handled);"); result.Source.Add("if (handled == false)"); result.Source.Add("{"); result.Source.Add("result = base.ToString();"); result.Source.Add("}"); result.Source.Add("AfterToString(ref result);"); result.Source.Add("return result;"); result.Source.Add("}"); result.Source.Add("partial void BeforeToString(ref string result, ref bool handled);"); result.Source.Add("partial void AfterToString(ref string result);"); result.Source.Add("}"); result.EnvelopeWithANamespace(CreateModelsNamespace(type)); result.FormatCSharpCode(); return(result); }
private Contracts.IGeneratedItem CreateWebApiController(Type type) { //var routeBase = $"/api/[controller]"; var entityName = CreateEntityNameFromInterface(type); var subNameSpace = CreateSubNamespaceFromType(type); var contractType = $"Contracts.{subNameSpace}.{type.Name}"; var modelType = $"Transfer.{subNameSpace}.{entityName}"; var controllerName = entityName.EndsWith("s") ? entityName : $"{entityName}s"; var result = new Models.GeneratedItem(Common.UnitType.WebApi, Common.ItemType.WebApiController) { FullName = CreateWebApiControllerFullNameFromInterface(type), FileExtension = StaticLiterals.CSharpFileExtension, }; result.SubFilePath = $"{result.FullName}{result.FileExtension}"; ConvertWebApiControllerName(type, ref controllerName); result.Add("using Microsoft.AspNetCore.Mvc;"); result.Add($"using TContract = {contractType};"); result.Add($"using TModel = {modelType};"); result.Add("[ApiController]"); result.Add("[Route(\"Controller\")]"); CreateWebApiControllerAttributes(type, result.Source); result.Add($"public partial class {controllerName}Controller : GenericController<TContract, TModel>"); result.Add("{"); result.Add("}"); result.EnvelopeWithANamespace(CreateWebApiControllerNameSpace(type)); result.FormatCSharpCode(); return(result); }
private Contracts.IGeneratedItem CreatePersistenceController(Type type) { type.CheckArgument(nameof(type)); var entityName = CreateEntityNameFromInterface(type); var subNameSpace = CreateSubNamespaceFromType(type); var entityType = $"{StaticLiterals.EntitiesFolder}.{subNameSpace}.{entityName}"; var controllerName = $"{entityName}Controller"; var baseControllerName = "GenericPersistenceController"; var controllerAttributes = InitLogicControllerAttributes(type); var result = new Models.GeneratedItem(Common.UnitType.Logic, Common.ItemType.PersistenceController) { FullName = CreateLogicControllerFullNameFromInterface(type), FileExtension = StaticLiterals.CSharpFileExtension, }; result.SubFilePath = $"{result.FullName}{result.FileExtension}"; ConvertGenericPersistenceControllerName(type, ref baseControllerName); CreateLogicControllerAttributes(type, controllerAttributes); result.AddRange(controllerAttributes); result.Add($"sealed partial class {controllerName} : {baseControllerName}<{type.FullName}, {entityType}>"); result.Add("{"); result.AddRange(CreatePartialStaticConstrutor(controllerName)); result.AddRange(CreatePartialConstrutor("internal", controllerName, $"{SolutionProperties.DataContextFolder}.IContext context", "base(context)")); result.AddRange(CreatePartialConstrutor("internal", controllerName, "ControllerObject controller", "base(controller)", null, false)); result.Add("}"); result.EnvelopeWithANamespace(CreateLogicControllerNameSpace(type)); result.FormatCSharpCode(); return(result); }
private Contracts.IGeneratedItem CreateEntityFromContract(Type type, Common.ItemType itemType) { type.CheckArgument(nameof(type)); var baseItfc = GetPersistenceBaseContract(type); var contractHelper = new ContractHelper(type); var result = new Models.GeneratedItem(Common.UnitType.Logic, itemType) { FullName = CreateEntityFullNameFromInterface(type), FileExtension = StaticLiterals.CSharpFileExtension, }; result.SubFilePath = $"{result.FullName}{result.FileExtension}"; CreateEntityAttributes(type, result.Source); result.Add($"partial class {contractHelper.EntityName} : {type.FullName}"); result.Add("{"); result.AddRange(CreatePartialStaticConstrutor(contractHelper.EntityName)); result.AddRange(CreatePartialConstrutor("public", contractHelper.EntityName)); foreach (var item in ContractHelper.GetEntityProperties(type).Where(p => CanCreateProperty(type, p.Name))) { var codeLines = new List <string>(CreateProperty(item)); AfterCreateProperty(type, item, codeLines); result.AddRange(codeLines); } result.AddRange(CreateCopyProperties(type)); result.AddRange(CreateEquals(type)); result.AddRange(CreateGetHashCode(type)); result.AddRange(CreateFactoryMethods(type, baseItfc != null)); result.Add("}"); result.EnvelopeWithANamespace(CreateNameSpace(type), "using System;"); result.FormatCSharpCode(); return(result); }
private Contracts.IGeneratedItem CreateOneToManyBusinessController(Type type) { type.CheckArgument(nameof(type)); var entityName = CreateEntityNameFromInterface(type); var subNameSpace = CreateSubNamespaceFromType(type); var entityType = $"{StaticLiterals.EntitiesFolder}.{subNameSpace}.{entityName}"; var controllerName = $"{entityName}Controller"; var baseControllerName = "GenericOneToManyController"; var interfaceTypes = type.GetInterfaces(); var oneGenericType = interfaceTypes[0].GetGenericArguments()[0]; var manyGenericType = interfaceTypes[0].GetGenericArguments()[1]; var oneEntityType = $"{CreateEntityFullNameFromInterface(oneGenericType)}"; var manyEntityType = $"{CreateEntityFullNameFromInterface(manyGenericType)}"; var oneCtrlType = $"{CreateLogicControllerFullNameFromInterface(oneGenericType)}"; var manyCtrlType = $"{CreateLogicControllerFullNameFromInterface(manyGenericType)}"; var result = new Models.GeneratedItem(Common.UnitType.Logic, Common.ItemType.BusinessController) { FullName = CreateLogicControllerFullNameFromInterface(type), FileExtension = StaticLiterals.CSharpFileExtension, }; result.SubFilePath = $"{result.FullName}{result.FileExtension}"; ConvertGenericPersistenceControllerName(type, ref baseControllerName); CreateLogicControllerAttributes(type, result.Source); result.Add($"sealed partial class {controllerName} : {baseControllerName}<{type.FullName}, {entityType}, {oneGenericType.FullName}, {oneEntityType}, {manyGenericType.FullName}, {manyEntityType}>"); result.Add("{"); var initStatements = new List <string>() { $"oneEntityController = new {oneCtrlType}(this);", $"manyEntityController = new {manyCtrlType}(this);", }; result.AddRange(CreatePartialStaticConstrutor(controllerName)); result.AddRange(CreatePartialConstrutor("public", controllerName, $"{SolutionProperties.DataContextFolder}.IContext context", "base(context)", initStatements)); result.AddRange(CreatePartialConstrutor("public", controllerName, "ControllerObject controller", "base(controller)", initStatements, false)); result.Add($"private {oneCtrlType} oneEntityController = null;"); result.Add($"protected override GenericController<{oneGenericType.FullName}, {oneEntityType}> OneEntityController"); result.Add("{"); result.Add($"get => oneEntityController; // ?? (oneEntityController = new {oneCtrlType}(this));"); result.Add($"set => oneEntityController = value as {oneCtrlType};"); result.Add("}"); result.Add($"private {manyCtrlType} manyEntityController = null;"); result.Add($"protected override GenericController<{manyGenericType.FullName}, {manyEntityType}> ManyEntityController"); result.Add("{"); result.Add($"get => manyEntityController; // ?? (manyEntityController = new {manyCtrlType}(this));"); result.Add($"set => manyEntityController = value as {manyCtrlType};"); result.Add("}"); result.Add("}"); result.EnvelopeWithANamespace(CreateLogicControllerNameSpace(type)); result.FormatCSharpCode(); return(result); }
private Contracts.IGeneratedItem CreateBusinessModel(Type type, Common.UnitType unitType) { type.CheckArgument(nameof(type)); var result = new Models.GeneratedItem(unitType, Common.ItemType.BusinessModel) { FullName = CreateModelFullNameFromInterface(type), FileExtension = StaticLiterals.CSharpFileExtension, }; result.SubFilePath = $"{result.FullName}PartA{result.FileExtension}"; result.Source.Add($"partial class {CreateModelNameFromInterface(type)} : {GetBaseClassByContract(type)}"); result.Source.Add("{"); result.Source.Add("}"); result.EnvelopeWithANamespace(CreateModelsNamespace(type)); result.FormatCSharpCode(); return(result); }
private Contracts.IGeneratedItem CreateDelegateProperties(Type type, Type delegateType, string delegateObjectName, Common.UnitType unitType, Common.ItemType itemType) { type.CheckArgument(nameof(type)); delegateType.CheckArgument(nameof(delegateType)); var modelName = CreateModelNameFromInterface(type); var typeProperties = ContractHelper.GetAllProperties(delegateType); var result = new Models.GeneratedItem(unitType, itemType) { FullName = CreateModelFullNameFromInterface(type), FileExtension = StaticLiterals.CSharpFileExtension, }; result.SubFilePath = $"{result.FullName}{result.FileExtension}"; CreateModelAttributes(type, result.Source); result.Add($"public partial class {modelName}"); result.Add("{"); foreach (var item in ContractHelper.FilterPropertiesForGeneration(typeProperties)) { var propertyType = GetPropertyType(item); if (item.CanRead || item.CanWrite) { CreateModelPropertyAttributes(delegateType, item, result.Source); result.Add($"public {propertyType} {item.Name}"); result.Add("{"); if (item.CanRead) { result.Add($"get => {delegateObjectName}.{item.Name};"); } if (item.CanWrite) { result.Add($"set => {delegateObjectName}.{item.Name} = value;"); } result.Add("}"); } } result.Add("}"); result.EnvelopeWithANamespace(CreateModelsNamespace(type), "using System;"); result.FormatCSharpCode(); return(result); }
/// <summary> /// Diese Methode erstellt den Programmcode der Beziehungen zwischen den Entitaeten aus den Schnittstellen-Typen. /// </summary> /// <param name="type">Der Schnittstellen-Typ.</param> /// <param name="types">Die Schnittstellen-Typen.</param> /// <param name="mapPropertyName">Ein Lambda-Ausdruck zum konvertieren des Eigenschaftsnamen.</param> /// <returns>Die Entitaet als Text.</returns> private Contracts.IGeneratedItem CreateEntityToEntityFromContracts(Type type, IEnumerable <Type> types) { type.CheckArgument(nameof(type)); types.CheckArgument(nameof(types)); var typeName = CreateEntityNameFromInterface(type); var result = new Models.GeneratedItem(Common.UnitType.Logic, Common.ItemType.PersistenceEntity) { FullName = CreateEntityFullNameFromInterface(type), FileExtension = StaticLiterals.CSharpFileExtension, }; result.SubFilePath = $"{result.FullName}PartB{result.FileExtension}"; result.Add($"partial class {typeName}"); result.Add("{"); foreach (var other in types) { var otherHelper = new ContractHelper(other); if (otherHelper.ContextType != CommonBase.Attributes.ContextType.View) { var otherName = GeneratorObject.CreateEntityNameFromInterface(other); foreach (var pi in other.GetProperties()) { if (pi.Name.Equals($"{typeName}Id")) { var otherFullName = GeneratorObject.CreateEntityFullNameFromInterface(other); var navigationName = $"{otherName}s"; result.Add(($"public System.Collections.Generic.ICollection<{otherFullName}> {navigationName} " + "{ get; set; }")); } } } } var interfaces = new List <Type>(); var typeInterface = GetTypeInterface(type); interfaces.Add(type); foreach (var item in GeneratorObject.GetInterfaces(type) .Where(t => t != typeInterface)) { interfaces.Add(item); } foreach (var item in interfaces) { foreach (var pi in item.GetProperties()) { foreach (var other in types) { var otherHelper = new ContractHelper(other); if (otherHelper.ContextType != CommonBase.Attributes.ContextType.View) { var otherName = GeneratorObject.CreateEntityNameFromInterface(other); if (pi.Name.Equals($"{otherName}Id")) { var propHelper = new ContractPropertyHelper(pi); var otherFullName = GeneratorObject.CreateEntityFullNameFromInterface(other); var navigationName = propHelper.NavigationName.GetValueOrDefault(otherName); result.Add(($"[System.ComponentModel.DataAnnotations.Schema.ForeignKey(\"{pi.Name}\")]")); result.Add(($"public {otherFullName} {navigationName} " + "{ get; set; }")); } else if (pi.Name.StartsWith($"{otherName}Id_")) { var data = pi.Name.Split("_"); if (data.Length == 2) { var otherFullName = GeneratorObject.CreateEntityFullNameFromInterface(other); result.Add(($"[System.ComponentModel.DataAnnotations.Schema.ForeignKey(\"{pi.Name}\")]")); result.Add(($"public {otherFullName} {data[1]} " + "{ get; set; }")); } } } } } } result.Add("}"); result.EnvelopeWithANamespace(CreateNameSpace(type)); result.FormatCSharpCode(); return(result); }