Skip to content
This repository was archived by the owner on Jan 27, 2022. It is now read-only.

Commit 8966d6d

Browse files
authored
Merge pull request #7 from wintoncode/class-valueobjects
Remove struct constraint on value objects.
2 parents c5dac7d + a189f80 commit 8966d6d

4 files changed

Lines changed: 56 additions & 41 deletions

File tree

src/Winton.DomainModelling.DocumentDb/IValueObjectFacade.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface IValueObjectFacade
1919
/// <param name="valueObject">The Value Object to persist.</param>
2020
/// <returns>A Task.</returns>
2121
Task Create<TValueObject>(TValueObject valueObject)
22-
where TValueObject : struct, IEquatable<TValueObject>;
22+
where TValueObject : IEquatable<TValueObject>;
2323

2424
/// <summary>
2525
/// Delete a Value Object of a specified type.
@@ -28,14 +28,14 @@ Task Create<TValueObject>(TValueObject valueObject)
2828
/// <param name="valueObject">The Value Object to delete.</param>
2929
/// <returns>A Task.</returns>
3030
Task Delete<TValueObject>(TValueObject valueObject)
31-
where TValueObject : struct, IEquatable<TValueObject>;
31+
where TValueObject : IEquatable<TValueObject>;
3232

3333
/// <summary>
3434
/// Query Value Objects of a specified type.
3535
/// </summary>
3636
/// <typeparam name="TValueObject">The type of the Value Objects.</typeparam>
3737
/// <returns>An <see cref="IQueryable{TValueObject}" />.</returns>
3838
IQueryable<TValueObject> Query<TValueObject>()
39-
where TValueObject : struct, IEquatable<TValueObject>;
39+
where TValueObject : IEquatable<TValueObject>;
4040
}
4141
}

src/Winton.DomainModelling.DocumentDb/ValueObjectDocument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace Winton.DomainModelling.DocumentDb
88
{
99
internal sealed class ValueObjectDocument<TValueObject>
10-
where TValueObject : struct, IEquatable<TValueObject>
10+
where TValueObject : IEquatable<TValueObject>
1111
{
1212
[JsonConstructor]
1313
private ValueObjectDocument(TValueObject valueObject, string id)

src/Winton.DomainModelling.DocumentDb/ValueObjectFacade.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public ValueObjectFacade(
4949
/// <param name="valueObject">The Value Object to persist.</param>
5050
/// <returns>A Task.</returns>
5151
public async Task Create<TValueObject>(TValueObject valueObject)
52-
where TValueObject : struct, IEquatable<TValueObject>
52+
where TValueObject : IEquatable<TValueObject>
5353
{
5454
ValueObjectDocument<TValueObject> document = Get(valueObject);
5555

@@ -69,7 +69,7 @@ public async Task Create<TValueObject>(TValueObject valueObject)
6969
/// <param name="valueObject">The Value Object to delete.</param>
7070
/// <returns>A Task.</returns>
7171
public async Task Delete<TValueObject>(TValueObject valueObject)
72-
where TValueObject : struct, IEquatable<TValueObject>
72+
where TValueObject : IEquatable<TValueObject>
7373
{
7474
ValueObjectDocument<TValueObject> document = Get(valueObject);
7575

@@ -86,13 +86,13 @@ public async Task Delete<TValueObject>(TValueObject valueObject)
8686
/// <typeparam name="TValueObject">The type of the Value Objects.</typeparam>
8787
/// <returns>An <see cref="T:System.Linq.IQueryable`1" />.</returns>
8888
public IQueryable<TValueObject> Query<TValueObject>()
89-
where TValueObject : struct, IEquatable<TValueObject>
89+
where TValueObject : IEquatable<TValueObject>
9090
{
9191
return CreateValueObjectDocumentQuery<TValueObject>().Select(x => x.ValueObject);
9292
}
9393

9494
private IQueryable<ValueObjectDocument<TValueObject>> CreateValueObjectDocumentQuery<TValueObject>()
95-
where TValueObject : struct, IEquatable<TValueObject>
95+
where TValueObject : IEquatable<TValueObject>
9696
{
9797
string valueObjectType = ValueObjectDocument<TValueObject>.GetDocumentType();
9898

@@ -101,7 +101,7 @@ private IQueryable<ValueObjectDocument<TValueObject>> CreateValueObjectDocumentQ
101101
}
102102

103103
private ValueObjectDocument<TValueObject> Get<TValueObject>(TValueObject valueObject)
104-
where TValueObject : struct, IEquatable<TValueObject>
104+
where TValueObject : IEquatable<TValueObject>
105105
{
106106
return CreateValueObjectDocumentQuery<TValueObject>()
107107
.AsEnumerable()

test/Winton.DomainModelling.DocumentDb.IntegrationTests/ValueObjectFacadeTests.cs

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,6 @@ public void Dispose()
4444
_documentClient.DeleteDatabaseAsync(_database.SelfLink).Wait();
4545
}
4646

47-
private struct OtherTestValueObject : IEquatable<OtherTestValueObject>
48-
{
49-
[JsonConstructor]
50-
public OtherTestValueObject(int value)
51-
{
52-
Value = value;
53-
}
54-
55-
// ReSharper disable once MemberCanBePrivate.Local
56-
public int Value { get; }
57-
58-
public bool Equals(OtherTestValueObject other)
59-
{
60-
return Value == other.Value;
61-
}
62-
63-
public override bool Equals(object obj)
64-
{
65-
if (obj is null)
66-
{
67-
return false;
68-
}
69-
70-
return obj is OtherTestValueObject o && Equals(o);
71-
}
72-
73-
public override int GetHashCode()
74-
{
75-
return Value;
76-
}
77-
}
78-
7947
private struct TestValueObject : IEquatable<TestValueObject>
8048
{
8149
[JsonConstructor]
@@ -190,5 +158,52 @@ private async Task ShouldQueryValueObjectsOfCorrectType()
190158
});
191159
}
192160
}
161+
162+
private class OtherTestValueObject : IEquatable<OtherTestValueObject>
163+
{
164+
[JsonConstructor]
165+
public OtherTestValueObject(int value)
166+
{
167+
Value = value;
168+
}
169+
170+
// ReSharper disable once MemberCanBePrivate.Local
171+
public int Value { get; }
172+
173+
public bool Equals(OtherTestValueObject other)
174+
{
175+
if (other is null)
176+
{
177+
return false;
178+
}
179+
180+
if (ReferenceEquals(this, other))
181+
{
182+
return true;
183+
}
184+
185+
return Value == other.Value;
186+
}
187+
188+
public override bool Equals(object obj)
189+
{
190+
if (obj is null)
191+
{
192+
return false;
193+
}
194+
195+
if (ReferenceEquals(this, obj))
196+
{
197+
return true;
198+
}
199+
200+
return obj.GetType() == GetType() && Equals((OtherTestValueObject)obj);
201+
}
202+
203+
public override int GetHashCode()
204+
{
205+
return Value;
206+
}
207+
}
193208
}
194209
}

0 commit comments

Comments
 (0)