Skip to content

Commit 445b200

Browse files
committed
phase 1: refactor base libs
1 parent bfcd533 commit 445b200

File tree

60 files changed

+722
-505
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+722
-505
lines changed
0 Bytes
Binary file not shown.

.vs/netcorekit/v15/Server/sqlite3/db.lock

Whitespace-only changes.
-9.47 MB
Binary file not shown.
Binary file not shown.
-3.95 MB
Binary file not shown.

.vs/netcorekit/v16/.suo

10.5 KB
Binary file not shown.

.vs/netcorekit/v16/Server/sqlite3/db.lock

Whitespace-only changes.
-4.73 MB
Binary file not shown.

samples/BiMonetaryApi/Startup.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.Extensions.DependencyInjection;
55
using NetCoreKit.RestTemplate.MongoDb;
6-
using MyExchangeService = NetCoreKit.Samples.BiMonetaryApi.Rpc.ExchangeService;
6+
using static NetCoreKit.Samples.BiMonetaryApi.Rpc.ExchangeService;
77

88
namespace NetCoreKit.Samples.BiMonetaryApi
99
{
@@ -15,8 +15,8 @@ public void ConfigureServices(IServiceCollection services)
1515
{
1616
var config = resolver.GetService<IConfiguration>();
1717
var channel = new Channel(config["RpcClients:ExchangeService"], ChannelCredentials.Insecure);
18-
var client = new MyExchangeService.ExchangeServiceClient(channel);
19-
services.AddSingleton(typeof(MyExchangeService.ExchangeServiceClient), client);
18+
var client = new ExchangeServiceClient(channel);
19+
services.AddSingleton(typeof(ExchangeServiceClient), client);
2020
});
2121
}
2222

samples/BiMonetaryApi/v1/Controllers/TickerController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using NetCoreKit.Domain;
66
using NetCoreKit.Infrastructure.Mongo;
77
using NetCoreKit.Samples.BiMonetaryApi.Rpc;
8-
using MyExchangeService = NetCoreKit.Samples.BiMonetaryApi.Rpc.ExchangeService;
8+
using static NetCoreKit.Samples.BiMonetaryApi.Rpc.ExchangeService;
99

