Skip to content

Commit 40dc5bf

Browse files
committed
MagicObject 添加快照的值获取
1 parent 185904f commit 40dc5bf

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

Senparc.CO2NET.MagicObject.Tests/MoTests.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public void Get_SingleProperty_NoChange()
4343
Assert.AreEqual("Alice", result.OldValue);
4444
Assert.AreEqual("Alice", result.NewValue);
4545
Assert.IsFalse(result.IsChanged);
46+
Assert.IsFalse(result.HasShapshot);
4647
}
4748

4849
[TestMethod]
@@ -107,20 +108,38 @@ public void SetProperties_BatchSetProperties()
107108
[TestMethod]
108109
public void TakeSnapshot_RestoreSnapshot()
109110
{
111+
//_person = new Person { Name = "Alice", Age = 30 };
112+
113+
Assert.IsFalse(_mo.HasChanges());
114+
110115
_mo.Set(p => p.Name, "Charlie");
116+
Console.WriteLine(_mo.Get(z => z.Age).SnapshotValue);
117+
Assert.IsFalse(_mo.Get(z => z.Age).HasShapshot);
118+
Assert.IsNull(_mo.Get(z => z.Name).SnapshotValue);
119+
Assert.AreEqual(0, _mo.Get(z => z.Age).SnapshotValue);//仍然会返回默认值
120+
Assert.IsTrue(_mo.HasChanges());
121+
111122
_mo.TakeSnapshot();
112123

124+
Assert.IsTrue(_mo.Get(z => z.Age).HasShapshot);
125+
Assert.AreEqual( 30,_mo.Get(z => z.Age).SnapshotValue);
126+
113127
_mo.Set(p => p.Name, "Dave");
114128
var resultBeforeRestore = _mo.Get(p => p.Name);
115-
Assert.AreEqual("Charlie", resultBeforeRestore.OldValue);
129+
Assert.AreEqual("Alice", resultBeforeRestore.OldValue);
130+
Assert.AreEqual("Charlie", resultBeforeRestore.SnapshotValue);
116131
Assert.AreEqual("Dave", resultBeforeRestore.NewValue);
117132
Assert.IsTrue(resultBeforeRestore.IsChanged);
118133

119134
_mo.RestoreSnapshot();
135+
Assert.IsTrue(_mo.Get(z => z.Age).HasShapshot);//快照本身不会被清除
136+
120137
var resultAfterRestore = _mo.Get(p => p.Name);
121-
Assert.AreEqual("Charlie", resultAfterRestore.OldValue);
138+
Assert.AreEqual("Alice", resultAfterRestore.OldValue);
139+
Assert.AreEqual("Charlie", resultBeforeRestore.SnapshotValue);
122140
Assert.AreEqual("Charlie", resultAfterRestore.NewValue);
123-
Assert.IsFalse(resultAfterRestore.IsChanged);
141+
Assert.IsTrue(resultAfterRestore.IsChanged);
142+
Assert.IsFalse(_mo.HasChanges());
124143
}
125144

126145
[TestMethod]

Senparc.CO2NET.MagicObject/MO.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@
55

66
namespace Senparc.CO2NET.MagicObject
77
{
8-
public class PropertyChangeResult<TValue>
9-
{
10-
public TValue OldValue { get; set; }
11-
public TValue NewValue { get; set; }
12-
public bool IsChanged { get; set; }
13-
14-
public override string ToString()
15-
{
16-
return IsChanged ? $"{OldValue} -> {NewValue}" : NewValue?.ToString();
17-
}
18-
}
19-
208
public class MO<T>
219
{
2210
private T OriginalObject { get; set; }
@@ -77,14 +65,20 @@ public PropertyChangeResult<TValue> Get<TValue>(Expression<Func<T, TValue>> expr
7765
{
7866
if (expression.Body is MemberExpression memberExpression && memberExpression.Member is PropertyInfo propertyInfo)
7967
{
80-
var originalValue = (TValue)propertyInfo.GetValue(_snapshot != null ? _snapshot : OriginalObject);
68+
var hasSnapshot = _snapshot != null;
69+
70+
var originalValue = (TValue)propertyInfo.GetValue(OriginalObject);
71+
72+
TValue? snapshptValue = hasSnapshot ? (TValue?)propertyInfo.GetValue(_snapshot) : default;
8173
var newValue = (TValue)propertyInfo.GetValue(Object);
8274

8375
return new PropertyChangeResult<TValue>
8476
{
8577
OldValue = originalValue,
78+
SnapshotValue = snapshptValue,
8679
NewValue = newValue,
87-
IsChanged = !Equals(originalValue, newValue)
80+
IsChanged = /*hasSnapshot ? Equals(snapshptValue, newValue) :*/ !Equals(originalValue, newValue),
81+
HasShapshot = hasSnapshot
8882
};
8983
}
9084
else
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Senparc.CO2NET.MagicObject
8+
{
9+
public class PropertyChangeResult<TValue>
10+
{
11+
public TValue OldValue { get; set; }
12+
public TValue NewValue { get; set; }
13+
public TValue? SnapshotValue { get; set; }
14+
public bool IsChanged { get; set; }
15+
public bool HasShapshot { get; set; }
16+
17+
18+
public override string ToString()
19+
{
20+
return IsChanged ? $"{OldValue} -> {NewValue}" : NewValue?.ToString();
21+
}
22+
}
23+
24+
}

0 commit comments

Comments
 (0)