C#9引入的自定义类型record
record是C#9引入的新的引用類型,詳細(xì)見官方文檔:https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-9#record-types
在C#中,引用類型有:interface,class,delegate,數(shù)組;現(xiàn)在record加入了進(jìn)來,這是一個(gè)與類極度相似,但又不同的類型,重點(diǎn)在比較兩個(gè)對象相等時(shí),不是用引用地址作比較(雖然它是個(gè)引用類型),而是用“類型名{屬性名1=屬性值,屬性名2=屬性值,……}”(注1:這是一種形象但不嚴(yán)格的說法)。
DDD中,有實(shí)體和值對象的概念,其中值對象定義如下:
通過對象屬性值來識別的對象,它將多個(gè)相關(guān)屬性組合為一個(gè)概念整體——《實(shí)現(xiàn)領(lǐng)域驅(qū)動設(shè)計(jì)》
就是通過這個(gè)對象的各個(gè)屬性相同不相同來判斷是不是同一個(gè)值對象,這時(shí),record就最合適不過了,它能直接判斷兩個(gè)實(shí)例化后的對象是否相等,。
record本質(zhì)上個(gè)class,對反射,dapper的適配,面向?qū)ο蟮奶卣鞫际窍嗤?#xff0c;demo見:https://github.com/axzxs2001/Asp.NetCoreExperiment/blob/master/Asp.NetCoreExperiment/CSharp/RecordTypeDemo/Program.cs
using Dapper; using Npgsql; using System; using System.Collections.Generic; using System.Linq; using System.Reflection;namespace RecordTypeDemo {class Program{static void Main(string[] args){//接口類型IShow show = new Entity1() { ID = 1, Name = "桂素偉" };Console.WriteLine(show);//輸出結(jié)果:Entity1 { ID = 1, Name = 桂素偉 }//抽象類型Entity entity = new Entity1() { ID = 1, Name = "桂素偉" };Console.WriteLine(entity);//輸出結(jié)果:Entity1 { ID = 1, Name = 桂素偉 }//注1:雖然 類型名{屬性名1=屬性值,屬性名2=屬性值,……} 相等,但比較結(jié)果是不等的Console.WriteLine($"show == entity結(jié)果:{show == entity}");//輸出結(jié)果:show == entity結(jié)果::False//實(shí)體類型var entity1 = new Entity1() { ID = 1, Name = "桂素偉" };Console.WriteLine(entity1);//輸出結(jié)果:Entity1 { ID = 1, Name = 桂素偉 }Console.WriteLine($"(entity1 == entity結(jié)果:{entity1 == entity}");//輸出結(jié)果:(entity1 == entity結(jié)果:TrueConsole.WriteLine($"(entity1 == entity結(jié)果:{entity1.Equals(entity)}");//輸出結(jié)果:(entity1 == entity結(jié)果:TrueAddEntity(entity);ReflectionTest(entity1);}/// <summary>/// 反射中使用record,和類相山/// </summary>/// <param name="entity"></param>static void ReflectionTest(Entity entity){var type = Assembly.GetExecutingAssembly().GetType("RecordTypeDemo.Entity");Console.WriteLine(type.IsClass);}/// <summary>/// 對dapper適配/// </summary>/// <param name="entity"></param>/// <returns></returns>static bool AddEntity(Entity entity){using (var con = new NpgsqlConnection("Server=127.0.0.1;Port=5432;UserId=postgres;Password=postgres2018;Database=postgres;")){var list = con.Query<Entity1>("select * from entitys").ToList();if (!list.Contains(entity)){con.Execute("insert into entitys(id,name) values(@id,@name)", entity);}return true;}/*表結(jié)構(gòu) CREATE TABLE public.entitys (id integer NOT NULL DEFAULT nextval('entitys_id_seq'::regclass),name character varying(256) COLLATE pg_catalog."default",CONSTRAINT entitys_pkey PRIMARY KEY (id) ) */}}#region 面向?qū)ο筇卣骱皖愐粯?// <summary>/// 接口/// </summary>public interface IShow{void Show();}/// <summary>/// 抽象記錄/// </summary>public abstract record Entity : IShow{public abstract int ID { get; set; }public abstract string Name { get; set; }public void Show(){Console.WriteLine($"{this.GetType().Name}:");Console.WriteLine($"{this.ToString()}");}}/// <summary>/// 記錄/// </summary>public record Entity1 : Entity{public override int ID{get;set;}public override string Name{get;set;}}#endregion }總結(jié)
以上是生活随笔為你收集整理的C#9引入的自定义类型record的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从读大学到工作,我的这几年时光是如何度过
- 下一篇: C# 8: 默认接口方法