From 6c8518df23af0c4bed2afbb8ef2830aceb6911f1 Mon Sep 17 00:00:00 2001 From: Ilan Uzan Date: Tue, 2 Sep 2025 23:48:28 +0200 Subject: [PATCH] fix: refactor enum extensions members --- CodeGenerator/CodeGenerator.cs | 3 +- CodeGenerator/Generators/EnumsGen.cs | 55 ++++----------- Drivers/DbDriver.cs | 42 +++--------- Drivers/EnumDbDriver.cs | 56 ++++++++++++++- Drivers/Generators/CommonGen.cs | 2 +- Drivers/MySqlConnectorDriver.cs | 14 ++++ Drivers/NpgsqlDriver.cs | 23 +++++++ .../MySqlConnectorDapperExample/Models.cs | 68 ------------------- .../Models.cs | 68 ------------------- examples/MySqlConnectorExample/Models.cs | 48 ------------- .../MySqlConnectorLegacyExample/Models.cs | 48 ------------- examples/NpgsqlDapperExample/Models.cs | 20 ------ examples/NpgsqlDapperLegacyExample/Models.cs | 20 ------ examples/NpgsqlExample/Models.cs | 30 +++----- examples/NpgsqlLegacyExample/Models.cs | 30 +++----- 15 files changed, 141 insertions(+), 386 deletions(-) diff --git a/CodeGenerator/CodeGenerator.cs b/CodeGenerator/CodeGenerator.cs index d9b8c664..2b239a03 100644 --- a/CodeGenerator/CodeGenerator.cs +++ b/CodeGenerator/CodeGenerator.cs @@ -115,10 +115,11 @@ public Task Generate(GenerateRequest generateRequest) } }); + var enums = DbDriver is EnumDbDriver enumDbDriver ? enumDbDriver.Enums : []; var files = GetFileQueries() .Select(fq => QueriesGen.GenerateFile(fq.Value, fq.Key)) .AddRangeExcludeNulls([ - ModelsGen.GenerateFile(DbDriver.Tables, DbDriver.Enums), + ModelsGen.GenerateFile(DbDriver.Tables, enums), UtilsGen.GenerateFile() ]) .AddRangeIf([CsprojGen.GenerateFile()], Options.GenerateCsproj); diff --git a/CodeGenerator/Generators/EnumsGen.cs b/CodeGenerator/Generators/EnumsGen.cs index 3e06aa32..7dcfc317 100644 --- a/CodeGenerator/Generators/EnumsGen.cs +++ b/CodeGenerator/Generators/EnumsGen.cs @@ -10,52 +10,27 @@ internal class EnumsGen(DbDriver dbDriver) { public MemberDeclarationSyntax[] Generate(string name, IList possibleValues) { + if (dbDriver is not EnumDbDriver enumDbDriver) + return []; + var enumValuesDef = possibleValues .Select((v, i) => $"{v.ToPascalCase()} = {i + 1}") .JoinByComma(); var enumType = ParseMemberDeclaration($$""" - public enum {{name}} - { - Invalid = 0, // reserved for invalid enum value - {{enumValuesDef}} - } - """)!; - var enumExtensions = ParseMemberDeclaration($$""" - public static class {{name}}Extensions - { - private static readonly Dictionary StringToEnum = new Dictionary() - { - [string.Empty] = {{name}}.Invalid, - {{possibleValues - .Select(v => $"[\"{v}\"] = {name}.{v.ToPascalCase()}") - .JoinByComma()}} - }; - - private static readonly Dictionary<{{name}}, string> EnumToString = new Dictionary<{{name}}, string>() - { - [{{name}}.Invalid] = string.Empty, - {{possibleValues - .Select(v => $"[{name}.{v.ToPascalCase()}] = \"{v}\"") - .JoinByComma()}} - }; - - public static {{name}} To{{name}}(this string me) - { - return StringToEnum[me]; - } + public enum {{name}} + { + Invalid = 0, // reserved for invalid enum value + {{enumValuesDef}} + } + """)!; - public static string Stringify(this {{name}} me) - { - return EnumToString[me]; - } + var classMembers = enumDbDriver.GetEnumExtensionsMembers(name, possibleValues); + if (classMembers.Length == 0) + return [enumType]; - public static HashSet<{{name}}> To{{name}}Set(this string me) - { - return new HashSet<{{name}}>(me.Split(',').ToList().Select(v => StringToEnum[v])); - } - } - """)!; - return [enumType, enumExtensions]; + var enumExtensionsClass = (ClassDeclarationSyntax)ParseMemberDeclaration( + $$"""public static class {{name}}Extensions { }""")!; + return [enumType, enumExtensionsClass.AddMembers(classMembers)]; } } \ No newline at end of file diff --git a/Drivers/DbDriver.cs b/Drivers/DbDriver.cs index f0fe9089..a9c4f59a 100644 --- a/Drivers/DbDriver.cs +++ b/Drivers/DbDriver.cs @@ -19,8 +19,6 @@ public abstract class DbDriver public Dictionary> Tables { get; } - public Dictionary> Enums { get; } - protected IList Queries { get; } private HashSet NullableTypesInDotnetCore { get; } = @@ -31,7 +29,7 @@ public abstract class DbDriver "IPAddress" ]; - private HashSet NullableTypes { get; } = + protected HashSet NullableTypes { get; } = [ "bool", "byte", @@ -79,11 +77,6 @@ protected DbDriver( DefaultSchema = catalog.DefaultSchema; Tables = ConstructTablesLookup(catalog); Queries = queries; - Enums = ConstructEnumsLookup(catalog); - - foreach (var schemaEnums in Enums) - foreach (var e in schemaEnums.Value) - NullableTypes.Add(e.Key.ToModelName(schemaEnums.Key, DefaultSchema)); if (!Options.DotnetFramework.IsDotnetCore()) return; @@ -108,21 +101,6 @@ private static Dictionary> ConstructTablesLook ); } - private static Dictionary> ConstructEnumsLookup(Catalog catalog) - { - return catalog - .Schemas - .SelectMany(s => s.Enums.Select(e => new { EnumItem = e, Schema = s.Name })) - .GroupBy(x => x.Schema == catalog.DefaultSchema ? string.Empty : x.Schema) - .ToDictionary( - group => group.Key, - group => group.ToDictionary( - x => x.EnumItem.Name, - x => x.EnumItem - ) - ); - } - public virtual ISet GetUsingDirectivesForQueries() { return new HashSet @@ -193,15 +171,10 @@ protected virtual ISet GetConfigureSqlMappings() public virtual MemberDeclarationSyntax[] GetMemberDeclarationsForUtils() { - var memberDeclarations = new List(); if (!Options.UseDapper) - return [.. memberDeclarations]; - - memberDeclarations.AddRange(ColumnMappings - .Where(m => TypeExistsInQueries(m.Key) && m.Value.SqlMapperImpl is not null) - .Select(m => ParseMemberDeclaration(m.Value.SqlMapperImpl!)!)); - - return [.. memberDeclarations, + return []; + return [.. + GetSqlMapperMemberDeclarations(), ParseMemberDeclaration($$""" public static void ConfigureSqlMapper() { @@ -210,6 +183,13 @@ public static void ConfigureSqlMapper() """)!]; } + private MemberDeclarationSyntax[] GetSqlMapperMemberDeclarations() + { + return [.. ColumnMappings + .Where(m => TypeExistsInQueries(m.Key) && m.Value.SqlMapperImpl is not null) + .Select(m => ParseMemberDeclaration(m.Value.SqlMapperImpl!)!)]; + } + public abstract string TransformQueryText(Query query); public abstract ConnectionGenCommands EstablishConnection(Query query); diff --git a/Drivers/EnumDbDriver.cs b/Drivers/EnumDbDriver.cs index ef857c91..a4fd298b 100644 --- a/Drivers/EnumDbDriver.cs +++ b/Drivers/EnumDbDriver.cs @@ -1,10 +1,49 @@ +using Microsoft.CodeAnalysis.CSharp.Syntax; using Plugin; using SqlcGenCsharp; using SqlcGenCsharp.Drivers; using System.Collections.Generic; +using System.Linq; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -public abstract class EnumDbDriver(Options options, Catalog catalog, IList queries) : DbDriver(options, catalog, queries) +public abstract class EnumDbDriver : DbDriver { + public Dictionary> Enums { get; } + + public EnumDbDriver(Options options, Catalog catalog, IList queries) : base(options, catalog, queries) + { + Enums = ConstructEnumsLookup(catalog); + + foreach (var schemaEnums in Enums) + foreach (var e in schemaEnums.Value) + NullableTypes.Add(e.Key.ToModelName(schemaEnums.Key, DefaultSchema)); + } + + public virtual MemberDeclarationSyntax[] GetEnumExtensionsMembers(string name, IList possibleValues) + { + return [.. new List() + { + ParseMemberDeclaration($$""" + private static readonly Dictionary StringToEnum = new Dictionary() + { + [string.Empty] = {{name}}.Invalid, + {{possibleValues + .Select(v => $"[\"{v}\"] = {name}.{v.ToPascalCase()}") + .JoinByComma()}} + }; + """)! + }.AddRangeIf( + [ + ParseMemberDeclaration($$""" + public static {{name}} To{{name}}(this string me) + { + return StringToEnum[me]; + } + """)! + ], + !Options.UseDapper)]; + } + protected abstract Enum? GetEnumType(Column column); protected abstract string EnumToCsharpDataType(Column column); @@ -28,4 +67,19 @@ protected override string GetCsharpTypeWithoutNullableSuffix(Column column, Quer return EnumToCsharpDataType(column); return base.GetCsharpTypeWithoutNullableSuffix(column, query); } + + private static Dictionary> ConstructEnumsLookup(Catalog catalog) + { + return catalog + .Schemas + .SelectMany(s => s.Enums.Select(e => new { EnumItem = e, Schema = s.Name })) + .GroupBy(x => x.Schema == catalog.DefaultSchema ? string.Empty : x.Schema) + .ToDictionary( + group => group.Key, + group => group.ToDictionary( + x => x.EnumItem.Name, + x => x.EnumItem + ) + ); + } } \ No newline at end of file diff --git a/Drivers/Generators/CommonGen.cs b/Drivers/Generators/CommonGen.cs index 6a1bc25f..088be948 100644 --- a/Drivers/Generators/CommonGen.cs +++ b/Drivers/Generators/CommonGen.cs @@ -52,7 +52,7 @@ public string ConstructDapperParamsDict(Query query) {{queryParamsVar}}.Add($"@{{p.Column.Name}}Arg{i}", {{argsVar}}.{{param}}[i]); """; - if (dbDriver.Enums.ContainsKey(p.Column.Type.Name)) + if (dbDriver is EnumDbDriver enumDbDriver && enumDbDriver.Enums.ContainsKey(p.Column.Type.Name)) param += "?.ToEnumString()"; var notNull = dbDriver.IsColumnNotNull(p.Column, query); diff --git a/Drivers/MySqlConnectorDriver.cs b/Drivers/MySqlConnectorDriver.cs index f50c181a..527eccee 100644 --- a/Drivers/MySqlConnectorDriver.cs +++ b/Drivers/MySqlConnectorDriver.cs @@ -275,6 +275,20 @@ protected override ISet GetConfigureSqlMappings() .AddRangeExcludeNulls(setSqlMappings); } + public override MemberDeclarationSyntax[] GetEnumExtensionsMembers(string name, IList possibleValues) + { + return [.. base + .GetEnumExtensionsMembers(name, possibleValues) + .AddRangeExcludeNulls([ + ParseMemberDeclaration($$""" + public static HashSet<{{name}}> To{{name}}Set(this string me) + { + return new HashSet<{{name}}>(me.Split(',').ToList().Select(v => StringToEnum[v])); + } + """)! + ])]; + } + private MemberDeclarationSyntax[] GetSetTypeHandlers() { var setTypeHandlerFunc = (string x) => diff --git a/Drivers/NpgsqlDriver.cs b/Drivers/NpgsqlDriver.cs index 6744aabe..a4f9e935 100644 --- a/Drivers/NpgsqlDriver.cs +++ b/Drivers/NpgsqlDriver.cs @@ -479,6 +479,29 @@ public override void SetValue(IDbDataParameter parameter, T{{optionalDotnetCoreN ]; } + public override MemberDeclarationSyntax[] GetEnumExtensionsMembers(string name, IList possibleValues) + { + return [.. base + .GetEnumExtensionsMembers(name, possibleValues) + .AddRangeExcludeNulls([ + ParseMemberDeclaration($$""" + private static readonly Dictionary<{{name}}, string> EnumToString = new Dictionary<{{name}}, string>() + { + [{{name}}.Invalid] = string.Empty, + {{possibleValues + .Select(v => $"[{name}.{v.ToPascalCase()}] = \"{v}\"") + .JoinByComma()}} + }; + """)!, + ParseMemberDeclaration($$""" + public static string Stringify(this {{name}} me) + { + return EnumToString[me]; + } + """)! + ])]; + } + public override ConnectionGenCommands EstablishConnection(Query query) { var connectionStringVar = Variable.ConnectionString.AsPropertyName(); diff --git a/examples/MySqlConnectorDapperExample/Models.cs b/examples/MySqlConnectorDapperExample/Models.cs index 84b866b4..f2e1b2e3 100644 --- a/examples/MySqlConnectorDapperExample/Models.cs +++ b/examples/MySqlConnectorDapperExample/Models.cs @@ -93,23 +93,6 @@ public static class BiosBioTypeExtensions ["Biography"] = BiosBioType.Biography, ["Memoir"] = BiosBioType.Memoir }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [BiosBioType.Invalid] = string.Empty, - [BiosBioType.Autobiography] = "Autobiography", - [BiosBioType.Biography] = "Biography", - [BiosBioType.Memoir] = "Memoir" - }; - public static BiosBioType ToBiosBioType(this string me) - { - return StringToEnum[me]; - } - - public static string Stringify(this BiosBioType me) - { - return EnumToString[me]; - } - public static HashSet ToBiosBioTypeSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); @@ -133,23 +116,6 @@ public static class BiosAuthorTypeExtensions ["Editor"] = BiosAuthorType.Editor, ["Translator"] = BiosAuthorType.Translator }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [BiosAuthorType.Invalid] = string.Empty, - [BiosAuthorType.Author] = "Author", - [BiosAuthorType.Editor] = "Editor", - [BiosAuthorType.Translator] = "Translator" - }; - public static BiosAuthorType ToBiosAuthorType(this string me) - { - return StringToEnum[me]; - } - - public static string Stringify(this BiosAuthorType me) - { - return EnumToString[me]; - } - public static HashSet ToBiosAuthorTypeSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); @@ -173,23 +139,6 @@ public static class MysqlStringTypesCEnumExtensions ["medium"] = MysqlStringTypesCEnum.Medium, ["big"] = MysqlStringTypesCEnum.Big }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [MysqlStringTypesCEnum.Invalid] = string.Empty, - [MysqlStringTypesCEnum.Small] = "small", - [MysqlStringTypesCEnum.Medium] = "medium", - [MysqlStringTypesCEnum.Big] = "big" - }; - public static MysqlStringTypesCEnum ToMysqlStringTypesCEnum(this string me) - { - return StringToEnum[me]; - } - - public static string Stringify(this MysqlStringTypesCEnum me) - { - return EnumToString[me]; - } - public static HashSet ToMysqlStringTypesCEnumSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); @@ -213,23 +162,6 @@ public static class MysqlStringTypesCSetExtensions ["coffee"] = MysqlStringTypesCSet.Coffee, ["milk"] = MysqlStringTypesCSet.Milk }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [MysqlStringTypesCSet.Invalid] = string.Empty, - [MysqlStringTypesCSet.Tea] = "tea", - [MysqlStringTypesCSet.Coffee] = "coffee", - [MysqlStringTypesCSet.Milk] = "milk" - }; - public static MysqlStringTypesCSet ToMysqlStringTypesCSet(this string me) - { - return StringToEnum[me]; - } - - public static string Stringify(this MysqlStringTypesCSet me) - { - return EnumToString[me]; - } - public static HashSet ToMysqlStringTypesCSetSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); diff --git a/examples/MySqlConnectorDapperLegacyExample/Models.cs b/examples/MySqlConnectorDapperLegacyExample/Models.cs index f9ff3f99..ed69a748 100644 --- a/examples/MySqlConnectorDapperLegacyExample/Models.cs +++ b/examples/MySqlConnectorDapperLegacyExample/Models.cs @@ -94,23 +94,6 @@ public static class BiosBioTypeExtensions ["Biography"] = BiosBioType.Biography, ["Memoir"] = BiosBioType.Memoir }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [BiosBioType.Invalid] = string.Empty, - [BiosBioType.Autobiography] = "Autobiography", - [BiosBioType.Biography] = "Biography", - [BiosBioType.Memoir] = "Memoir" - }; - public static BiosBioType ToBiosBioType(this string me) - { - return StringToEnum[me]; - } - - public static string Stringify(this BiosBioType me) - { - return EnumToString[me]; - } - public static HashSet ToBiosBioTypeSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); @@ -134,23 +117,6 @@ public static class BiosAuthorTypeExtensions ["Editor"] = BiosAuthorType.Editor, ["Translator"] = BiosAuthorType.Translator }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [BiosAuthorType.Invalid] = string.Empty, - [BiosAuthorType.Author] = "Author", - [BiosAuthorType.Editor] = "Editor", - [BiosAuthorType.Translator] = "Translator" - }; - public static BiosAuthorType ToBiosAuthorType(this string me) - { - return StringToEnum[me]; - } - - public static string Stringify(this BiosAuthorType me) - { - return EnumToString[me]; - } - public static HashSet ToBiosAuthorTypeSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); @@ -174,23 +140,6 @@ public static class MysqlStringTypesCEnumExtensions ["medium"] = MysqlStringTypesCEnum.Medium, ["big"] = MysqlStringTypesCEnum.Big }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [MysqlStringTypesCEnum.Invalid] = string.Empty, - [MysqlStringTypesCEnum.Small] = "small", - [MysqlStringTypesCEnum.Medium] = "medium", - [MysqlStringTypesCEnum.Big] = "big" - }; - public static MysqlStringTypesCEnum ToMysqlStringTypesCEnum(this string me) - { - return StringToEnum[me]; - } - - public static string Stringify(this MysqlStringTypesCEnum me) - { - return EnumToString[me]; - } - public static HashSet ToMysqlStringTypesCEnumSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); @@ -214,23 +163,6 @@ public static class MysqlStringTypesCSetExtensions ["coffee"] = MysqlStringTypesCSet.Coffee, ["milk"] = MysqlStringTypesCSet.Milk }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [MysqlStringTypesCSet.Invalid] = string.Empty, - [MysqlStringTypesCSet.Tea] = "tea", - [MysqlStringTypesCSet.Coffee] = "coffee", - [MysqlStringTypesCSet.Milk] = "milk" - }; - public static MysqlStringTypesCSet ToMysqlStringTypesCSet(this string me) - { - return StringToEnum[me]; - } - - public static string Stringify(this MysqlStringTypesCSet me) - { - return EnumToString[me]; - } - public static HashSet ToMysqlStringTypesCSetSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); diff --git a/examples/MySqlConnectorExample/Models.cs b/examples/MySqlConnectorExample/Models.cs index 864e31fd..e4df2e76 100644 --- a/examples/MySqlConnectorExample/Models.cs +++ b/examples/MySqlConnectorExample/Models.cs @@ -29,23 +29,11 @@ public static class BiosBioTypeExtensions ["Biography"] = BiosBioType.Biography, ["Memoir"] = BiosBioType.Memoir }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [BiosBioType.Invalid] = string.Empty, - [BiosBioType.Autobiography] = "Autobiography", - [BiosBioType.Biography] = "Biography", - [BiosBioType.Memoir] = "Memoir" - }; public static BiosBioType ToBiosBioType(this string me) { return StringToEnum[me]; } - public static string Stringify(this BiosBioType me) - { - return EnumToString[me]; - } - public static HashSet ToBiosBioTypeSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); @@ -69,23 +57,11 @@ public static class BiosAuthorTypeExtensions ["Editor"] = BiosAuthorType.Editor, ["Translator"] = BiosAuthorType.Translator }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [BiosAuthorType.Invalid] = string.Empty, - [BiosAuthorType.Author] = "Author", - [BiosAuthorType.Editor] = "Editor", - [BiosAuthorType.Translator] = "Translator" - }; public static BiosAuthorType ToBiosAuthorType(this string me) { return StringToEnum[me]; } - public static string Stringify(this BiosAuthorType me) - { - return EnumToString[me]; - } - public static HashSet ToBiosAuthorTypeSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); @@ -109,23 +85,11 @@ public static class MysqlStringTypesCEnumExtensions ["medium"] = MysqlStringTypesCEnum.Medium, ["big"] = MysqlStringTypesCEnum.Big }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [MysqlStringTypesCEnum.Invalid] = string.Empty, - [MysqlStringTypesCEnum.Small] = "small", - [MysqlStringTypesCEnum.Medium] = "medium", - [MysqlStringTypesCEnum.Big] = "big" - }; public static MysqlStringTypesCEnum ToMysqlStringTypesCEnum(this string me) { return StringToEnum[me]; } - public static string Stringify(this MysqlStringTypesCEnum me) - { - return EnumToString[me]; - } - public static HashSet ToMysqlStringTypesCEnumSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); @@ -149,23 +113,11 @@ public static class MysqlStringTypesCSetExtensions ["coffee"] = MysqlStringTypesCSet.Coffee, ["milk"] = MysqlStringTypesCSet.Milk }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [MysqlStringTypesCSet.Invalid] = string.Empty, - [MysqlStringTypesCSet.Tea] = "tea", - [MysqlStringTypesCSet.Coffee] = "coffee", - [MysqlStringTypesCSet.Milk] = "milk" - }; public static MysqlStringTypesCSet ToMysqlStringTypesCSet(this string me) { return StringToEnum[me]; } - public static string Stringify(this MysqlStringTypesCSet me) - { - return EnumToString[me]; - } - public static HashSet ToMysqlStringTypesCSetSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); diff --git a/examples/MySqlConnectorLegacyExample/Models.cs b/examples/MySqlConnectorLegacyExample/Models.cs index 1abbf592..b73dfb56 100644 --- a/examples/MySqlConnectorLegacyExample/Models.cs +++ b/examples/MySqlConnectorLegacyExample/Models.cs @@ -94,23 +94,11 @@ public static class BiosBioTypeExtensions ["Biography"] = BiosBioType.Biography, ["Memoir"] = BiosBioType.Memoir }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [BiosBioType.Invalid] = string.Empty, - [BiosBioType.Autobiography] = "Autobiography", - [BiosBioType.Biography] = "Biography", - [BiosBioType.Memoir] = "Memoir" - }; public static BiosBioType ToBiosBioType(this string me) { return StringToEnum[me]; } - public static string Stringify(this BiosBioType me) - { - return EnumToString[me]; - } - public static HashSet ToBiosBioTypeSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); @@ -134,23 +122,11 @@ public static class BiosAuthorTypeExtensions ["Editor"] = BiosAuthorType.Editor, ["Translator"] = BiosAuthorType.Translator }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [BiosAuthorType.Invalid] = string.Empty, - [BiosAuthorType.Author] = "Author", - [BiosAuthorType.Editor] = "Editor", - [BiosAuthorType.Translator] = "Translator" - }; public static BiosAuthorType ToBiosAuthorType(this string me) { return StringToEnum[me]; } - public static string Stringify(this BiosAuthorType me) - { - return EnumToString[me]; - } - public static HashSet ToBiosAuthorTypeSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); @@ -174,23 +150,11 @@ public static class MysqlStringTypesCEnumExtensions ["medium"] = MysqlStringTypesCEnum.Medium, ["big"] = MysqlStringTypesCEnum.Big }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [MysqlStringTypesCEnum.Invalid] = string.Empty, - [MysqlStringTypesCEnum.Small] = "small", - [MysqlStringTypesCEnum.Medium] = "medium", - [MysqlStringTypesCEnum.Big] = "big" - }; public static MysqlStringTypesCEnum ToMysqlStringTypesCEnum(this string me) { return StringToEnum[me]; } - public static string Stringify(this MysqlStringTypesCEnum me) - { - return EnumToString[me]; - } - public static HashSet ToMysqlStringTypesCEnumSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); @@ -214,23 +178,11 @@ public static class MysqlStringTypesCSetExtensions ["coffee"] = MysqlStringTypesCSet.Coffee, ["milk"] = MysqlStringTypesCSet.Milk }; - private static readonly Dictionary EnumToString = new Dictionary() - { - [MysqlStringTypesCSet.Invalid] = string.Empty, - [MysqlStringTypesCSet.Tea] = "tea", - [MysqlStringTypesCSet.Coffee] = "coffee", - [MysqlStringTypesCSet.Milk] = "milk" - }; public static MysqlStringTypesCSet ToMysqlStringTypesCSet(this string me) { return StringToEnum[me]; } - public static string Stringify(this MysqlStringTypesCSet me) - { - return EnumToString[me]; - } - public static HashSet ToMysqlStringTypesCSetSet(this string me) { return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); diff --git a/examples/NpgsqlDapperExample/Models.cs b/examples/NpgsqlDapperExample/Models.cs index 923b9b7a..b99e2fa3 100644 --- a/examples/NpgsqlDapperExample/Models.cs +++ b/examples/NpgsqlDapperExample/Models.cs @@ -119,20 +119,10 @@ public static class CEnumExtensions [CEnum.Medium] = "medium", [CEnum.Big] = "big" }; - public static CEnum ToCEnum(this string me) - { - return StringToEnum[me]; - } - public static string Stringify(this CEnum me) { return EnumToString[me]; } - - public static HashSet ToCEnumSet(this string me) - { - return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); - } } public enum ExtendedBioType @@ -159,18 +149,8 @@ public static class ExtendedBioTypeExtensions [ExtendedBioType.Biography] = "Biography", [ExtendedBioType.Memoir] = "Memoir" }; - public static ExtendedBioType ToExtendedBioType(this string me) - { - return StringToEnum[me]; - } - public static string Stringify(this ExtendedBioType me) { return EnumToString[me]; } - - public static HashSet ToExtendedBioTypeSet(this string me) - { - return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); - } } \ No newline at end of file diff --git a/examples/NpgsqlDapperLegacyExample/Models.cs b/examples/NpgsqlDapperLegacyExample/Models.cs index 31723a2e..eddac678 100644 --- a/examples/NpgsqlDapperLegacyExample/Models.cs +++ b/examples/NpgsqlDapperLegacyExample/Models.cs @@ -120,20 +120,10 @@ public static class CEnumExtensions [CEnum.Medium] = "medium", [CEnum.Big] = "big" }; - public static CEnum ToCEnum(this string me) - { - return StringToEnum[me]; - } - public static string Stringify(this CEnum me) { return EnumToString[me]; } - - public static HashSet ToCEnumSet(this string me) - { - return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); - } } public enum ExtendedBioType @@ -160,19 +150,9 @@ public static class ExtendedBioTypeExtensions [ExtendedBioType.Biography] = "Biography", [ExtendedBioType.Memoir] = "Memoir" }; - public static ExtendedBioType ToExtendedBioType(this string me) - { - return StringToEnum[me]; - } - public static string Stringify(this ExtendedBioType me) { return EnumToString[me]; } - - public static HashSet ToExtendedBioTypeSet(this string me) - { - return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); - } } } \ No newline at end of file diff --git a/examples/NpgsqlExample/Models.cs b/examples/NpgsqlExample/Models.cs index a30721ec..17dfe72a 100644 --- a/examples/NpgsqlExample/Models.cs +++ b/examples/NpgsqlExample/Models.cs @@ -36,6 +36,11 @@ public static class CEnumExtensions ["medium"] = CEnum.Medium, ["big"] = CEnum.Big }; + public static CEnum ToCEnum(this string me) + { + return StringToEnum[me]; + } + private static readonly Dictionary EnumToString = new Dictionary() { [CEnum.Invalid] = string.Empty, @@ -43,20 +48,10 @@ public static class CEnumExtensions [CEnum.Medium] = "medium", [CEnum.Big] = "big" }; - public static CEnum ToCEnum(this string me) - { - return StringToEnum[me]; - } - public static string Stringify(this CEnum me) { return EnumToString[me]; } - - public static HashSet ToCEnumSet(this string me) - { - return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); - } } public enum ExtendedBioType @@ -76,6 +71,11 @@ public static class ExtendedBioTypeExtensions ["Biography"] = ExtendedBioType.Biography, ["Memoir"] = ExtendedBioType.Memoir }; + public static ExtendedBioType ToExtendedBioType(this string me) + { + return StringToEnum[me]; + } + private static readonly Dictionary EnumToString = new Dictionary() { [ExtendedBioType.Invalid] = string.Empty, @@ -83,18 +83,8 @@ public static class ExtendedBioTypeExtensions [ExtendedBioType.Biography] = "Biography", [ExtendedBioType.Memoir] = "Memoir" }; - public static ExtendedBioType ToExtendedBioType(this string me) - { - return StringToEnum[me]; - } - public static string Stringify(this ExtendedBioType me) { return EnumToString[me]; } - - public static HashSet ToExtendedBioTypeSet(this string me) - { - return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); - } } \ No newline at end of file diff --git a/examples/NpgsqlLegacyExample/Models.cs b/examples/NpgsqlLegacyExample/Models.cs index 6482da2a..2a06f586 100644 --- a/examples/NpgsqlLegacyExample/Models.cs +++ b/examples/NpgsqlLegacyExample/Models.cs @@ -113,6 +113,11 @@ public static class CEnumExtensions ["medium"] = CEnum.Medium, ["big"] = CEnum.Big }; + public static CEnum ToCEnum(this string me) + { + return StringToEnum[me]; + } + private static readonly Dictionary EnumToString = new Dictionary() { [CEnum.Invalid] = string.Empty, @@ -120,20 +125,10 @@ public static class CEnumExtensions [CEnum.Medium] = "medium", [CEnum.Big] = "big" }; - public static CEnum ToCEnum(this string me) - { - return StringToEnum[me]; - } - public static string Stringify(this CEnum me) { return EnumToString[me]; } - - public static HashSet ToCEnumSet(this string me) - { - return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); - } } public enum ExtendedBioType @@ -153,6 +148,11 @@ public static class ExtendedBioTypeExtensions ["Biography"] = ExtendedBioType.Biography, ["Memoir"] = ExtendedBioType.Memoir }; + public static ExtendedBioType ToExtendedBioType(this string me) + { + return StringToEnum[me]; + } + private static readonly Dictionary EnumToString = new Dictionary() { [ExtendedBioType.Invalid] = string.Empty, @@ -160,19 +160,9 @@ public static class ExtendedBioTypeExtensions [ExtendedBioType.Biography] = "Biography", [ExtendedBioType.Memoir] = "Memoir" }; - public static ExtendedBioType ToExtendedBioType(this string me) - { - return StringToEnum[me]; - } - public static string Stringify(this ExtendedBioType me) { return EnumToString[me]; } - - public static HashSet ToExtendedBioTypeSet(this string me) - { - return new HashSet(me.Split(',').ToList().Select(v => StringToEnum[v])); - } } } \ No newline at end of file