Skip to content

Commit b2ed58d

Browse files
committed
Issue rsm-hcd#34 and rsm-hcd#17: Implemented CreateAsync
1 parent d64bdb8 commit b2ed58d

File tree

10 files changed

+102
-17
lines changed

10 files changed

+102
-17
lines changed

src/Conductors/RepositoryConductor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public partial class RepositoryConductor<T> : Conductor, IRepositoryConductor<T>
4646
/// <param name="item">Item to be created</param>
4747
/// <param name="createdById">Id of user creating the item</param>
4848
/// <returns></returns>
49+
[Obsolete("This method is deprecated in favor of its async counter part", false)]
4950
public virtual IResult<T> Create(T item, long? createdById = default(long?)) => _createConductor.Create(item, createdById);
5051

5152
/// <summary>

src/Conductors/RepositoryConductorAsync.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,16 @@ public RepositoryConductor(
7272

7373
#endregion Constructor
7474

75-
75+
/// <inheritdoc />
7676
public Task<IResult<List<T>>> BulkCreateAsync(IEnumerable<T> items, long? createdById = null, CancellationToken cancellationToken = default) =>
7777
_createConductor.BulkCreateAsync(items, createdById, cancellationToken);
7878

79+
/// <inheritdoc />
7980
public Task<IResult<List<T>>> BulkCreateDistinctAsync<TKey>(IEnumerable<T> items, System.Func<T, TKey> property, long? createdById = null, CancellationToken cancellationToken = default) =>
8081
_createConductor.BulkCreateDistinctAsync(items, property, createdById, cancellationToken);
82+
83+
/// <inheritdoc />
84+
public Task<IResult<T>> CreateAsync(T item, long? createdById = null, CancellationToken cancellationToken = default) =>
85+
_createConductor.CreateAsync(item, createdById, cancellationToken);
8186
}
8287
}

src/Conductors/RepositoryCreateConductor.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,15 @@ public partial class RepositoryCreateConductor<T> : Conductor, IRepositoryCreate
2323
return _repository.BulkCreate(items, createdById);
2424
}
2525

26-
/// <summary>
27-
/// Ability to create entities using a list in a single bulk operation without duplicates.
28-
/// </summary>
29-
/// <param name="items">List of items to create</param>
30-
/// <param name="property">Property used to remove duplicates</param>
31-
/// <param name="createdById">Id of user creating the items</param>
32-
/// <returns></returns>
26+
/// <inheritdoc />
27+
[Obsolete("This method is deprecated in favor of its async counter part", false)]
3328
public IResult<List<T>> BulkCreateDistinct<TKey>(IEnumerable<T> items, Func<T, TKey> property, long? createdById = null)
3429
{
3530
return _repository.BulkCreateDistinct(items, property, createdById);
3631
}
3732

38-
/// <summary>
39-
/// Ability to create an entity
40-
/// </summary>
41-
/// <param name="item">Item to be created</param>
42-
/// <param name="createdById">Id of user creating the item</param>
43-
/// <returns></returns>
33+
/// <inheritdoc />
34+
[Obsolete("This method is deprecated in favor of its async counter part", false)]
4435
public virtual IResult<T> Create(T item, long? createdById = default(long?))
4536
{
4637
return _repository.Create(item, createdById);

src/Conductors/RepositoryCreateConductorAsync.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,13 @@ public Task<IResult<List<T>>> BulkCreateDistinctAsync<TKey>(IEnumerable<T> items
5353
if (!items.Any()) throw new ArgumentException("An empty collection was provided", nameof(items));
5454
return _repository.BulkCreateDistinctAsync(items, property, createdById, cancellationToken);
5555
}
56+
57+
/// <inheritdoc />
58+
public virtual Task<IResult<T>> CreateAsync(T item, long? createdById = null, CancellationToken cancellationToken = default)
59+
{
60+
cancellationToken.ThrowIfCancellationRequested();
61+
if (item == null) throw new ArgumentNullException(nameof(item));
62+
return _repository.CreateAsync(item, createdById);
63+
}
5664
}
5765
}

src/Core/Interfaces/Conductors/IRepositoryCreateConductor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public partial interface IRepositoryCreateConductor<T>
4141
[Obsolete("This method is deprecated in favor of its async counter part", false)]
4242
IResult<List<T>> BulkCreateDistinct<TKey>(IEnumerable<T> items, Func<T, TKey> property, long? createdById = null);
4343

44+
/// <summary>
45+
/// Ability to create an entity
46+
/// </summary>
47+
/// <param name="item">Item to be created</param>
48+
/// <param name="createdById">Id of user creating the item</param>
49+
/// <returns></returns>
50+
[Obsolete("This method is deprecated in favor of its async counter part", false)]
4451
IResult<T> Create(T item, long? createdById = null);
4552
IResult<List<T>> Create(IEnumerable<T> items, long? createdById = null);
4653

src/Core/Interfaces/Conductors/IRepositoryCreateConductorAsync.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,13 @@ public partial interface IRepositoryCreateConductor<T>
3131
/// <returns></returns>
3232
Task<IResult<List<T>>> BulkCreateDistinctAsync<TKey>(IEnumerable<T> items, Func<T, TKey> property, long? createdById = null, CancellationToken cancellationToken = default);
3333

