예제 #1
0
        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);
        }
        public static IEnumerable <string> CreateTypeImports(Type type)
        {
            type.CheckArgument(nameof(type));

            var result     = new List <string>();
            var properties = ContractHelper.GetAllProperties(type);
            var entityName = CreateEntityNameFromInterface(type);

            foreach (var propertyInfo in properties)
            {
                if (propertyInfo.PropertyType.IsEnum)
                {
                    var typeName = $"{propertyInfo.PropertyType.Name}";

                    if (typeName.Equals(entityName) == false)
                    {
                        var subPath = GeneratorObject.CreateSubPathFromType(propertyInfo.PropertyType).ToLower();

                        result.Add(CreateImport(typeName, subPath));
                    }
                }
                else if (propertyInfo.PropertyType.IsGenericType)
                {
                    Type subType = propertyInfo.PropertyType.GetGenericArguments().First();

                    if (subType.IsInterface)
                    {
                        var typeName = subType.Name[1..];
예제 #3
0
        public static IEnumerable <string> CreateModelFromInterface(Type type,
                                                                    Action <Type, List <string> > createAttributes = null,
                                                                    Action <Type, PropertyInfo, List <string> > createPropertyAttributes = null)
        {
            type.CheckArgument(nameof(type));

            var result     = new List <string>();
            var entityName = CreateEntityNameFromInterface(type);
            var properties = ContractHelper.GetAllProperties(type);

            createAttributes?.Invoke(type, result);
            result.Add($"public partial class {entityName} : {type.FullName}");
            result.Add("{");
            result.AddRange(CreatePartialStaticConstrutor(entityName));
            result.AddRange(CreatePartialConstrutor("public", entityName));
            foreach (var item in ContractHelper.FilterPropertiesForGeneration(properties))
            {
                createPropertyAttributes?.Invoke(type, item, result);
                result.AddRange(CreateProperty(item));
            }
            result.AddRange(CreateCopyProperties(type));
            result.AddRange(CreateFactoryMethods(type, false));
            result.Add("}");
            return(result);
        }
        public Contracts.IGeneratedItem CreateContract(Type type, IEnumerable <Type> types)
        {
            type.CheckArgument(nameof(type));

            var subPath             = CreateSubPathFromType(type);
            var entityName          = CreateEntityNameFromInterface(type);
            var fileName            = $"{ConvertFileName(entityName)}.{CodeExtension}";
            var properties          = ContractHelper.GetAllProperties(type);
            var declarationTypeName = string.Empty;
            var result = new Models.GeneratedItem(Common.UnitType.AngularApp, Common.ItemType.TypeScriptContract)
            {
                FullName      = CreateTypeScriptFullName(type),
                FileExtension = CodeExtension,
            };

            result.SubFilePath = Path.Combine(ProjectContractsPath, subPath, fileName);

            StartCreateContract(type, result.Source);
            result.Add($"export interface {entityName}" + " {");

            foreach (var item in properties)
            {
                if (declarationTypeName.Equals(item.DeclaringType.Name) == false)
                {
                    declarationTypeName = item.DeclaringType.Name;
                    result.Add($"/** {declarationTypeName} **/");
                }

                result.AddRange(CreateTypeScriptProperty(item));
            }
            result.AddRange(CreateContactToContractFromContracts(type, types));
            result.Add("}");
            result.FormatCSharpCode();
            result.Source.Insert(result.Source.Count - 1, StaticLiterals.AngularCustomCodeBeginLabel);
            result.Source.Insert(result.Source.Count - 1, StaticLiterals.AngularCustomCodeEndLabel);

            var imports = new List <string>();

            imports.AddRange(CreateTypeImports(type));
            imports.AddRange(CreateContactToContractImports(type, types));
            imports.Add(StaticLiterals.AngularCustomImportBeginLabel);
            imports.Add(StaticLiterals.AngularCustomImportEndLabel);

            InsertTypeImports(imports, result.Source);

            FinishCreateContract(type, result.Source);
            return(result);
        }
예제 #5
0
        /// <summary>
        /// Diese Methode erstellt den Programmcode fuer das Vergleichen der Eigenschaften.
        /// </summary>
        /// <param name="type">Die Schnittstellen-Typ Information.</param>
        /// <returns>Die Equals-Methode als Text.</returns>
        internal static IEnumerable <string> CreateEquals(Type type)
        {
            type.CheckArgument(nameof(type));

            var result             = new List <string>();
            var counter            = 0;
            var properties         = ContractHelper.GetAllProperties(type);
            var filteredProperties = ContractHelper.FilterPropertiesForGeneration(properties);

            if (filteredProperties.Any())
            {
                result.Add($"public override bool Equals(object obj)");
                result.Add("{");
                result.Add($"if (obj is not {type.FullName} instance)");
                result.Add("{");
                result.Add("return false;");
                result.Add("}");
                result.Add("return base.Equals(instance) && Equals(instance);");
                result.Add("}");
                result.Add(string.Empty);
                result.Add($"protected bool Equals({type.FullName} other)");
                result.Add("{");
                result.Add("if (other == null)");
                result.Add("{");
                result.Add("return false;");
                result.Add("}");

                foreach (var pi in filteredProperties)
                {
                    if (pi.CanRead)
                    {
                        var codeLine = counter == 0 ? "return " : "       && ";

                        if (pi.PropertyType.IsValueType)
                        {
                            codeLine += $"{pi.Name} == other.{pi.Name}";
                        }
                        else
                        {
                            codeLine += $"IsEqualsWith({pi.Name}, other.{pi.Name})";
                        }
                        result.Add(codeLine);
                        counter++;
                    }
                }
                if (counter > 0)
                {
                    result[^ 1] = $"{result[^1]};";
예제 #6
0
        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);
        }
예제 #7
0
        internal static IEnumerable <string> CreateCopyProperties(Type type, string copyType)
        {
            type.CheckArgument(nameof(type));
            copyType.CheckArgument(nameof(copyType));

            var result = new List <string>
            {
                $"public void CopyProperties({copyType} other)",
                "{",
                "if (other == null)",
                "{",
                "throw new System.ArgumentNullException(nameof(other));",
                "}",
                string.Empty,
                "bool handled = false;",
                "BeforeCopyProperties(other, ref handled);",
                "if (handled == false)",
                "{"
            };

            foreach (var item in ContractHelper.GetAllProperties(type))
            {
                var contractPropertyHelper = new ContractPropertyHelper(item);

                if (contractPropertyHelper.HasImplementation == false)
                {
                    if (item.Name.Equals(StaticLiterals.ConnectorItemName) && item.DeclaringType.Name.Equals(StaticLiterals.ICompositeName))
                    {
                        result.Add($"{StaticLiterals.ConnectorItemName}.CopyProperties(other.{StaticLiterals.ConnectorItemName});");
                    }
                    else if (item.Name.Equals(StaticLiterals.OneItemName) && item.DeclaringType.Name.Equals(StaticLiterals.ICompositeName))
                    {
                        result.Add($"{StaticLiterals.OneItemName}.CopyProperties(other.{StaticLiterals.OneItemName});");
                    }
                    else if (item.Name.Equals(StaticLiterals.AnotherItemName) && item.DeclaringType.Name.Equals(StaticLiterals.ICompositeName))
                    {
                        result.Add($"{StaticLiterals.AnotherItemName}.CopyProperties(other.{StaticLiterals.AnotherItemName});");
                    }
                    else if (item.Name.Equals(StaticLiterals.OneItemName) && item.DeclaringType.Name.Equals(StaticLiterals.IOneToAnotherName))
                    {
                        result.Add($"{StaticLiterals.OneItemName}.CopyProperties(other.{StaticLiterals.OneItemName});");
                    }
                    else if (item.Name.Equals(StaticLiterals.AnotherItemName) && item.DeclaringType.Name.Equals(StaticLiterals.IOneToAnotherName))
                    {
                        result.Add($"{StaticLiterals.AnotherItemName}.CopyProperties(other.{StaticLiterals.AnotherItemName});");
                    }
                    else if (item.Name.Equals(StaticLiterals.OneItemName) && item.DeclaringType.Name.Equals(StaticLiterals.IOneToManyName))
                    {
                        result.Add($"{StaticLiterals.OneItemName}.CopyProperties(other.{StaticLiterals.OneItemName});");
                    }
                    else if (item.Name.Equals(StaticLiterals.ManyItemsName) && item.DeclaringType.Name.Equals(StaticLiterals.IOneToManyName))
                    {
                        result.Add($"Clear{StaticLiterals.ManyItemsName}();");
                        result.Add($"foreach (var item in other.{StaticLiterals.ManyItemsName})");
                        result.Add("{");
                        result.Add($"Add{StaticLiterals.ManyItemName}(item);");
                        result.Add("}");
                    }
                    else if (item.CanRead)
                    {
                        result.Add($"{item.Name} = other.{item.Name};");
                    }
                }
            }
            result.Add("}");
            result.Add("AfterCopyProperties(other);");
            result.Add("}");

            result.Add($"partial void BeforeCopyProperties({copyType} other, ref bool handled);");
            result.Add($"partial void AfterCopyProperties({copyType} other);");

            return(result);
        }