Skip to content

Commit 58f7ef7

Browse files
committed
Refactor
1 parent 2309225 commit 58f7ef7

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

IPConfig/Models/IDeepCloneable.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace IPConfig.Models;
2+
3+
/// <summary>
4+
/// 支持深层克隆。
5+
/// </summary>
6+
/// <typeparam name="T"></typeparam>
7+
public interface IDeepCloneable<T>
8+
{
9+
T DeepClone();
10+
}

IPConfig/Models/IPConfigBase.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
namespace IPConfig.Models;
88

9-
public abstract partial class IPConfigBase<T> : ObservableValidator, IDeepCloneTo<T> where T : IPConfigBase<T>
9+
public abstract partial class IPConfigBase<T> : ObservableValidator,
10+
IDeepCloneable<T>, IDeepCloneTo<T> where T : IPConfigBase<T>, new()
1011
{
1112
protected bool _allowAutoDisableAutoDns;
1213

@@ -131,7 +132,24 @@ public void AllowAutoDisableDhcp(bool allow = true)
131132
_allowAutoDisableDhcp = allow;
132133
}
133134

134-
public abstract void DeepCloneTo(T other);
135+
public virtual T DeepClone()
136+
{
137+
T clone = new();
138+
DeepCloneTo(clone);
139+
140+
return clone;
141+
}
142+
143+
public virtual void DeepCloneTo(T other)
144+
{
145+
other.IP = IP;
146+
other.IsDhcpEnabled = IsDhcpEnabled;
147+
other.Mask = Mask;
148+
other.Gateway = Gateway;
149+
other.IsAutoDns = IsAutoDns;
150+
other.Dns1 = Dns1;
151+
other.Dns2 = Dns2;
152+
}
135153

136154
public virtual void FormatProperties()
137155
{

IPConfig/Models/IPConfigModel.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,9 @@ public void DeepCloneTo(IPConfigModel other)
122122

123123
public bool PropertyEquals(IPConfigModel other)
124124
{
125-
if (Name == other.Name
126-
&& IPv4Config.IP == other.IPv4Config.IP
127-
&& IPv4Config.IsDhcpEnabled == other.IPv4Config.IsDhcpEnabled
128-
&& IPv4Config.Mask == other.IPv4Config.Mask
129-
&& IPv4Config.Gateway == other.IPv4Config.Gateway
130-
&& IPv4Config.IsAutoDns == other.IPv4Config.IsAutoDns
131-
&& IPv4Config.Dns1 == other.IPv4Config.Dns1
132-
&& IPv4Config.Dns2 == other.IPv4Config.Dns2
133-
&& Remark == other.Remark)
125+
bool iPv4ConfigEquals = IPv4Config.PropertyEquals(other.IPv4Config);
126+
127+
if (iPv4ConfigEquals && Name == other.Name && Remark == other.Remark)
134128
{
135129
return true;
136130
}

IPConfig/Models/IPv4Config.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,20 @@ public class IPv4Config : IPConfigBase<IPv4Config>
4040
base.ClearErrors(propertyName);
4141
}
4242

43-
public override void DeepCloneTo(IPv4Config other)
43+
public bool PropertyEquals(IPv4Config other)
4444
{
45-
other.IP = IP;
46-
other.IsDhcpEnabled = IsDhcpEnabled;
47-
other.Mask = Mask;
48-
other.Gateway = Gateway;
49-
other.IsAutoDns = IsAutoDns;
50-
other.Dns1 = Dns1;
51-
other.Dns2 = Dns2;
45+
if (IP == other.IP
46+
&& IsDhcpEnabled == other.IsDhcpEnabled
47+
&& Mask == other.Mask
48+
&& Gateway == other.Gateway
49+
&& IsAutoDns == other.IsAutoDns
50+
&& Dns1 == other.Dns1
51+
&& Dns2 == other.Dns2)
52+
{
53+
return true;
54+
}
55+
56+
return false;
5257
}
5358

5459
public override string ToString()

0 commit comments

Comments
 (0)