https://github.com/manusoft/democqrs
Sample of CQRS with MediatR - Asp.Net Web API dotnet-8
https://github.com/manusoft/democqrs
Last synced: 7 months ago
JSON representation
Sample of CQRS with MediatR - Asp.Net Web API dotnet-8
- Host: GitHub
- URL: https://github.com/manusoft/democqrs
- Owner: manusoft
- License: mit
- Created: 2024-06-12T07:26:24.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-10-03T05:34:24.000Z (over 1 year ago)
- Last Synced: 2024-12-27T14:44:30.485Z (over 1 year ago)
- Language: C#
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# DemoCQRS - ASP.NET Web API Project

DemoCQRS is an ASP.NET Web API project showcasing the implementation of the Command Query Responsibility Segregation (CQRS) pattern. The project demonstrates two versions: one without using MediatR and another with MediatR.
## Branches
### Branch: `samples/without-mediatr`
This branch contains a simple implementation of CQRS without using the MediatR library.
#### Movie.cs
```csharp
namespace DemoCQRS.Data;
public class Movie
{
public int Id { get; set; }
public string Name { get; set; }
}
```
#### AppDbContext.cs
```csharp
namespace DemoCQRS.Data;
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions options) : base(options) { }
public DbSet Movies { get; set; }
}
```
#### MovieController.cs
```csharp
namespace DemoCQRS.Controllers;
[Route("api/[controller]")]
[ApiController]
public class MovieController : ControllerBase
{
private readonly AppDbContext _context;
public MovieController(AppDbContext context)
{
_context = context;
}
[HttpPost]
public async Task Create(Movie movie)
{
_context.Movies.Add(movie);
await _context.SaveChangesAsync();
return Ok(movie.Id);
}
[HttpGet("{id}")]
public async Task Get(int id)
{
var movie = await _context.Movies.FindAsync(id);
return Ok(movie);
}
}
```
#### Program.cs
```csharp
builder.Services.AddDbContext(options => options.UseInMemoryDatabase("AppDb"));
```
### Branch: `master`
This branch includes the implementation of CQRS using the MediatR library.
#### Program.cs
```csharp
// Add MediatR services
builder.Services.AddMediatR(configuration =>
{
configuration.RegisterServicesFromAssembly(typeof(Program).Assembly);
});
```
#### CreateMovieCommand.cs
```csharp
namespace DemoCQRS.Features.Movies.Commands;
public sealed record CreateMovieCommand(string Name) : IRequest;
```
#### CreatePlayerCommandHandler.cs
```csharp
namespace DemoCQRS.Features.Movies.Commands;
public sealed class CreatePlayerCommandHandler : IRequestHandler
{
private readonly AppDbContext _context;
public CreatePlayerCommandHandler(AppDbContext context)
{
_context = context;
}
public async Task Handle(CreateMovieCommand request, CancellationToken cancellationToken)
{
var movie = new Movie() { Name = request.Name };
_context.Movies.Add(movie);
await _context.SaveChangesAsync();
return movie.Id;
}
}
```
#### GetMovieByIdQuery.cs
```csharp
namespace DemoCQRS.Features.Movies.Queries;
public sealed record GetMovieByIdQuery(int Id): IRequest;
```
#### GetMoviePlayerByIdQueryHandler.cs
```csharp
namespace DemoCQRS.Features.Movies.Queries;
public sealed class GetMoviePlayerByIdQueryHandler : IRequestHandler
{
private readonly AppDbContext _context;
public GetMoviePlayerByIdQueryHandler(AppDbContext context)
{
_context = context;
}
public async Task Handle(GetMovieByIdQuery request, CancellationToken cancellationToken)
{
var movie = await _context.Movies.FindAsync(request.Id);
return (movie);
}
}
```
#### MovieWithMediatRController.cs
```csharp
namespace DemoCQRS.Controllers;
[Route("api/[controller]")]
[ApiController]
public class MovieWithMediatRController : ControllerBase
{
private readonly ISender _sender;
public MovieWithMediatRController(ISender sender)
{
_sender = sender;
}
[HttpPost]
public async Task Create(CreateMovieCommand command)
{
var movieId = await _sender.Send(command);
return Ok(movieId);
}
[HttpGet("{id}")]
public async Task Get(int id)
{
var movie = await _sender.Send(new GetMovieByIdQuery(id));
if(movie == null)
return NotFound();
return Ok(movie);
}
}
```
## Setup Instructions
1. Clone the repository:
```bash
git clone
```
2. Switch to the desired branch:
```bash
git checkout
```
3. Restore the dependencies:
```bash
dotnet restore
```
4. Run the application:
```bash
dotnet run
```
## Summary
This project demonstrates how to implement CQRS in an ASP.NET Web API project, with and without the MediatR library. Each branch contains a complete example, allowing for a direct comparison between the two approaches.