Skip to content

Commit 0e4e12d

Browse files
committed
More code and tests written for deep cloning.
1 parent 9118701 commit 0e4e12d

24 files changed

+1048
-366
lines changed

src/Umbraco.Core/Models/ContentTypeSort.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,27 @@ public object DeepClone()
4242
clone.Id = new Lazy<int>(() => id);
4343
return clone;
4444
}
45+
46+
protected bool Equals(ContentTypeSort other)
47+
{
48+
return Id.Value.Equals(other.Id.Value) && string.Equals(Alias, other.Alias);
49+
}
50+
51+
public override bool Equals(object obj)
52+
{
53+
if (ReferenceEquals(null, obj)) return false;
54+
if (ReferenceEquals(this, obj)) return true;
55+
if (obj.GetType() != this.GetType()) return false;
56+
return Equals((ContentTypeSort) obj);
57+
}
58+
59+
public override int GetHashCode()
60+
{
61+
unchecked
62+
{
63+
return (Id.GetHashCode()*397) ^ Alias.GetHashCode();
64+
}
65+
}
66+
4567
}
4668
}

src/Umbraco.Core/Models/DictionaryItem.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Reflection;
45
using System.Runtime.Serialization;
56
using Umbraco.Core.Models.EntityBase;
@@ -96,5 +97,12 @@ internal override void AddingEntity()
9697
if(ParentId == Guid.Empty)
9798
_parentId = new Guid("41c7638d-f529-4bff-853e-59a0c2fb1bde");
9899
}
100+
101+
public override object DeepClone()
102+
{
103+
var clone = (DictionaryItem)base.DeepClone();
104+
clone.Translations = Translations.Select(x => (IDictionaryTranslation) x.DeepClone()).ToList();
105+
return clone;
106+
}
99107
}
100108
}

src/Umbraco.Core/Models/DictionaryTranslation.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,12 @@ public string Value
6565
}, _value, ValueSelector);
6666
}
6767
}
68+
69+
public override object DeepClone()
70+
{
71+
var clone = (DictionaryTranslation)base.DeepClone();
72+
clone.Language = (ILanguage)Language.DeepClone();
73+
return clone;
74+
}
6875
}
6976
}

src/Umbraco.Core/Models/Relation.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,12 @@ public int RelationTypeId
107107
{
108108
get { return _relationType.Id; }
109109
}
110+
111+
public override object DeepClone()
112+
{
113+
var clone = (Relation)base.DeepClone();
114+
clone.RelationType = (RelationType)RelationType.DeepClone();
115+
return clone;
116+
}
110117
}
111118
}

src/Umbraco.Core/Models/RelationType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,6 @@ public Guid ChildObjectType
118118
}, _childObjectType, ChildObjectTypeSelector);
119119
}
120120
}
121+
121122
}
122123
}

src/Umbraco.Core/Models/ServerRegistration.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Reflection;
34
using Umbraco.Core.Models.EntityBase;
45
using Umbraco.Core.Persistence.Mappers;
@@ -34,7 +35,7 @@ public ServerRegistration(int id, string serverAddress, string computerName, Dat
3435
{
3536
UpdateDate = updateDate;
3637
CreateDate = createDate;
37-
Key = Id.ToString().EncodeAsGuid();
38+
Key = Id.ToString(CultureInfo.InvariantCulture).EncodeAsGuid();
3839
Id = id;
3940
ServerAddress = serverAddress;
4041
ComputerName = computerName;
@@ -51,7 +52,7 @@ public ServerRegistration(string serverAddress, string computerName, DateTime cr
5152
{
5253
CreateDate = createDate;
5354
UpdateDate = createDate;
54-
Key = 0.ToString().EncodeAsGuid();
55+
Key = 0.ToString(CultureInfo.InvariantCulture).EncodeAsGuid();
5556
ServerAddress = serverAddress;
5657
ComputerName = computerName;
5758
}

src/Umbraco.Core/Models/Task.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,12 @@ public string Comment
132132
}, _comment, CommentSelector);
133133
}
134134
}
135+
136+
public override object DeepClone()
137+
{
138+
var clone = (Task) base.DeepClone();
139+
clone.TaskType = (TaskType)TaskType.DeepClone();
140+
return clone;
141+
}
135142
}
136143
}

src/Umbraco.Core/Models/UmbracoEntity.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Globalization;
4+
using System.Linq;
45
using System.Reflection;
56
using Umbraco.Core.Models.EntityBase;
67

@@ -46,17 +47,20 @@ internal class UmbracoEntity : Entity, IUmbracoEntity
4647
public UmbracoEntity()
4748
{
4849
AdditionalData = new Dictionary<string, object>();
50+
UmbracoProperties = new List<UmbracoProperty>();
4951
}
5052

5153
public UmbracoEntity(bool trashed)
5254
{
5355
AdditionalData = new Dictionary<string, object>();
56+
UmbracoProperties = new List<UmbracoProperty>();
5457
Trashed = trashed;
5558
}
5659

5760
public UmbracoEntity(int trashed)
5861
{
5962
AdditionalData = new Dictionary<string, object>();
63+
UmbracoProperties = new List<UmbracoProperty>();
6064
Trashed = trashed == 1;
6165
}
6266

@@ -287,10 +291,45 @@ public Guid NodeObjectTypeId
287291
/// </summary>
288292
public IList<UmbracoProperty> UmbracoProperties { get; set; }
289293

290-
internal class UmbracoProperty
294+
public override object DeepClone()
295+
{
296+
var clone = (UmbracoEntity)base.DeepClone();
297+
clone.UmbracoProperties = UmbracoProperties.Select(x => (UmbracoProperty) x.DeepClone()).ToList();
298+
return clone;
299+
}
300+
301+
internal class UmbracoProperty : IDeepCloneable
291302
{
292303
public Guid DataTypeControlId { get; set; }
293304
public string Value { get; set; }
305+
public object DeepClone()
306+
{
307+
//Memberwise clone on Entity will work since it doesn't have any deep elements
308+
// for any sub class this will work for standard properties as well that aren't complex object's themselves.
309+
var clone = MemberwiseClone();
310+
return clone;
311+
}
312+
313+
protected bool Equals(UmbracoProperty other)
314+
{
315+
return DataTypeControlId.Equals(other.DataTypeControlId) && string.Equals(Value, other.Value);
316+
}
317+
318+
public override bool Equals(object obj)
319+
{
320+
if (ReferenceEquals(null, obj)) return false;
321+
if (ReferenceEquals(this, obj)) return true;
322+
if (obj.GetType() != this.GetType()) return false;
323+
return Equals((UmbracoProperty) obj);
324+
}
325+
326+
public override int GetHashCode()
327+
{
328+
unchecked
329+
{
330+
return (DataTypeControlId.GetHashCode()*397) ^ (Value != null ? Value.GetHashCode() : 0);
331+
}
332+
}
294333
}
295334
}
296335
}

0 commit comments

Comments
 (0)