Skip to content

Commit 8ebaffe

Browse files
authored
Merge pull request #14 from codepb/develop
Merge develop to master or 1.1.0 release
2 parents fbfca1d + a884185 commit 8ebaffe

14 files changed

+260
-71
lines changed

src/DDDToolkit.API/AggregateController.cs

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,62 +6,33 @@
66

77
namespace DDDToolkit.API
88
{
9-
public abstract class AggregateController<T, TId> : Controller where T : class, IAggregateRoot<TId>
9+
public abstract class AggregateController<T, TId> : Controller, IAggregateController<T, TId> where T : class, IAggregateRoot<TId>
1010
{
11-
private readonly IApplicationService<T, TId> _applicationService;
11+
private readonly IReadableAggregateController<T, TId> _readableAggregateController;
12+
private readonly IWritableAggregateController<T, TId> _writableAggregateController;
1213

1314
protected AggregateController(IApplicationService<T, TId> applicationService)
1415
{
15-
_applicationService = applicationService;
16+
_readableAggregateController = new ReadableAggregateControllerImpl<T, TId>(applicationService);
17+
_writableAggregateController = new WritableAggregateControllerImpl<T, TId>(applicationService);
1618
}
1719

1820
[HttpGet]
1921
[Route("{id}")]
20-
public async Task<T> GetById(TId id)
21-
{
22-
return await _applicationService.GetById(id);
23-
}
22+
public Task<T> GetById(TId id) => _readableAggregateController.GetById(id);
2423

2524
[HttpGet]
26-
public async Task<IReadOnlyCollection<T>> GetAll()
27-
{
28-
return await _applicationService.GetAll();
29-
}
25+
public Task<IReadOnlyCollection<T>> GetAll() => _readableAggregateController.GetAll();
3026

3127
[HttpPost]
32-
public async Task<IActionResult> Create([FromBody] T aggregate)
33-
{
34-
if (!ModelState.IsValid)
35-
{
36-
return BadRequest(ModelState);
37-
}
38-
39-
await _applicationService.Add(aggregate);
40-
41-
return NoContent();
42-
}
28+
public Task<IActionResult> Create([FromBody] T aggregate) => _writableAggregateController.Create(aggregate);
4329

4430
[HttpPut]
4531
[Route("{id}")]
46-
public async Task<IActionResult> Edit(TId id, [FromBody] T aggregate)
47-
{
48-
if (!ModelState.IsValid)
49-
{
50-
return BadRequest(ModelState);
51-
}
52-
53-
await _applicationService.Update(id, aggregate);
54-
55-
return NoContent();
56-
}
32+
public Task<IActionResult> Edit(TId id, [FromBody] T aggregate) => _writableAggregateController.Edit(id, aggregate);
5733

5834
[HttpDelete]
5935
[Route("{id}")]
60-
public async Task<IActionResult> Delete(TId id)
61-
{
62-
await _applicationService.Delete(id);
63-
64-
return NoContent();
65-
}
36+
public Task<IActionResult> Delete(TId id) => _writableAggregateController.Delete(id);
6637
}
6738
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using DDDToolkit.Core.Interfaces;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
namespace DDDToolkit.API
7+
{
8+
public interface IAggregateController<T, TId>
9+
: IReadableAggregateController<T, TId>
10+
, IWritableAggregateController<T, TId>
11+
where T : class, IAggregateRoot<TId>
12+
{
13+
}
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using DDDToolkit.Core.Interfaces;
4+
5+
namespace DDDToolkit.API
6+
{
7+
public interface IReadableAggregateController<T, TId> where T : class, IAggregateRoot<TId>
8+
{
9+
Task<IReadOnlyCollection<T>> GetAll();
10+
Task<T> GetById(TId id);
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Threading.Tasks;
2+
using DDDToolkit.Core.Interfaces;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace DDDToolkit.API
6+
{
7+
public interface IWritableAggregateController<T, TId> where T : class, IAggregateRoot<TId>
8+
{
9+
Task<IActionResult> Create([FromBody] T aggregate);
10+
Task<IActionResult> Delete(TId id);
11+
Task<IActionResult> Edit(TId id, [FromBody] T aggregate);
12+
}
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using DDDToolkit.ApplicationLayer;
2+
using DDDToolkit.Core.Interfaces;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace DDDToolkit.API
10+
{
11+
public abstract class ReadableAggregateController<T, TId> : Controller, IReadableAggregateController<T, TId> where T : class, IAggregateRoot<TId>
12+
{
13+
private readonly IReadableApplicationService<T, TId> _applicationService;
14+
15+
protected ReadableAggregateController(IReadableApplicationService<T, TId> applicationService)
16+
{
17+
_applicationService = applicationService;
18+
}
19+
20+
[HttpGet]
21+
[Route("{id}")]
22+
public async Task<T> GetById(TId id)
23+
{
24+
return await _applicationService.GetById(id);
25+
}
26+
27+
[HttpGet]
28+
public async Task<IReadOnlyCollection<T>> GetAll()
29+
{
30+
return await _applicationService.GetAll();
31+
}
32+
}
33+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using DDDToolkit.ApplicationLayer;
2+
using DDDToolkit.Core.Interfaces;
3+
4+
namespace DDDToolkit.API
5+
{
6+
internal class ReadableAggregateControllerImpl<T, TId> : ReadableAggregateController<T, TId> where T : class, IAggregateRoot<TId>
7+
{
8+
internal ReadableAggregateControllerImpl(IReadableApplicationService<T, TId> applicationService) : base(applicationService)
9+
{
10+
}
11+
}
12+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using DDDToolkit.ApplicationLayer;
2+
using DDDToolkit.Core.Interfaces;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace DDDToolkit.API
10+
{
11+
public abstract class WritableAggregateController<T, TId> : Controller, IWritableAggregateController<T, TId> where T : class, IAggregateRoot<TId>
12+
{
13+
private readonly IWritableApplicationService<T, TId> _applicationService;
14+
15+
protected WritableAggregateController(IWritableApplicationService<T, TId> applicationService)
16+
{
17+
_applicationService = applicationService;
18+
}
19+
20+
[HttpPost]
21+
public async Task<IActionResult> Create([FromBody] T aggregate)
22+
{
23+
if (!ModelState.IsValid)
24+
{
25+
return BadRequest(ModelState);
26+
}
27+
28+
await _applicationService.Add(aggregate);
29+
30+
return NoContent();
31+
}
32+
33+
[HttpPut]
34+
[Route("{id}")]
35+
public async Task<IActionResult> Edit(TId id, [FromBody] T aggregate)
36+
{
37+
if (!ModelState.IsValid)
38+
{
39+
return BadRequest(ModelState);
40+
}
41+
42+
await _applicationService.Update(id, aggregate);
43+
44+
return NoContent();
45+
}
46+
47+
[HttpDelete]
48+
[Route("{id}")]
49+
public async Task<IActionResult> Delete(TId id)
50+
{
51+
await _applicationService.Delete(id);
52+
53+
return NoContent();
54+
}
55+
}
56+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using DDDToolkit.Core.Interfaces;
2+
using DDDToolkit.ApplicationLayer;
3+
4+
namespace DDDToolkit.API
5+
{
6+
internal class WritableAggregateControllerImpl<T, TId> : WritableAggregateController<T, TId> where T : class, IAggregateRoot<TId>
7+
{
8+
internal WritableAggregateControllerImpl(IWritableApplicationService<T, TId> applicationService) : base(applicationService)
9+
{
10+
}
11+
}
12+
}

src/DDDToolkit.ApplicationLayer/ApplicationService.cs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,23 @@ namespace DDDToolkit.ApplicationLayer
88
public class ApplicationService<T, TId> : IApplicationService<T, TId>
99
where T : class, IAggregateRoot<TId>
1010
{
11-
protected readonly IUnitOfWork _unitOfWork;
11+
private IReadableApplicationService<T, TId> _readableApplicationService;
12+
private IWritableApplicationService<T, TId> _writableApplicationService;
1213

1314
public ApplicationService(IUnitOfWork unitOfWork)
1415
{
15-
_unitOfWork = unitOfWork;
16+
_readableApplicationService = new ReadableApplicationService<T, TId>(unitOfWork);
17+
_writableApplicationService = new WritableApplicationService<T, TId>(unitOfWork);
1618
}
1719

18-
public virtual Task<T> GetById(TId id)
19-
{
20-
return _unitOfWork.Repository<T,TId>().GetById(id);
21-
}
20+
public virtual Task<T> GetById(TId id) => _readableApplicationService.GetById(id);
2221

23-
public virtual Task<IReadOnlyCollection<T>> GetAll()
24-
{
25-
return _unitOfWork.Repository<T, TId>().GetAll();
26-
}
22+
public virtual Task<IReadOnlyCollection<T>> GetAll() => _readableApplicationService.GetAll();
2723

28-
public virtual async Task Add(T aggregate)
29-
{
30-
await _unitOfWork.Repository<T, TId>().Add(aggregate);
31-
await _unitOfWork.Save();
32-
}
24+
public virtual Task Add(T aggregate) => _writableApplicationService.Add(aggregate);
3325

34-
public virtual async Task Update(TId id, T aggregate)
35-
{
36-
aggregate.SetId(id);
37-
await _unitOfWork.Repository<T, TId>().Update(aggregate);
38-
await _unitOfWork.Save();
39-
}
26+
public virtual Task Update(TId id, T aggregate) => _writableApplicationService.Update(id, aggregate);
4027

41-
public virtual async Task Delete(TId id)
42-
{
43-
await _unitOfWork.Repository<T, TId>().Remove(id);
44-
await _unitOfWork.Save();
45-
}
28+
public virtual Task Delete(TId id) => _writableApplicationService.Delete(id);
4629
}
4730
}

src/DDDToolkit.ApplicationLayer/IApplicationService.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
namespace DDDToolkit.ApplicationLayer
77
{
88
public interface IApplicationService<T, TId>
9-
where T : IAggregateRoot<TId>
9+
: IReadableApplicationService<T, TId>,
10+
IWritableApplicationService<T, TId>
11+
where T : class, IAggregateRoot<TId>
1012
{
11-
Task Add(T aggregate);
12-
Task Delete(TId id);
13-
Task<IReadOnlyCollection<T>> GetAll();
14-
Task<T> GetById(TId id);
15-
Task Update(TId id, T aggregate);
1613
}
1714
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using DDDToolkit.Core.Interfaces;
4+
5+
namespace DDDToolkit.ApplicationLayer
6+
{
7+
public interface IReadableApplicationService<T, TId> where T : class, IAggregateRoot<TId>
8+
{
9+
Task<IReadOnlyCollection<T>> GetAll();
10+
Task<T> GetById(TId id);
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Threading.Tasks;
2+
using DDDToolkit.Core.Interfaces;
3+
4+
namespace DDDToolkit.ApplicationLayer
5+
{
6+
public interface IWritableApplicationService<T, TId> where T : class, IAggregateRoot<TId>
7+
{
8+
Task Add(T aggregate);
9+
Task Delete(TId id);
10+
Task Update(TId id, T aggregate);
11+
}
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using DDDToolkit.ApplicationLayer.Transactions;
2+
using DDDToolkit.Core.Interfaces;
3+
using System.Collections.Generic;
4+
using System.Threading.Tasks;
5+
6+
namespace DDDToolkit.ApplicationLayer
7+
{
8+
public class ReadableApplicationService<T, TId> : IReadableApplicationService<T, TId>
9+
where T : class, IAggregateRoot<TId>
10+
{
11+
protected readonly IUnitOfWork _unitOfWork;
12+
13+
public ReadableApplicationService(IUnitOfWork unitOfWork)
14+
{
15+
_unitOfWork = unitOfWork;
16+
}
17+
18+
public virtual Task<T> GetById(TId id)
19+
{
20+
return _unitOfWork.ReadableRepository<T, TId>().GetById(id);
21+
}
22+
23+
public virtual Task<IReadOnlyCollection<T>> GetAll()
24+
{
25+
return _unitOfWork.ReadableRepository<T, TId>().GetAll();
26+
}
27+
}
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using DDDToolkit.ApplicationLayer.Transactions;
2+
using DDDToolkit.Core.Interfaces;
3+
using System.Threading.Tasks;
4+
5+
namespace DDDToolkit.ApplicationLayer
6+
{
7+
public class WritableApplicationService<T, TId> : IWritableApplicationService<T, TId> where T : class, IAggregateRoot<TId>
8+
{
9+
protected readonly IUnitOfWork _unitOfWork;
10+
11+
public WritableApplicationService(IUnitOfWork unitOfWork)
12+
{
13+
_unitOfWork = unitOfWork;
14+
}
15+
public virtual async Task Add(T aggregate)
16+
{
17+
await _unitOfWork.WritableRepository<T, TId>().Add(aggregate);
18+
await _unitOfWork.Save();
19+
}
20+
21+
public virtual async Task Update(TId id, T aggregate)
22+
{
23+
aggregate.SetId(id);
24+
await _unitOfWork.WritableRepository<T, TId>().Update(aggregate);
25+
await _unitOfWork.Save();
26+
}
27+
28+
public virtual async Task Delete(TId id)
29+
{
30+
await _unitOfWork.WritableRepository<T, TId>().Remove(id);
31+
await _unitOfWork.Save();
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)