Skip to content

Commit a24d6a7

Browse files
authored
Add possibility to use CQRS with MediatR (jhipster#626)
1 parent 379a232 commit a24d6a7

File tree

73 files changed

+2686
-140
lines changed

Some content is hidden

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

73 files changed

+2686
-140
lines changed

.github/workflows/test-integration-jwt.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
app-type:
1515
- jwt-with-angular-app
1616
- jwt-with-blazor-app
17+
- jwt-with-angular-cqrs-app
18+
- jwt-with-blazor-cqrs-app
1719
- jwt-with-mssql-app
1820
- jwt-with-mysql-app
1921
- jwt-with-postgres-app
@@ -22,8 +24,12 @@ jobs:
2224
include:
2325
- app-type: jwt-with-angular-app
2426
arg: 'import-jdl'
27+
- app-type: jwt-with-angular-cqrs-app
28+
arg: 'import-jdl'
2529
- app-type: jwt-with-blazor-app
2630
arg: 'import-jdl blazor'
31+
- app-type: jwt-with-blazor-cqrs-app
32+
arg: 'import-jdl blazor'
2733
- app-type: jwt-with-mssql-app
2834
arg: 'no'
2935
- app-type: jwt-with-mysql-app

.github/workflows/test-integration-oauth.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
os: [ubuntu-latest]
1414
app-type:
1515
- oauth-with-angular-app
16+
- oauth-with-angular-cqrs-app
1617
- oauth-with-mssql-app
1718
- oauth-with-mysql-app
1819
- oauth-with-oracle-app
@@ -22,6 +23,8 @@ jobs:
2223
include:
2324
- app-type: oauth-with-angular-app
2425
arg: 'import-jdl'
26+
- app-type: oauth-with-angular-cqrs-app
27+
arg: 'import-jdl'
2528
- app-type: oauth-with-mssql-app
2629
arg: 'no'
2730
- app-type: oauth-with-mysql-app

.vscode/launch.json

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,6 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7-
{
8-
"type": "node",
9-
"request": "launch",
10-
"name": "JHipster Mocha Tests",
11-
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
12-
"args": [
13-
"--timeout",
14-
"999999",
15-
"--reporter",
16-
"spec",
17-
"${workspaceFolder}/test"
18-
],
19-
"internalConsoleOptions": "openOnSessionStart"
20-
},
217
{
228
"type": "node",
239
"request": "launch",
@@ -40,7 +26,7 @@
4026
{
4127
"type": "node",
4228
"request": "launch",
43-
"name": "jhipster entity",
29+
"name": "jhipster entity",
4430
"program": "${workspaceFolder}/node_modules/generator-jhipster/cli/jhipster.js",
4531
"args": [
4632
"entity",
@@ -53,7 +39,34 @@
5339
{
5440
"type": "node",
5541
"request": "launch",
56-
"name": "jhipster info",
42+
"name": "jhipster jdl",
43+
"program": "${workspaceFolder}/node_modules/generator-jhipster/cli/jhipster.js",
44+
"args": [
45+
"jdl",
46+
"${workspaceFolder}/test-integration/samples/jdl-default/app.jdl",
47+
"--force"
48+
],
49+
"cwd": "${workspaceFolder}/test-integration/samples/jwt-with-angular-app",
50+
"console": "integratedTerminal"
51+
},
52+
{
53+
"type": "node",
54+
"request": "launch",
55+
"name": "jhipster mocha tests",
56+
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
57+
"args": [
58+
"--timeout",
59+
"999999",
60+
"--reporter",
61+
"spec",
62+
"${workspaceFolder}/test"
63+
],
64+
"internalConsoleOptions": "openOnSessionStart"
65+
},
66+
{
67+
"type": "node",
68+
"request": "launch",
69+
"name": "jhipster info",
5770
"program": "${workspaceFolder}/node_modules/generator-jhipster/cli/jhipster.js",
5871
"args": [
5972
"info"

docs/Features/cqrs.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
# CQRS
3+
4+
## Introduction
5+
6+
CQRS stands for **Command Query Responsibility Segregation**. You have the possibility to use this pattern thanks to [MediatR](https://github.com/jbogard/MediatR).
7+
8+
When generating your application, you can choose to use CQRS. Enabling it will generate a new layer for your commands and queries called Application.
9+
10+
```
11+
├── Namespace.Application
12+
│ ├── Commands - Your commands
13+
│ │ ├── MyEntity - Your entity
14+
│ │ │ ├── MyEntityCreateCommand - A command
15+
│ │ │ ├── MyEntityCreateCommandHandler - An handler for your command
16+
│ ├── Queries - Your queries
17+
│ │ ├── MyEntity - Your entity
18+
│ │ │ ├── MyEntityGetQuery - A query
19+
│ │ │ ├── MyEntityGetQueryHandler - An handler for your query
20+
├── Namespace.Crosscutting
21+
├── Namespace.Domain
22+
├── Namespace.Domain.Services
23+
├── Namespace.Dto
24+
├── Namespace.Infrastructure
25+
```
26+
27+
## Create your own Queries or Commands
28+
29+
In order to create your own commands and/or queries you have to create two classes :
30+
- A command/query
31+
- An handler for it
32+
33+
For instance, let's create a query `MyEntityGetQuery.cs`:
34+
35+
```csharp
36+
namespace MyCompany.Application.Queries {
37+
public class MyEntityGetQuery : IRequest<MyEntity>
38+
{
39+
public long Id { get; set; }
40+
}
41+
}
42+
```
43+
This Query should have an Id and returns a MyEntity object.
44+
Here's the handler `MyEntityGetQueryHandler.cs` :
45+
```csharp
46+
namespace MyCompany.Application.Queries {
47+
public class MyEntityGetQueryHandler : IRequestHandler<MyEntityGetQuery, MyEntity>
48+
{
49+
private IReadOnlyMyEntityRepository _myEntityRepository;
50+
51+
public MyEntityGetQueryHandler(IReadOnlyMyEntityRepository myEntityRepository)
52+
{
53+
_myEntityRepository = myEntityRepository;
54+
}
55+
56+
public Task<MyEntity> Handle(MyEntityGetQuery request,
57+
CancellationToken cancellationToken)
58+
{
59+
return _myEntityRepository.QueryHelper()
60+
.GetOneAsync(myEntity => myEntity.Id == request.Id);
61+
}
62+
}
63+
}
64+
```
65+
Please note that we are using a **ReadOnlyRepository** rather than a service in order to do the segregation between Commands and Queries. Lastly, create your routing method within your controller :
66+
```csharp
67+
[HttpGet("my-entity/{id}")]
68+
public async Task<IActionResult> GetMyEntity([FromRoute] long id)
69+
{
70+
var result = await _mediator.Send(new MyEntityGetQuery { Id = id });
71+
return ActionResultUtil.WrapOrNotFound(result);
72+
}
73+
```

generators/entity-server/files.js

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const utils = require('../utils');
2525
const INTERPOLATE_REGEX = baseConstants.INTERPOLATE_REGEX;
2626
const SERVER_SRC_DIR = constants.SERVER_SRC_DIR;
2727
const SERVER_TEST_DIR = constants.SERVER_TEST_DIR;
28+
const PROJECT_APPLICATION_SUFFIX = constants.PROJECT_APPLICATION_SUFFIX;
2829
const PROJECT_CROSSCUTTING_SUFFIX = constants.PROJECT_CROSSCUTTING_SUFFIX;
2930
const PROJECT_SERVICE_SUFFIX = constants.PROJECT_SERVICE_SUFFIX;
3031
const PROJECT_INFRASTRUCTURE_SUFFIX = constants.PROJECT_INFRASTRUCTURE_SUFFIX;
@@ -59,6 +60,20 @@ const serverFiles = {
5960
generator.entityClass
6061
)}Repository.cs`,
6162
},
63+
{
64+
file: 'Project.Domain/Repositories/Interfaces/IReadOnlyEntityRepository.cs',
65+
renameTo: generator =>
66+
`${generator.pascalizedBaseName}${
67+
constants.PROJECT_DOMAIN_SUFFIX
68+
}/Repositories/Interfaces/IReadOnly${generator.asEntity(generator.entityClass)}Repository.cs`,
69+
},
70+
{
71+
file: 'Project.Infrastructure/Data/Repositories/ReadOnlyEntityRepository.cs',
72+
renameTo: generator =>
73+
`${generator.pascalizedBaseName}${PROJECT_INFRASTRUCTURE_SUFFIX}/Data/Repositories/ReadOnly${generator.asEntity(
74+
generator.entityClass
75+
)}Repository.cs`,
76+
},
6277
],
6378
},
6479
{
@@ -70,6 +85,82 @@ const serverFiles = {
7085
},
7186
],
7287
},
88+
{
89+
condition: generator => generator.cqrsEnabled === true,
90+
path: SERVER_SRC_DIR,
91+
templates: [
92+
{
93+
file: 'Project.Application/Queries/EntityGetQuery.cs',
94+
renameTo: generator =>
95+
`${generator.pascalizedBaseName}${PROJECT_APPLICATION_SUFFIX}/Queries/${generator.asEntity(
96+
generator.entityClass
97+
)}/${generator.asEntity(generator.entityClass)}GetQuery.cs`,
98+
},
99+
{
100+
file: 'Project.Application/Queries/EntityGetQueryHandler.cs',
101+
renameTo: generator =>
102+
`${generator.pascalizedBaseName}${PROJECT_APPLICATION_SUFFIX}/Queries/${generator.asEntity(
103+
generator.entityClass
104+
)}/${generator.asEntity(generator.entityClass)}GetQueryHandler.cs`,
105+
},
106+
{
107+
file: 'Project.Application/Queries/EntityGetAllQuery.cs',
108+
renameTo: generator =>
109+
`${generator.pascalizedBaseName}${PROJECT_APPLICATION_SUFFIX}/Queries/${generator.asEntity(
110+
generator.entityClass
111+
)}/${generator.asEntity(generator.entityClass)}GetAllQuery.cs`,
112+
},
113+
{
114+
file: 'Project.Application/Queries/EntityGetAllQueryHandler.cs',
115+
renameTo: generator =>
116+
`${generator.pascalizedBaseName}${PROJECT_APPLICATION_SUFFIX}/Queries/${generator.asEntity(
117+
generator.entityClass
118+
)}/${generator.asEntity(generator.entityClass)}GetAllQueryHandler.cs`,
119+
},
120+
{
121+
file: 'Project.Application/Commands/EntityDeleteCommand.cs',
122+
renameTo: generator =>
123+
`${generator.pascalizedBaseName}${PROJECT_APPLICATION_SUFFIX}/Commands/${generator.asEntity(
124+
generator.entityClass
125+
)}/${generator.asEntity(generator.entityClass)}DeleteCommand.cs`,
126+
},
127+
{
128+
file: 'Project.Application/Commands/EntityDeleteCommandHandler.cs',
129+
renameTo: generator =>
130+
`${generator.pascalizedBaseName}${PROJECT_APPLICATION_SUFFIX}/Commands/${generator.asEntity(
131+
generator.entityClass
132+
)}/${generator.asEntity(generator.entityClass)}DeleteCommandHandler.cs`,
133+
},
134+
{
135+
file: 'Project.Application/Commands/EntityCreateCommand.cs',
136+
renameTo: generator =>
137+
`${generator.pascalizedBaseName}${PROJECT_APPLICATION_SUFFIX}/Commands/${generator.asEntity(
138+
generator.entityClass
139+
)}/${generator.asEntity(generator.entityClass)}CreateCommand.cs`,
140+
},
141+
{
142+
file: 'Project.Application/Commands/EntityCreateCommandHandler.cs',
143+
renameTo: generator =>
144+
`${generator.pascalizedBaseName}${PROJECT_APPLICATION_SUFFIX}/Commands/${generator.asEntity(
145+
generator.entityClass
146+
)}/${generator.asEntity(generator.entityClass)}CreateCommandHandler.cs`,
147+
},
148+
{
149+
file: 'Project.Application/Commands/EntityUpdateCommand.cs',
150+
renameTo: generator =>
151+
`${generator.pascalizedBaseName}${PROJECT_APPLICATION_SUFFIX}/Commands/${generator.asEntity(
152+
generator.entityClass
153+
)}/${generator.asEntity(generator.entityClass)}UpdateCommand.cs`,
154+
},
155+
{
156+
file: 'Project.Application/Commands/EntityUpdateCommandHandler.cs',
157+
renameTo: generator =>
158+
`${generator.pascalizedBaseName}${PROJECT_APPLICATION_SUFFIX}/Commands/${generator.asEntity(
159+
generator.entityClass
160+
)}/${generator.asEntity(generator.entityClass)}UpdateCommandHandler.cs`,
161+
},
162+
],
163+
},
73164
{
74165
condition: generator => generator.dto === 'mapstruct',
75166
path: SERVER_SRC_DIR,
@@ -118,7 +209,7 @@ const serverFiles = {
118209
],
119210
service: [
120211
{
121-
condition: generator => generator.service === 'serviceImpl',
212+
condition: generator => generator.service === 'serviceImpl' && generator.cqrsEnabled !== true,
122213
path: SERVER_SRC_DIR,
123214
templates: [
124215
{
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<%#
2+
Copyright 2019-2021 the original author or authors from the JHipster project.
3+
4+
This file is part of the JHipster project, see https://www.jhipster.tech/
5+
for more information.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
-%>
19+
<%
20+
let hasDto = dto === 'mapstruct';
21+
%>
22+
using <%= namespace %>.Domain;
23+
using MediatR;
24+
<%_ if (hasDto) { _%>
25+
using <%= namespace %>.Dto;
26+
<%_ } _%>
27+
28+
namespace <%= namespace %>.Application.Commands
29+
{
30+
public class <%= pascalizedEntityClass %>CreateCommand : IRequest<<%= pascalizedEntityClass %>>
31+
{
32+
public <%= hasDto ? asDto(pascalizedEntityClass) : pascalizedEntityClass %> <%= hasDto ? asDto(pascalizedEntityClass) : pascalizedEntityClass %> { get; set; }
33+
}
34+
}

0 commit comments

Comments
 (0)