惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AWS News Blog
AWS News Blog
T
Tenable Blog
Project Zero
Project Zero
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 热门话题
T
Threat Research - Cisco Blogs
T
Threatpost
Security Latest
Security Latest
C
Cisco Blogs
L
Lohrmann on Cybersecurity
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
NISL@THU
NISL@THU
AI
AI
V
Vulnerabilities – Threatpost
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
H
Hackread – Cybersecurity News, Data Breaches, AI and More
H
Hacker News: Front Page
U
Unit 42
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MyScale Blog
MyScale Blog
O
OpenAI News
Scott Helme
Scott Helme
V2EX - 技术
V2EX - 技术
P
Proofpoint News Feed
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cyberwarzone
Cyberwarzone
博客园 - 【当耐特】
H
Heimdal Security Blog
S
Schneier on Security
阮一峰的网络日志
阮一峰的网络日志
Help Net Security
Help Net Security
D
DataBreaches.Net
Y
Y Combinator Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
TaoSecurity Blog
TaoSecurity Blog
K
Kaspersky official blog
N
News and Events Feed by Topic
WordPress大学
WordPress大学
P
Palo Alto Networks Blog

博客园 - On the road....

c# -- 对象销毁和垃圾回收 C#集合 -- Equality和Order插件 C#集合--Dictionary C#集合 -- 自定义集合与代理 C#集合 -- Lists,Queues, Stacks 和 Sets C#集合--ICollection接口和IList接口 C#集合--数组 C#集合-列举(Enumeration) C#排序比较 Customer IEnuramble Extension C#相等性比较 c#列举和迭代器 C#按需序列化对象为Json字符串 c#如何区分静态只读变量和常量 C#如何更好地理解引用类型和值类型 C#代理那点事儿 Pro ASP.NET MVC –第五章 使用Razor Pro ASP.NET MVC –第六章 MVC的基本工具 Pro ASP.NET MVC –第四章 语言特性精华
C#记录对象的变化
On the road.... · 2014-02-21 · via 博客园 - On the road....

经常,我们会遇到一个场景,在保存对象到数据库之前,对比内存对象和数据库值的差异。

下面我写了一种实现,为保存定义一个事件,然后自动找出对象之间的差异,请注意,并没有通过反射的方式去获取每个属性及其值。因为那样会影响性能。

闲话不表,直接贴代码

class Program
    {
        static void Main(string[] args)
        {
            Staff s = new Staff("10000", "11111", "22222");
            StaffOM.Save(s);  
            
            Console.ReadLine();
        }
    }


public class Staff
    {
        public string StaffNo { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public Staff(string no, string fn, string ln)
        {
            StaffNo = no;
            FirstName = fn;
            LastName = ln;
        }

        public static IList<ObjectDifference> GetDifferences(Staff x, Staff y)
        {
            return new List<ObjectDifference> 
            {
                new ObjectDifference("FirstName", x.FirstName, y.FirstName),            
                new ObjectDifference("LastName", x.LastName, y.LastName)
            };
        }
    }

public static class StaffOM
    {
        public static void Save(Staff s)
        {
            
            StaffDAO.OnSave += StaffDAO_OnSave;

            StaffDAO.Save(s);
        }

        public static void StaffDAO_OnSave(Staff s)
        {
            Staff old = new Staff("10000", "AAA", "BBB");
            
            string result = ObjectHelper.FindDifference(()=>Staff.GetDifferences(old,s));

            Console.WriteLine(result);
        }
    }

public delegate void StaffSaveHandler(Staff s1);


public static class StaffDAO
    {
        public static void Save(Staff s)
        {
                OnSave(s);
        }

        public static event StaffSaveHandler OnSave;
    }


public static class ObjectHelper 
    {
        public static string FindDifference(Func<IList<ObjectDifference>> func)
        {
            StringBuilder buffer = new StringBuilder();
            foreach (ObjectDifference objDiff in func())
                buffer.Append(objDiff.ToString());

            return buffer.ToString();
        }
    }


public class ObjectDifference
    {
        public string PropertyName { get; set; }
        public string OldValue { get; set; }
        public string NewValue { get; set; }

        public ObjectDifference(string p, string o, string n)
        {
            PropertyName = p;
            OldValue = o;
            NewValue = n;
        }

        public override string ToString()
        {
            return string.Format("{0}: {1} -> {2}\n", PropertyName, OldValue, NewValue);
        }
    }

All code