用 Dapper 充实枚举类

我正在使用 Dapper 来补充 C# 类。我最近从字符串常量集合转移到此处定义的“枚举类”:https ://learn.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over -枚举类型

我的枚举如下所示:

public abstract class Enumeration : IComparable

    {

        public string Name { get; }


        protected Enumeration(string name)

        {

            Name = name;

        }


        public static IEnumerable<T> GetAll<T>() where T : Enumeration

        {

            var fields = typeof(T).GetFields(BindingFlags.Public |

                                             BindingFlags.Static |

                                             BindingFlags.DeclaredOnly);


            return fields.Select(f => f.GetValue(null)).Cast<T>();

        }


        public static IEnumerable<T> ToSortedEnumerable<T>() where T : Enumeration

        {

            List<T> values = GetAll<T>().ToList();

            values.Sort();

            return values;

        }


        public int CompareTo(object other) =>

            string.Compare(Name, ((Enumeration) other).Name, StringComparison.Ordinal);


        public static implicit operator string(Enumeration enumeration)

        {

            return enumeration?.ToString();

        }


        public static bool operator ==(Enumeration e1, Enumeration e2)

        {

            return Equals(e1, e2);

        }


        public static bool operator !=(Enumeration e1, Enumeration e2)

        {

            return !Equals(e1, e2);

        }


        public static bool HasValue<T>(string valueToCheck) where T : Enumeration

        {

            return Enumeration.GetAll<T>().Any(x => x.Name.Equals(valueToCheck, StringComparison.OrdinalIgnoreCase));

        }


        public static bool TryGetEnumeration<T>(string valueToCheck, out T result) where T : Enumeration

        {

            result = Enumeration.GetAll<T>()

                                .FirstOrDefault(

                                    x => x.Name.Equals(valueToCheck, StringComparison.OrdinalIgnoreCase));


            return result != null;

        }


GCT1015
浏览 119回答 2
2回答

回首忆惘然

好吧,想通了。两件事情:首先, splitOn 是实现此目的的方法。一个不同但相关的最终版本如下所示:return connection.Query<Program,&nbsp; &nbsp; AssistanceProgramCategory,&nbsp; &nbsp; AssistanceProgramType,&nbsp; &nbsp; AssistanceProgramLegalType,&nbsp; &nbsp; ProgramAuthority,&nbsp; &nbsp; Program>(sql: Constants.SqlStatements.SELECT_PROGRAMS_SQL,&nbsp; &nbsp; (program, category, programType, legalType, authority) =>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; program.AssistanceCategory = category;&nbsp; &nbsp; &nbsp; &nbsp; program.ProgramType = programType;&nbsp; &nbsp; &nbsp; &nbsp; program.ProgramLegalType = legalType;&nbsp; &nbsp; &nbsp; &nbsp; program.Authority = authority;&nbsp; &nbsp; &nbsp; &nbsp; return program;&nbsp; &nbsp; }, splitOn: "Name,Jurisdiction");其中 AssistanceProgramCategory、AssistanceProgramType 和 AssistanceProgramLegalType 都是 Enumeration 的子项。其次,SQL 必须提供带有名称的列,如下所示:SELECT global_id as GlobalId,tier,program_description as Name,program_type as Name,program_legal_type as Name,jurisdiction as Jurisdiction,customer_id as CustomerId,program_name as ProgramNameForJurisdiction,program_description as ProgramNameFROM public.assistance_programs第三,我只需要将“Name”放入 splitOn 一次 - Name 的每个实例都会导致创建一个新对象。最后,我必须交换 Jurisdiction 和 CustomerId,因为 CustomerId 可以为 null,并且当为 NULL 时,它不会将最终的水合激发到 ProgramAuthority 中。管辖权始终存在,因此通过交换 SQL 中的列来解决问题。

守候你守候我

也许这太简单了,但是,您选择的列名需要与您要映射到的类中的属性相匹配,否则 Dapper 将不知道如何使映射匹配。所以如果你的班级是:public class Person{&nbsp; &nbsp; public Race Race {get;}&nbsp; &nbsp; public Guid PersonId {get;}}那么您的查询需要匹配:return connection.Query<Person>(sql: @"&nbsp; &nbsp; SELECT&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Race&nbsp; &nbsp; &nbsp; &nbsp;, person_id as PersonIdFROM public.people");请注意 Race 上的大写 R。(为了更好地衡量,我也喜欢让它们保持相同的顺序,尽管我不确定这是否重要。)除此之外,如果您直接对数据库执行查询,您会得到您期望的结果吗?
打开App,查看更多内容
随时随地看视频慕课网APP