Ejemplo n.º 1
0
        /// <summary>
        /// Génération de la déclaration de la classe.
        /// </summary>
        /// <param name="w">Writer</param>
        /// <param name="item">Classe à générer.</param>
        private void GenerateClassDeclaration(CSharpWriter w, ModelClass item)
        {
            if (item.Stereotype == Stereotype.Reference)
            {
                w.WriteAttribute(1, "Reference");
            }
            else if (item.Stereotype == Stereotype.Statique)
            {
                w.WriteAttribute(1, "Reference", "true");
            }

            if (!string.IsNullOrEmpty(item.DefaultProperty))
            {
                w.WriteAttribute(1, "DefaultProperty", $@"""{item.DefaultProperty}""");
            }

            if (item.DataContract.IsPersistent && !item.IsView)
            {
                if (_parameters.DbSchema != null)
                {
                    w.WriteAttribute(1, "Table", $@"""{item.DataContract.Name}""", $@"Schema = ""{_parameters.DbSchema}""");
                }
                else
                {
                    w.WriteAttribute(1, "Table", $@"""{item.DataContract.Name}""");
                }
            }

            w.WriteClassDeclaration(item.Name, item.ParentClass?.Name, new List <string>());

            GenerateConstProperties(w, item);
            GenerateConstructors(w, item);
            GenerateProperties(w, item);
            GenerateExtensibilityMethods(w, item);
            w.WriteLine(1, "}");

            if (_parameters.UseTypeSafeConstValues.Value)
            {
                GenerateConstPropertiesClass(w, item);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Génère la propriété concernée.
        /// </summary>
        /// <param name="w">Writer.</param>
        /// <param name="property">La propriété générée.</param>
        private void GenerateProperty(CSharpWriter w, ModelProperty property)
        {
            w.WriteSummary(2, property.Comment);

            if (!property.Class.IsView && property.IsPersistent && property.DataMember != null && (!_parameters.NoColumnOnAlias.Value || property.Class.Trigram != null))
            {
                if (property.DataDescription.Domain.PersistentDataType.Contains("json"))
                {
                    w.WriteAttribute(2, "Column", $@"""{property.DataMember.Name}""", $@"TypeName = ""{property.DataDescription.Domain.PersistentDataType}""");
                }
                else
                {
                    w.WriteAttribute(2, "Column", $@"""{property.DataMember.Name}""");
                }
            }

            if (property.DataMember.IsRequired && !property.DataDescription.IsPrimaryKey)
            {
                w.WriteAttribute(2, "Required");
            }

            if (property.DataDescription != null)
            {
                if (!string.IsNullOrEmpty(property.DataDescription.ReferenceType) && !property.DataDescription.ReferenceClass.IsExternal)
                {
                    w.WriteAttribute(2, "ReferencedType", $"typeof({property.DataDescription.ReferenceClass.Name})");
                }

                if (property.DataDescription.Domain != null)
                {
                    w.WriteAttribute(2, "Domain", $@"""{property.DataDescription.Domain.Code}""");

                    if (!string.IsNullOrEmpty(property.DataDescription.Domain.CustomAnnotation))
                    {
                        w.WriteLine(2, property.DataDescription.Domain.CustomAnnotation);
                    }
                }
            }

            if (property.DataDescription.IsPrimaryKey)
            {
                w.WriteAttribute(2, "Key");
                if (property.IsIdManuallySet)
                {
                    w.WriteAttribute(2, "DatabaseGenerated", "DatabaseGeneratedOption.None");
                }
            }
            else if (property.DefaultValue != null)
            {
                w.WriteAttribute(2, "DatabaseGenerated", "DatabaseGeneratedOption.Identity");
            }

            if (!property.IsPrimitive)
            {
                w.WriteAttribute(2, "NotMapped");
            }

            string @override = property.IsDerived ? "override " : "";

            w.WriteLine(2, $"public {@override}{LoadShortDataType(property.DataType)} {property.Name} {{ get; set; }}");
        }
        /// <summary>
        /// Génération de la déclaration de la classe.
        /// </summary>
        /// <param name="w">Writer</param>
        /// <param name="item">Classe à générer.</param>
        private void GenerateClassDeclaration(CSharpWriter w, ModelClass item)
        {
            if (item.Stereotype == Stereotype.Reference)
            {
                w.WriteAttribute(1, "Reference");
            }
            else if (item.Stereotype == Stereotype.Statique)
            {
                w.WriteAttribute(1, "Reference", "true");
            }

            if (!string.IsNullOrEmpty(item.DefaultProperty))
            {
                w.WriteAttribute(1, "DefaultProperty", $@"""{item.DefaultProperty}""");
            }

            if (item.DataContract.IsPersistent && !item.IsView)
            {
                if (_parameters.DbSchema != null)
                {
                    w.WriteAttribute(1, "Table", $@"""{item.DataContract.Name}""", $@"Schema = ""{_parameters.DbSchema}""");
                }
                else
                {
                    w.WriteAttribute(1, "Table", $@"""{item.DataContract.Name}""");
                }
            }

            ICollection <string> interfaces = new List <string>();

            if (_parameters.IsWithEntityInterface && item.DataContract.IsPersistent)
            {
                if (item.HasPrimaryKey && item.PrimaryKey.Count == 1)
                {
                    string name = item.PrimaryKey.First().Name;
                    string type = item.PrimaryKey.First().DataType;

                    if (name == "Id" && type == "int?")
                    {
                        interfaces.Add("IIdEntity");
                    }
                    else if (name == "Code" && type == "string")
                    {
                        interfaces.Add("ICodeEntity");
                    }
                }

                interfaces.Add("IEntity");
            }

            w.WriteClassDeclaration(item.Name, item.ParentClass?.Name, interfaces);

            GenerateConstProperties(w, item);
            GenerateConstructors(w, item);
            GenerateProperties(w, item);
            GenerateExtensibilityMethods(w, item);
            w.WriteLine(1, "}");

            if (_parameters.UseTypeSafeConstValues.Value)
            {
                GenerateConstPropertiesClass(w, item);
            }
        }