Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 6db8e81

Browse files
committed
Aggiunta documentazione MediatR
1 parent 6122bd1 commit 6db8e81

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# MediatR configuration
2+
3+
This README shows how to implement a Command (Insert) and a Query (GetAll), the implementations of the GetById, Update and Delete methods are missing.
4+
5+
A complete implementation example is available in this repository https://github.com/AngeloDotNet/Sample.MediatRV2
6+
7+
## Registering services at Startup
8+
9+
```csharp
10+
public Startup(IConfiguration configuration)
11+
{
12+
Configuration = configuration;
13+
}
14+
15+
public IConfiguration Configuration { get; }
16+
17+
public void ConfigureServices(IServiceCollection services)
18+
{
19+
// Replace the Program value, with a suitable handler (example: CreatePersonHandler),
20+
// if the MediatR implementations are not in the same assembly as the Program class
21+
services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));
22+
}
23+
```
24+
25+
## Example of the PersonEntity class
26+
27+
```csharp
28+
public class PersonEntity
29+
{
30+
public int Id { get; set; }
31+
public Guid UserId { get; set; }
32+
public string Cognome { get; set; }
33+
public string Nome { get; set; }
34+
public string Email { get; set; }
35+
}
36+
```
37+
38+
## Example of the CreatePersonHandler class
39+
40+
```csharp
41+
public class CreatePersonHandler : NET6CustomLibrary.MediatR.ICommandHandler<CreatePersonCommand, PersonEntity>
42+
{
43+
private readonly ILogger<CreatePersonHandler> logger;
44+
private readonly IPeopleService peopleService;
45+
private readonly IMapper mapper;
46+
47+
public CreatePersonHandler(ILogger<CreatePersonHandler> logger, IPeopleService peopleService, IMapper mapper)
48+
{
49+
this.logger = logger;
50+
this.peopleService = peopleService;
51+
this.mapper = mapper;
52+
}
53+
54+
public async Task<PersonEntity> Handle(CreatePersonCommand command, CancellationToken cancellationToken)
55+
{
56+
var input = mapper.Map<PersonEntity>(command);
57+
58+
await peopleService.CreatePersonAsync(input);
59+
60+
var response = new PersonEntity()
61+
{
62+
Id = input.Id,
63+
UserId = input.UserId,
64+
Cognome = input.Cognome,
65+
Nome = input.Nome,
66+
Email = input.Email
67+
};
68+
69+
return response;
70+
}
71+
}
72+
```
73+
74+
## Example of the CreatePersonCommand class
75+
76+
```csharp
77+
public class CreatePersonCommand : NET6CustomLibrary.MediatR.ICommand<PersonEntity>
78+
{
79+
public Guid UserId { get; set; }
80+
public string Cognome { get; set; }
81+
public string Nome { get; set; }
82+
public string Email { get; set; }
83+
84+
public CreatePersonCommand(PersonCreateInputModel inputModel)
85+
{
86+
UserId = inputModel.UserId;
87+
Cognome = inputModel.Cognome;
88+
Nome = inputModel.Nome;
89+
Email = inputModel.Email;
90+
}
91+
}
92+
```
93+
94+
95+
## Example of the GetPeopleHandler class
96+
97+
```csharp
98+
public class GetPeopleHandler : NET6CustomLibrary.MediatR.IQueryHandler<GetPeopleListQuery, List<PersonEntity>>
99+
{
100+
private readonly ILogger<GetPeopleHandler> logger;
101+
private readonly IPeopleService peopleService;
102+
103+
public GetPeopleHandler(ILogger<GetPeopleHandler> logger, IPeopleService peopleService)
104+
{
105+
this.logger = logger;
106+
this.peopleService = peopleService;
107+
}
108+
109+
public async Task<List<PersonEntity>> Handle(GetPeopleListQuery request, CancellationToken cancellationToken)
110+
{
111+
var result = await peopleService.GetPeopleAsync();
112+
113+
return result;
114+
}
115+
}
116+
```
117+
118+
## Example of the GetPeopleListQuery class
119+
120+
```csharp
121+
public class GetPeopleListQuery : NET6CustomLibrary.MediatR.IQuery<List<PersonEntity>>
122+
{
123+
}
124+
```

0 commit comments

Comments
 (0)