private static void WriteInterfaceDeclaration(FormattedTextWriter writer, SqlServerVersion version, ModelTypeClass type, string interfaceName, Element element)
 {
     writer.WriteLine();
     writer.WriteLine($"public interface {interfaceName} : {DacUtilities.BaseModelInterfaceFullName}");
     using (writer.StartBlock())
     {
         foreach (ModelPropertyClass property in GetProperties(type))
         {
             if (!DacUtilities.SupportsVersion(property.SupportedPlatforms, version))
             {
                 continue;
             }
             bool   useGenericGetter;
             string typeName     = GetPropertyTypeName(property.DataType, out useGenericGetter);
             string propertyName = GetPropertyName(property, element);
             writer.WriteLine();
             writer.WriteLine($"{typeName} {propertyName} {{ get; }}");
         }
     }
 }
        public static void GenerateVersionSpecificClasses(FormattedTextWriter writer)
        {
            writer.WriteFileHeader();
            writer.WriteLine($"namespace {DacUtilities.NamespaceName}");
            using (writer.StartBlock())
            {
                foreach (SqlServerVersion version in DacUtilities.GetSqlServerVersions())
                {
                    string interfacePrefix = GetInterfacePrefix(version);

                    foreach (ModelTypeClass type in ModelSchema.SchemaInstance.AllTypes)
                    {
                        string  interfaceName          = GetBaseInterfaceName(type, version);
                        string  referenceInterfaceName = GetReferenceInterfaceName(type, version);
                        Element element = DacUtilities.FindElement(type.Name);
                        if (element != null)
                        {
                            writer.WriteLine();
                            writer.WriteLine("/// <summary>");
                            writer.WriteLine($"/// Explicit implementation of <see cref=\"{interfaceName}\"/>.");
                            writer.WriteLine("/// </summary>");
                            writer.WriteLine($"public partial class {DacUtilities.ClassNamePrefix + type.Name} : {interfaceName}");
                            using (writer.StartBlock())
                            {
                                foreach (ModelPropertyClass property in GetProperties(type))
                                {
                                    if (!DacUtilities.SupportsVersion(property.SupportedPlatforms, version))
                                    {
                                        continue;
                                    }
                                    bool   useGenericGetter;
                                    string typeName     = GetPropertyTypeName(property.DataType, out useGenericGetter);
                                    string propertyName = GetPropertyName(property, element);
                                    writer.WriteLine();
                                    writer.WriteLine($"{typeName} {interfaceName}.{propertyName} => {propertyName};");
                                }
                                foreach (var relationship in type.Relationships.OrderBy(r => r.Name))
                                {
                                    if (!!DacUtilities.SupportsVersion(relationship.SupportedPlatforms, version))
                                    {
                                        continue;
                                    }
                                    string returnType     = DacUtilities.DefaultReturnType;
                                    string castExpression = "";
                                    if (element != null)
                                    {
                                        Relationship localoverride;
                                        if (element.Children.OfType <Relationship>().ToDictionary(r => r.Name, r => r).TryGetValue(relationship.Name, out localoverride))
                                        {
                                            if (localoverride.Specialize)
                                            {
                                                returnType     = $"{localoverride.ReturnTypeNamespace}.{interfacePrefix}{localoverride.ReturnType}";
                                                castExpression = $".Cast<{returnType}>()";
                                            }
                                            else
                                            {
                                                returnType = $"{localoverride.ReturnTypeNamespace}.{localoverride.ReturnType}";
                                            }
                                        }
                                    }
                                    writer.WriteLine();
                                    writer.WriteLine($"// {relationship.Type} relationship");
                                    writer.WriteLine($"{IEnumerableName}<{returnType}> {interfaceName}.{relationship.Name} => {relationship.Name + castExpression};");
                                }
                            }
                        }
                    }
                }
            }
        }
        public static void GenerateInterfaces(FormattedTextWriter writer)
        {
            writer.WriteFileHeader();
            writer.WriteLine($"namespace {DacUtilities.NamespaceName}");
            using (writer.StartBlock())
            {
                using (writer.BeginRegion(DacUtilities.BaseModelInterfaceName))
                {
                    writer.WriteLine($"public interface {DacUtilities.BaseModelInterfaceName}");
                    using (writer.StartBlock())
                    {
                        writer.WriteLine($"{ObjectIdentifierName} Name {{ get; }}");
                        writer.WriteLine($"{TSqlObjectName} Element {{ get; }}");
                        writer.WriteLine($"{TSqlScriptName} GetAst();");
                        writer.WriteLine($"{IEnumerableName}<{TSqlObjectName}> GetChildren();");
                        writer.WriteLine($"{IEnumerableName}<{TSqlObjectName}> GetChildren({DacQueryScopesName} queryScopes);");
                        writer.WriteLine($"object GetMetadata({ModelMetadataClassName} metadata);");
                        writer.WriteLine($"TMetadata GetMetadata<TMetadata>({ModelMetadataClassName} metadata);");
                        writer.WriteLine($"{TSqlObjectName} GetParent();");
                        writer.WriteLine($"{TSqlObjectName} GetParent({DacQueryScopesName} queryScopes);");
                        writer.WriteLine($"object GetProperty({ModelPropertyClassName} property);");
                        writer.WriteLine($"TProperty GetProperty<TProperty>({ModelPropertyClassName} property);");
                        writer.WriteLine($"{IEnumerableName}<{TSqlObjectName}> GetReferenced();");
                        writer.WriteLine($"{IEnumerableName}<{TSqlObjectName}> GetReferenced({DacQueryScopesName} queryScopes);");
                        writer.WriteLine($"{IEnumerableName}<{TSqlObjectName}> GetReferenced({ModelRelationshipClassName} relationshipType);");
                        writer.WriteLine($"{IEnumerableName}<{TSqlObjectName}> GetReferenced({ModelRelationshipClassName} relationshipType, {DacQueryScopesName} queryScopes);");
                        writer.WriteLine($"{IEnumerableName}<{ModelRelationshipInstanceName}> GetReferencedRelationshipInstances();");
                        writer.WriteLine($"{IEnumerableName}<{ModelRelationshipInstanceName}> GetReferencedRelationshipInstances({DacExternalQueryScopesName} queryScopes);");
                        writer.WriteLine($"{IEnumerableName}<{ModelRelationshipInstanceName}> GetReferencedRelationshipInstances({DacQueryScopesName} queryScopes);");
                        writer.WriteLine($"{IEnumerableName}<{ModelRelationshipInstanceName}> GetReferencedRelationshipInstances({ModelRelationshipClassName} relationshipType);");
                        writer.WriteLine($"{IEnumerableName}<{ModelRelationshipInstanceName}> GetReferencedRelationshipInstances({ModelRelationshipClassName} relationshipType, {DacExternalQueryScopesName} queryScopes);");
                        writer.WriteLine($"{IEnumerableName}<{ModelRelationshipInstanceName}> GetReferencedRelationshipInstances({ModelRelationshipClassName} relationshipType, {DacQueryScopesName} queryScopes);");
                        writer.WriteLine($"{IEnumerableName}<{TSqlObjectName}> GetReferencing();");
                        writer.WriteLine($"{IEnumerableName}<{TSqlObjectName}> GetReferencing({DacQueryScopesName} queryScopes);");
                        writer.WriteLine($"{IEnumerableName}<{TSqlObjectName}> GetReferencing({ModelRelationshipClassName} relationshipType);");
                        writer.WriteLine($"{IEnumerableName}<{TSqlObjectName}> GetReferencing({ModelRelationshipClassName} relationshipType, {DacQueryScopesName} queryScopes);");
                        writer.WriteLine($"{IEnumerableName}<{ModelRelationshipInstanceName}> GetReferencingRelationshipInstances();");
                        writer.WriteLine($"{IEnumerableName}<{ModelRelationshipInstanceName}> GetReferencingRelationshipInstances({DacQueryScopesName} queryScopes);");
                        writer.WriteLine($"{IEnumerableName}<{ModelRelationshipInstanceName}> GetReferencingRelationshipInstances({ModelRelationshipClassName} relationshipType);");
                        writer.WriteLine($"{IEnumerableName}<{ModelRelationshipInstanceName}> GetReferencingRelationshipInstances({ModelRelationshipClassName} relationshipType, {DacQueryScopesName} queryScopes);");
                        writer.WriteLine($"string GetScript();");
                        writer.WriteLine($"{ModelTypeClassName} ObjectType {{ get; }}");
                        writer.WriteLine($"object this[{ModelPropertyClassName} property] {{ get; }}");
                        writer.WriteLine($"bool TryGetAst(out {TSqlScriptName} objectAst);");
                        writer.WriteLine($"bool TryGetScript(out string objectScript);");
                    }
                }

                writer.WriteLine();

                writer.WriteLine($"public interface {DacUtilities.BaseModelInterfaceReferenceName} : {DacUtilities.BaseModelInterfaceName}");
                using (writer.StartBlock())
                {
                    writer.WriteLine($"TMetadataProperty GetMetadataProperty<TMetadataProperty>({ModelMetadataPropertyName} property);");
                }

                writer.WriteLine();

                WriteSimpleInterface(writer, "ISqlSecurityPrincipal", DacUtilities.BaseModelInterfaceName);
                WriteSimpleInterface(writer, "IServerSecurityPrincipal", "ISqlSecurityPrincipal");
                WriteSimpleInterface(writer, "ISqlDatabaseSecurityPrincipal", "ISqlSecurityPrincipal", "ISqlObjectAuthorizer");

                writer.WriteLine("");
                writer.WriteLine("public sealed class UnresolvedISqlDatabaseSecurityPrincipalElement : TSqlModelElementReference, ISqlDatabaseSecurityPrincipal");
                using (writer.StartBlock())
                {
                    writer.WriteLine($"public UnresolvedISqlDatabaseSecurityPrincipalElement({ModelRelationshipInstanceName} relationshipReference) : base(relationshipReference)");
                    using (writer.StartBlock())
                    {
                    }
                }

                WriteSimpleInterface(writer, "ISqlSecurable", DacUtilities.BaseModelInterfaceName);

                writer.WriteLine();
                writer.WriteLine("public sealed class UnresolvedISqlSecurableElement : TSqlModelElementReference, ISqlSecurable");
                using (writer.StartBlock())
                {
                    writer.WriteLine($"public UnresolvedISqlSecurableElement({ModelRelationshipInstanceName} relationshipReference):base(relationshipReference)");
                    using (writer.StartBlock())
                    {
                    }
                }

                writer.WriteLine("    }");
                writer.WriteLine("");
                writer.WriteLine($"public interface ISpecifiesIndex : {DacUtilities.BaseModelInterfaceName}");
                using (writer.StartBlock())
                {
                    writer.WriteLine($"{IEnumerableName}<ISqlIndex> Indexes {{ get; }}");
                }

                writer.WriteLine();

                writer.WriteLine($"public sealed class UnresolvedISpecifiesIndexElement: TSqlModelElementReference, ISpecifiesIndex");
                using (writer.StartBlock())
                {
                    writer.WriteLine($"public UnresolvedISpecifiesIndexElement({ModelRelationshipInstanceName} instance) : base(instance)");
                    using (writer.StartBlock())
                    {
                    }

                    writer.WriteLine();
                    writer.WriteLine($"public {IEnumerableName}<ISqlIndex> Indexes => throw new System.InvalidOperationException();");
                }

                WriteAdvancedInterface(writer, "TSqlDmlTrigger", "Triggers", "ISpecifiesDmlTrigger", DacUtilities.BaseModelInterfaceName);
                WriteAdvancedInterface(writer, "ISqlColumn", "Columns", "ISqlColumnSource", DacUtilities.BaseModelInterfaceName);

                WriteSimpleInterface(writer, "ISqlPromotedNodePath", DacUtilities.BaseModelInterfaceName);

                writer.WriteLine("");
                writer.WriteLine("public sealed class UnresolvedISqlPromotedNodePathElement : TSqlModelElementReference, ISqlPromotedNodePath");
                using (writer.StartBlock())
                {
                    writer.WriteLine($"public UnresolvedISqlPromotedNodePathElement({ModelRelationshipInstanceName} relationshipReference) : base(relationshipReference)");
                    using (writer.StartBlock())
                    {
                    }
                }

                WriteSimpleInterface(writer, "ISqlIndex", DacUtilities.BaseModelInterfaceName);
                WriteSimpleInterface(writer, "ITableTypeConstraint", DacUtilities.BaseModelInterfaceName);
                WriteSimpleInterface(writer, "IProtocolSpecifier", DacUtilities.BaseModelInterfaceName);
                WriteSimpleInterface(writer, "IEndpointLanguageSpecifier", DacUtilities.BaseModelInterfaceName);
                WriteSimpleInterface(writer, "IExtendedPropertyHost", DacUtilities.BaseModelInterfaceName);
                WriteSimpleInterface(writer, "ISqlIndex", DacUtilities.BaseModelInterfaceName);
                WriteSimpleInterface(writer, "ISqlObjectAuthorizer", DacUtilities.BaseModelInterfaceName);

                writer.WriteLine("");
                writer.WriteLine("public sealed class UnresolvedISqlObjectAuthorizerElement : TSqlModelElementReference, ISqlObjectAuthorizer");
                using (writer.StartBlock())
                {
                    writer.WriteLine($"public UnresolvedISqlObjectAuthorizerElement({ModelRelationshipInstanceName} relationshipReference) : base(relationshipReference)");
                    using (writer.StartBlock())
                    {
                    }
                }

                writer.WriteLine("");
                writer.WriteLine($"public interface ISpecifiesStorage : {DacUtilities.BaseModelInterfaceName}");
                using (writer.StartBlock())
                {
                    writer.WriteLine($"{IEnumerableName}<TSqlDataCompressionOption> DataCompressionOptions {{ get; }}");
                }

                writer.WriteLine($"public interface ISqlDataType: {DacUtilities.BaseModelInterfaceName}");
                using (writer.StartBlock())
                {
                }
                writer.WriteLine();
                writer.WriteLine("public sealed class UnresolvedISqlDataTypeElement : TSqlModelElementReference, ISqlDataType");
                using (writer.StartBlock())
                {
                    writer.WriteLine($"public UnresolvedISqlDataTypeElement({ModelRelationshipInstanceName} relationshipReference) : base(relationshipReference)");
                    using (writer.StartBlock())
                    {
                    }
                }

                foreach (SqlServerVersion version in DacUtilities.GetSqlServerVersions())
                {
                    string interfacePrefix = GetInterfacePrefix(version);

                    foreach (ModelTypeClass type in ModelSchema.SchemaInstance.AllTypes)
                    {
                        string  interfaceName          = GetBaseInterfaceName(type, version);
                        string  referenceInterfaceName = GetReferenceInterfaceName(type, version);
                        Element element = DacUtilities.FindElement(type.Name);
                        if (element != null)
                        {
                            WriteInterfaceDeclaration(writer, version, type, interfaceName, element);
                        }
                    }
                }
            }
        }