34+
/// <summary>
35+
/// Ability to create an entity
36+
/// </summary>
37+
/// <param name="item">Item to be created</param>
38+
/// <param name="createdById">Id of user creating the item</param>
39+
/// <param name="cancellationToken">a token allowing aborting of this request</param>
40+
/// <returns></returns>
41+
Task<IResult<T>> CreateAsync(T item, long? createdById = null, CancellationToken cancellationToken = default);
3442
}
3543
}

src/Core/Interfaces/Data/IRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public partial interface IRepository<T>
7171
/// <param name="item">Item to be created</param>
7272
/// <param name="createdById">Id of user creating the item</param>
7373
/// <returns></returns>
74+
[Obsolete("This method is deprecated in favor of its async counter part", false)]
7475
IResult<T> Create(T item, long? createdById = null);
7576

7677
/// <summary>

src/Core/Interfaces/Data/IRepositoryAsync.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,13 @@ public partial interface IRepository<T>
2929
/// <returns></returns>
3030
Task<IResult<List<T>>> BulkCreateDistinctAsync<TKey>(IEnumerable<T> items, Func<T, TKey> property, long? createdById = null, CancellationToken cancellationToken = default);
3131

32+
/// <summary>
33+
/// Ability to create an entity
34+
/// </summary>
35+
/// <param name="item">Item to be created</param>
36+
/// <param name="createdById">Id of user creating the item</param>
37+
/// <param name="cancellationToken">a token allowing aborting of this request</param>
38+
/// <returns></returns>
39+
Task<IResult<T>> CreateAsync(T item, long? createdById = null, CancellationToken cancellationToken = default);
3240
}
3341
}

test/Conductors.Tests/RepositoryConductorTests/BulkCreateDistinctAsyncShould.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public async Task Throw_Argument_Null_Exception_When_Given_Null_Input()
3737
var respositoryConductor = SetupSut(repositoryMock);
3838

3939
// Act & Assert
40-
await Assert.ThrowsAsync<ArgumentNullException>(() => respositoryConductor.BulkCreateAsync(null));
40+
await Assert.ThrowsAsync<ArgumentNullException>(() => respositoryConductor.BulkCreateDistinctAsync(null, (x) => x.Id));
4141
}
4242

4343
[Fact]
@@ -48,7 +48,7 @@ public async Task Throw_Argument_Exception_When_Given_Empty_Input()
4848
var respositoryConductor = SetupSut(repositoryMock);
4949

5050
// Act & Assert
51-
await Assert.ThrowsAsync<ArgumentException>(() => respositoryConductor.BulkCreateAsync(new List<UserStub>()));
51+
await Assert.ThrowsAsync<ArgumentException>(() => respositoryConductor.BulkCreateDistinctAsync(new List<UserStub>(), (x) => x.Id));
5252
}
5353

5454
[Fact]
@@ -61,7 +61,7 @@ public async Task Throw_Stop_If_Canceled()
6161
var cancellationToken = cancellationTokenSource.Token;
6262
cancellationTokenSource.Cancel();
6363
// Act & Assert
64-
await Assert.ThrowsAsync<OperationCanceledException>(() => respositoryConductor.BulkCreateAsync(new List<UserStub>(), 5, cancellationToken));
64+
await Assert.ThrowsAsync<OperationCanceledException>(() => respositoryConductor.BulkCreateDistinctAsync(new List<UserStub>(), (x) => x.Id, 5, cancellationToken));
6565
}
6666
}
6767
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using AndcultureCode.CSharp.Core.Interfaces.Conductors;
6+
using AndcultureCode.CSharp.Testing.Models.Stubs;
7+
using Xunit;
8+
using Xunit.Abstractions;
9+
10+
namespace AndcultureCode.CSharp.Conductors.Tests.RepositoryConductorTests
11+
{
12+
public class CreateAsyncShould : ProjectUnitTest
13+
{
14+
public CreateAsyncShould(ITestOutputHelper output) : base(output)
15+
{
16+
}
17+
18+
private IRepositoryConductor<UserStub> SetupSut(
19+
IRepositoryMock<UserStub> repositoryMock
20+
)
21+
{
22+
var repositoryCreateConductor = new RepositoryCreateConductor<UserStub>(repositoryMock.Object);
23+
return new RepositoryConductor<UserStub>(
24+
createConductor: repositoryCreateConductor,
25+
deleteConductor: null,
26+
readConductor: null,
27+
updateConductor: null
28+
);
29+
}
30+
31+
[Fact]
32+
public async Task Throw_Argument_Null_Exception_When_Given_Null_Input()
33+
{
34+
// Arrange
35+
var repositoryMock = new IRepositoryMock<UserStub>();
36+
var respositoryConductor = SetupSut(repositoryMock);
37+
38+
// Act & Assert
39+
await Assert.ThrowsAsync<ArgumentNullException>(() => respositoryConductor.CreateAsync(null));
40+
}
41+
42+
[Fact]
43+
public async Task Throw_Stop_If_Canceled()
44+
{
45+
// Arrange
46+
var repositoryMock = new IRepositoryMock<UserStub>();
47+
var respositoryConductor = SetupSut(repositoryMock);
48+
var cancellationTokenSource = new CancellationTokenSource();
49+
var cancellationToken = cancellationTokenSource.Token;
50+
cancellationTokenSource.Cancel();
51+
var userStub = new UserStub();
52+
// Act & Assert
53+
await Assert.ThrowsAsync<OperationCanceledException>(() => respositoryConductor.CreateAsync(userStub, 5, cancellationToken));
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)