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

Commit acbd584

Browse files
iscottb122Iain Scott
authored andcommitted
Changed constructors to static Create methods on document wrapper types.
1 parent 606aaf7 commit acbd584

6 files changed

Lines changed: 28 additions & 27 deletions

File tree

src/Winton.DomainModelling.DocumentDb/EntityDocument.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ internal sealed class EntityDocument<TEntity, TEntityId, TDto>
1010
where TEntity : Entity<TEntityId>
1111
where TEntityId : IEquatable<TEntityId>
1212
{
13-
public EntityDocument(TEntity entity, TDto dto)
14-
: this(dto, GetDocumentId(entity.Id), GetDocumentType())
15-
{
16-
}
17-
1813
[JsonConstructor]
1914
private EntityDocument(TDto entity, string id, string type)
2015
{
@@ -31,6 +26,11 @@ private EntityDocument(TDto entity, string id, string type)
3126

3227
public string Type { get; }
3328

29+
public static EntityDocument<TEntity, TEntityId, TDto> Create(TEntity entity, TDto dto)
30+
{
31+
return new EntityDocument<TEntity, TEntityId, TDto>(dto, GetDocumentId(entity.Id), GetDocumentType());
32+
}
33+
3434
public static string GetDocumentId(TEntityId id)
3535
{
3636
return $"{GetDocumentType()}_{JsonConvert.SerializeObject(id)}";

src/Winton.DomainModelling.DocumentDb/EntityFacade.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public async Task<TEntity> Create(TEntity entity)
4545
{
4646
TEntity entityWithId = entity.WithId<TEntity, TEntityId>();
4747

48-
var document = new EntityDocument<TEntity, TEntityId, TDto>(entityWithId, _dtoMapping(entityWithId));
48+
EntityDocument<TEntity, TEntityId, TDto> document =
49+
EntityDocument<TEntity, TEntityId, TDto>.Create(entityWithId, _dtoMapping(entityWithId));
4950

5051
ResourceResponse<Document> response = await _documentClient.CreateDocumentAsync(GetUri(), document);
5152
EntityDocument<TEntity, TEntityId, TDto> responseDocument = (dynamic)response.Resource;
@@ -92,7 +93,9 @@ public async Task<TEntity> Upsert(TEntity entity)
9293
throw new NotSupportedException("Upserting with default ID is not supported.");
9394
}
9495

95-
var document = new EntityDocument<TEntity, TEntityId, TDto>(entity, _dtoMapping(entity));
96+
EntityDocument<TEntity, TEntityId, TDto> document = EntityDocument<TEntity, TEntityId, TDto>.Create(
97+
entity,
98+
_dtoMapping(entity));
9699

97100
ResourceResponse<Document> response = await _documentClient.UpsertDocumentAsync(GetUri(), document);
98101
EntityDocument<TEntity, TEntityId, TDto> responseDocument = (dynamic)response.Resource;

src/Winton.DomainModelling.DocumentDb/ValueObjectDocument.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ namespace Winton.DomainModelling.DocumentDb
99
internal sealed class ValueObjectDocument<TValueObject, TDto>
1010
where TValueObject : IEquatable<TValueObject>
1111
{
12-
public ValueObjectDocument(TValueObject valueObject, TDto dto)
13-
: this(dto, null, GetDocumentType())
14-
{
15-
}
16-
1712
[JsonConstructor]
1813
private ValueObjectDocument(TDto valueObject, string id, string type)
1914
{
@@ -30,6 +25,11 @@ private ValueObjectDocument(TDto valueObject, string id, string type)
3025

3126
public string Type { get; }
3227

28+
public static ValueObjectDocument<TValueObject, TDto> Create(TValueObject valueObject, TDto dto)
29+
{
30+
return new ValueObjectDocument<TValueObject, TDto>(dto, null, GetDocumentType());
31+
}
32+
3333
public static string GetDocumentType()
3434
{
3535
return typeof(TValueObject).Name;

src/Winton.DomainModelling.DocumentDb/ValueObjectFacade.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public async Task Create(TValueObject valueObject)
4545

4646
if (document == null)
4747
{
48-
document = new ValueObjectDocument<TValueObject, TDto>(valueObject, _dtoMapping(valueObject));
48+
document = ValueObjectDocument<TValueObject, TDto>.Create(valueObject, _dtoMapping(valueObject));
4949

5050
await _documentClient.CreateDocumentAsync(GetUri(), document);
5151
}

test/Winton.DomainModelling.DocumentDb.Tests/EntityDocumentTests.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public sealed class Dto : EntityDocumentTests
7070
private void ShouldReturnDto()
7171
{
7272
var expected = new TestDto(1);
73-
var document = new EntityDocument<TestEntity, EntityId, TestDto>(new TestEntity((EntityId)1), expected);
73+
EntityDocument<TestEntity, EntityId, TestDto> document =
74+
EntityDocument<TestEntity, EntityId, TestDto>.Create(new TestEntity((EntityId)1), expected);
7475

7576
TestDto dto = document.Dto;
7677

@@ -113,9 +114,8 @@ public sealed class IdProperty : EntityDocumentTests
113114
[Fact]
114115
private void ShouldReturnEntityTypeAndEntityId()
115116
{
116-
var document = new EntityDocument<TestEntity, EntityId, TestDto>(
117-
new TestEntity((EntityId)1),
118-
new TestDto(1));
117+
EntityDocument<TestEntity, EntityId, TestDto> document =
118+
EntityDocument<TestEntity, EntityId, TestDto>.Create(new TestEntity((EntityId)1), new TestDto(1));
119119

120120
string id = document.Id;
121121

@@ -136,9 +136,8 @@ public sealed class Type : EntityDocumentTests
136136
[Fact]
137137
private void ShouldReturnEntityType()
138138
{
139-
var document = new EntityDocument<TestEntity, EntityId, TestDto>(
140-
new TestEntity((EntityId)1),
141-
new TestDto(1));
139+
EntityDocument<TestEntity, EntityId, TestDto> document =
140+
EntityDocument<TestEntity, EntityId, TestDto>.Create(new TestEntity((EntityId)1), new TestDto(1));
142141

143142
string type = document.Type;
144143

test/Winton.DomainModelling.DocumentDb.Tests/ValueObjectDocumentTests.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public sealed class Dto : ValueObjectDocumentTests
5959
private void ShouldReturnValueObject()
6060
{
6161
var expected = new TestDto("A");
62-
var document = new ValueObjectDocument<TestValueObject, TestDto>(new TestValueObject("A"), expected);
62+
ValueObjectDocument<TestValueObject, TestDto> document =
63+
ValueObjectDocument<TestValueObject, TestDto>.Create(new TestValueObject("A"), expected);
6364

6465
TestDto dto = document.Dto;
6566

@@ -91,9 +92,8 @@ public sealed class Id : ValueObjectDocumentTests
9192
[Fact]
9293
private void ShouldDefaultToNull()
9394
{
94-
var document = new ValueObjectDocument<TestValueObject, TestDto>(
95-
new TestValueObject("A"),
96-
new TestDto("A"));
95+
ValueObjectDocument<TestValueObject, TestDto> document =
96+
ValueObjectDocument<TestValueObject, TestDto>.Create(new TestValueObject("A"), new TestDto("A"));
9797

9898
string id = document.Id;
9999

@@ -114,9 +114,8 @@ public sealed class Type : ValueObjectDocumentTests
114114
[Fact]
115115
private void ShouldReturnValueObjectType()
116116
{
117-
var document = new ValueObjectDocument<TestValueObject, TestDto>(
118-
new TestValueObject("A"),
119-
new TestDto("A"));
117+
ValueObjectDocument<TestValueObject, TestDto> document =
118+
ValueObjectDocument<TestValueObject, TestDto>.Create(new TestValueObject("A"), new TestDto("A"));
120119

121120
string type = document.Type;
122121

0 commit comments

Comments
 (0)