1010
namespace NetCoreKit.Samples.BiMonetaryApi.v1.Controllers
1111
{
@@ -48,12 +48,12 @@ public class TickerController : ControllerBase
4848
{
4949
private readonly IQueryRepositoryFactory _repositoryFactory;
5050
private readonly IUnitOfWorkAsync _uow;
51-
private readonly MyExchangeService.ExchangeServiceClient _exchangeServiceClient;
51+
private readonly ExchangeServiceClient _exchangeServiceClient;
5252

5353
public TickerController(
5454
IQueryRepositoryFactory repositoryFactory,
5555
IUnitOfWorkAsync uow,
56-
MyExchangeService.ExchangeServiceClient exchangeServiceClient)
56+
ExchangeServiceClient exchangeServiceClient)
5757
{
5858
_repositoryFactory = repositoryFactory;
5959
_uow = uow;

samples/TodoApi/v1/UseCases/AddTask/RequestHandler.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using NetCoreKit.Infrastructure.EfCore.Extensions;
88
using NetCoreKit.Samples.TodoAPI.Domain;
99
using NetCoreKit.Samples.TodoAPI.Extensions;
10+
using NetCoreKit.Samples.TodoAPI.Infrastructure.Db;
1011
using Task = NetCoreKit.Samples.TodoAPI.Domain.Task;
1112

1213
namespace NetCoreKit.Samples.TodoAPI.v1.UseCases.AddTask
@@ -26,10 +27,11 @@ public RequestHandler(
2627

2728
public override async Task<AddTaskResponse> Handle(AddTaskRequest request, CancellationToken cancellationToken)
2829
{
29-
var commandRepository = CommandFactory.RepositoryAsync<Domain.Project>();
30-
var queryRepository = QueryFactory.QueryEfRepository<Domain.Project>();
30+
var commandRepository = CommandFactory.RepositoryAsync<Project>();
31+
var queryRepository = QueryFactory.QueryRepository<Project>();
3132

32-
var project = await queryRepository.GetByIdAsync(request.ProjectId, q => q.Include(x => x.Tasks), false);
33+
var project =
34+
await queryRepository.GetByIdAsync<TodoListDbContext, Project>(request.ProjectId, q => q.Include(x => x.Tasks), false);
3335
if (project == null)
3436
throw new Exception($"Couldn't found the project#{request.ProjectId}.");
3537

samples/TodoApi/v1/UseCases/ClearTasks/RequestHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using NetCoreKit.Domain;
66
using NetCoreKit.Infrastructure.AspNetCore.CleanArch;
77
using NetCoreKit.Infrastructure.EfCore.Extensions;
8+
using NetCoreKit.Samples.TodoAPI.Infrastructure.Db;
89

910
namespace NetCoreKit.Samples.TodoAPI.v1.UseCases.ClearTasks
1011
{
@@ -19,9 +20,10 @@ public override async Task<ClearTasksResponse> Handle(ClearTasksRequest request,
1920
CancellationToken cancellationToken)
2021
{
2122
var projectRepository = CommandFactory.RepositoryAsync<Domain.Project>();
22-
var queryRepository = QueryFactory.QueryEfRepository<Domain.Project>();
23+
var queryRepository = QueryFactory.QueryRepository<Domain.Project>();
2324

24-
var project = await queryRepository.GetByIdAsync(request.ProjectId, q => q.Include(x => x.Tasks), false);
25+
var project =
26+
await queryRepository.GetByIdAsync<TodoListDbContext, Domain.Project>(request.ProjectId, q => q.Include(x => x.Tasks), false);
2527
if (project == null)
2628
throw new Exception($"Couldn't found the project#{request.ProjectId}.");
2729

samples/TodoApi/v1/UseCases/DeleteTask/RequestHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using NetCoreKit.Domain;
66
using NetCoreKit.Infrastructure.AspNetCore.CleanArch;
77
using NetCoreKit.Infrastructure.EfCore.Extensions;
8+
using NetCoreKit.Samples.TodoAPI.Infrastructure.Db;
89

910
namespace NetCoreKit.Samples.TodoAPI.v1.UseCases.DeleteTask
1011
{
@@ -19,9 +20,9 @@ public override async Task<DeleteTaskResponse> Handle(DeleteTaskRequest request,
1920
CancellationToken cancellationToken)
2021
{
2122
var projectRepository = CommandFactory.RepositoryAsync<Domain.Project>();
22-
var queryRepository = QueryFactory.QueryEfRepository<Domain.Project>();
23+
var queryRepository = QueryFactory.QueryRepository<Domain.Project>();
2324

24-
var project = await queryRepository.GetByIdAsync(request.ProjectId, q => q.Include(x => x.Tasks), false);
25+
var project = await queryRepository.GetByIdAsync<TodoListDbContext, Domain.Project>(request.ProjectId, q => q.Include(x => x.Tasks), false);
2526
if (project == null) throw new Exception($"Couldn't find the project#{request.ProjectId}.");
2627

2728
project.RemoveTask(request.TaskId);

samples/TodoApi/v1/UseCases/GetTasks/RequestHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NetCoreKit.Infrastructure.AspNetCore.CleanArch;
77
using NetCoreKit.Infrastructure.EfCore.Extensions;
88
using NetCoreKit.Samples.TodoAPI.Extensions;
9+
using NetCoreKit.Samples.TodoAPI.Infrastructure.Db;
910

1011
namespace NetCoreKit.Samples.TodoAPI.v1.UseCases.GetTasks
1112
{
@@ -19,9 +20,9 @@ public RequestHandler(IQueryRepositoryFactory queryRepositoryFactory)
1920
public override async Task<GetTasksResponse> Handle(GetTasksRequest request,
2021
CancellationToken cancellationToken)
2122
{
22-
var queryRepository = QueryFactory.QueryEfRepository<Domain.Project>();
23+
var queryRepository = QueryFactory.QueryRepository<Domain.Project>();
2324

24-
var result = await queryRepository.GetByIdAsync(request.ProjectId, q => q.Include(x => x.Tasks));
25+
var result = await queryRepository.GetByIdAsync<TodoListDbContext, Domain.Project>(request.ProjectId, q => q.Include(x => x.Tasks));
2526
if (result == null) throw new Exception($"Couldn't find project#{request.ProjectId}.");
2627

2728
return new GetTasksResponse

samples/TodoApi/v1/UseCases/UpdateTask/RequestHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NetCoreKit.Infrastructure.AspNetCore.CleanArch;
77
using NetCoreKit.Infrastructure.EfCore.Extensions;
88
using NetCoreKit.Samples.TodoAPI.Extensions;
9+
using NetCoreKit.Samples.TodoAPI.Infrastructure.Db;
910

1011
namespace NetCoreKit.Samples.TodoAPI.v1.UseCases.UpdateTask
1112
{
@@ -20,9 +21,9 @@ public override async Task<UpdateTaskResponse> Handle(UpdateTaskRequest request,
2021
CancellationToken cancellationToken)
2122
{
2223
var commandRepository = CommandFactory.RepositoryAsync<Domain.Project>();
23-
var queryRepository = QueryFactory.QueryEfRepository<Domain.Project>();
24+
var queryRepository = QueryFactory.QueryRepository<Domain.Project>();
2425

25-
var project = await queryRepository.GetByIdAsync(request.ProjectId, q => q.Include(x => x.Tasks), false);
26+
var project = await queryRepository.GetByIdAsync<TodoListDbContext, Domain.Project>(request.ProjectId, q => q.Include(x => x.Tasks), false);
2627
if (project == null) throw new Exception($"Couldn't find project#{request.ProjectId}.");
2728

2829
project.UpdateTask(request.TaskId, request.Title, request.Order ?? 1, request.Completed ?? false);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using static NetCoreKit.Utils.Helpers.IdHelper;
5+
using static NetCoreKit.Utils.Helpers.DateTimeHelper;
6+
7+
namespace NetCoreKit.Domain
8+
{
9+
public interface IAggregateRoot : IAggregateRootWithType<Guid>
10+
{
11+
}
12+
13+
public interface IAggregateRootWithType<TId> : IEntityWithId<TId>
14+
{
15+
IAggregateRootWithType<TId> ApplyEvent(IEvent payload);
16+
List<IEvent> GetUncommittedEvents();
17+
void ClearUncommittedEvents();
18+
IAggregateRootWithType<TId> RemoveEvent(IEvent @event);
19+
IAggregateRootWithType<TId> AddEvent(IEvent uncommittedEvent);
20+
IAggregateRootWithType<TId> RegisterHandler<T>(Action<T> handler);
21+
}
22+
23+
public abstract class AggregateRootBase : AggregateRootWithIdBase<Guid>, IAggregateRoot
24+
{
25+
protected AggregateRootBase() : base(GenerateId())
26+
{
27+
}
28+
}
29+
30+
public abstract class AggregateRootWithIdBase<TId> : EntityWithIdBase<TId>, IAggregateRootWithType<TId>
31+
{
32+
private readonly IDictionary<Type, Action<object>> _handlers = new ConcurrentDictionary<Type, Action<object>>();
33+
private readonly List<IEvent> _uncommittedEvents = new List<IEvent>();
34+
35+
protected AggregateRootWithIdBase(TId id) : base(id)
36+
{
37+
Created = GenerateDateTime();
38+
}
39+
40+
public int Version { get; protected set; }
41+
42+
public IAggregateRootWithType<TId> AddEvent(IEvent uncommittedEvent)
43+
{
44+
_uncommittedEvents.Add(uncommittedEvent);
45+
ApplyEvent(uncommittedEvent);
46+
return this;
47+
}
48+
49+
public IAggregateRootWithType<TId> ApplyEvent(IEvent payload)
50+
{
51+
if (!_handlers.ContainsKey(payload.GetType()))
52+
return this;
53+
_handlers[payload.GetType()]?.Invoke(payload);
54+
Version++;
55+
return this;
56+
}
57+
58+
public void ClearUncommittedEvents()
59+
{
60+
_uncommittedEvents.Clear();
61+
}
62+
63+
public List<IEvent> GetUncommittedEvents()
64+
{
65+
return _uncommittedEvents;
66+
}
67+
68+
public IAggregateRootWithType<TId> RegisterHandler<T>(Action<T> handler)
69+
{
70+
_handlers.Add(typeof(T), e => handler((T)e));
71+
return this;
72+
}
73+
74+
public IAggregateRootWithType<TId> RemoveEvent(IEvent @event)
75+
{
76+
if (_uncommittedEvents.Find(e => e == @event) != null)
77+
_uncommittedEvents.Remove(@event);
78+
return this;
79+
}
80+
}
81+
}

src/NetCoreKit.Domain/Criterion.cs renamed to src/NetCoreKit.Domain/Dto.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
using System;
2-
31
namespace NetCoreKit.Domain
42
{
5-
public class Criterion
3+
/// <summary>
4+
/// Supertype for all Dto types
5+
/// </summary>
6+
public interface IDto
7+
{
8+
}
9+
10+
public class Criterion : IDto
611
{
712
private const int MaxPageSize = 50;
813
private const int ConfigurablePageSize = 10;
@@ -44,7 +49,7 @@ public string SortOrder
4449
public Criterion SetPageSize(int pageSize)
4550
{
4651
if (pageSize <= 0)
47-
throw new Exception("PageSize could not be less than zero.");
52+
throw new ValidationException("PageSize could not be less than zero.");
4853

4954
PageSize = pageSize;
5055
return this;
@@ -53,7 +58,7 @@ public Criterion SetPageSize(int pageSize)
5358
public Criterion SetCurrentPage(int currentPage)
5459
{
5560
if (currentPage <= 0)
56-
throw new Exception("CurrentPage could not be less than zero.");
61+
throw new ValidationException("CurrentPage could not be less than zero.");
5762

5863
CurrentPage = currentPage;
5964
return this;

src/NetCoreKit.Domain/Entity.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
using static NetCoreKit.Utils.Helpers.IdHelper;
4+
using static NetCoreKit.Utils.Helpers.DateTimeHelper;
5+
6+
namespace NetCoreKit.Domain
7+
{
8+
public interface IEntity : IEntityWithId<Guid>
9+
{
10+
}
11+
12+
/// <inheritdoc />
13+
/// <summary>
14+
/// Supertype for all Entity types
15+
/// </summary>
16+
public interface IEntityWithId<TId> : IIdentityWithId<TId>
17+
{
18+
}
19+
20+
public abstract class EntityBase : EntityWithIdBase<Guid>
21+
{
22+
protected EntityBase() : base(GenerateId())
23+
{
24+
}
25+
26+
protected EntityBase(Guid id) : base(id)
27+
{
28+
}
29+
}
30+
31+
/// <inheritdoc />
32+
/// <summary>
33+
/// Source: https://github.com/VaughnVernon/IDDD_Samples_NET
34+
/// </summary>
35+
public abstract class EntityWithIdBase<TId> : IEntityWithId<TId>
36+
{
37+
protected EntityWithIdBase(TId id)
38+
{
39+
Id = id;
40+
Created = GenerateDateTime();
41+
}
42+
43+
public DateTime Created { get; protected set; }
44+
45+
public DateTime Updated { get; protected set; }
46+
47+
[Key] public TId Id { get; protected set; }
48+
}
49+
}

0 commit comments

Comments
 (0)