https://github.com/nosratifarhad/mediatr
Separating commands and queries in User Mediator In .Net 6
https://github.com/nosratifarhad/mediatr
command cqrs dotnet dotnet-core dotnetcore mediatr query
Last synced: about 2 months ago
JSON representation
Separating commands and queries in User Mediator In .Net 6
- Host: GitHub
- URL: https://github.com/nosratifarhad/mediatr
- Owner: nosratifarhad
- Created: 2023-03-19T15:59:58.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-01T10:03:29.000Z (over 2 years ago)
- Last Synced: 2025-06-24T08:43:06.113Z (about 1 year ago)
- Topics: command, cqrs, dotnet, dotnet-core, dotnetcore, mediatr, query
- Language: C#
- Homepage:
- Size: 126 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MediatR
## More details about the architecture .
* 1 - Application Folder is Application Layer
* In This Layer , Have Command And Queries And Handlers .
* 2 - Service Folder is Service Layer
* For Data Persistence And Use ORM (Entity FrameWork).
### first You need to install the following packages :
```
NuGet\Install-Package MediatR.Extensions.Microsoft.DependencyInjection
```
### You must pass the "Assembly" when adding the MediatR service to project , like this
```csharp
//services.AddMediatR(Assembly.GetExecutingAssembly());
services.AddMediatR(typeof(Program).Assembly);
```
### And the Final level for use "MediatR" :
* Go To Application Folder (Layer) And Create Command Or Query and Use down Code in Application Layer .
* You should use "IRequest" for command and query requests , like this :
### Sample Command And CommandHandler
```csharp
// Use IRequest In Command Model
public class CreateProductCommand : IRequest
{
public string ProductTitle { get; set; }
public string ProductName { get; set; }
}
// Use IRequestHandler In Command Handler
public class CreateProductCommandHandler : IRequestHandler
{
public async Task Handle(CreateProductCommand request, CancellationToken cancellationToken)
{
var createProductDto = GenereateCreateProductDtoFromaCommand(request);
await _productService.CreateProduct(createProductDto).ConfigureAwait(false);
return Unit.Value;
}
}
```
### Sample Query And QueryHandler
```csharp
// Use IRequest In Query Model
public class GetProductQuery : IRequest>
{
public int ProductId { get; set; }
}
// Use IRequest In Query Handler
public class GetProductQueryHandler : IRequestHandler>
{
public async Task> Handle(GetProductQuery request, CancellationToken cancellationToken)
{
var productVMList = await _productService.GetProduct().ConfigureAwait(false);
return productVMList;
}
}
```

