Created
January 12, 2025 21:04
-
-
Save n-j-m/5e87d57b16b7c41ca15395beb862b38a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using MediatR; | |
| using TypeConst.Cli.Interfaces; | |
| namespace TypeConst.Cli.Models; | |
| public class Entity1 : IName, IRequest | |
| { | |
| public string Name { get; set; } = default!; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using MediatR; | |
| using TypeConst.Cli.Interfaces; | |
| using TypeConst.Cli.Models; | |
| namespace TypeConst.Cli.Handlers; | |
| public class Entity1Handler(IServiceWorker<Entity1> worker) : IRequestHandler<Entity1> | |
| { | |
| public async Task Handle(Entity1 request, CancellationToken cancellationToken) | |
| { | |
| await worker.DoWorkAsync(request, cancellationToken); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using TypeConst.Cli.Interfaces; | |
| using TypeConst.Cli.Models; | |
| namespace TypeConst.Cli.Services; | |
| public class Entity1ServiceWorker : IServiceWorker<Entity1> | |
| { | |
| public async Task DoWorkAsync(Entity1 entity, CancellationToken cancellationToken = default) | |
| { | |
| Console.WriteLine("Entity1: {0}", entity.Name); | |
| await Task.Delay(1000, cancellationToken); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using MediatR; | |
| using TypeConst.Cli.Interfaces; | |
| namespace TypeConst.Cli.Models; | |
| public class Entity2 : IName, IRequest | |
| { | |
| public string Name { get; set; } = default!; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using MediatR; | |
| using TypeConst.Cli.Interfaces; | |
| using TypeConst.Cli.Models; | |
| namespace TypeConst.Cli.Handlers; | |
| public class Entity2Handler(IServiceWorker<Entity2> worker) : IRequestHandler<Entity2> | |
| { | |
| public async Task Handle(Entity2 request, CancellationToken cancellationToken) | |
| { | |
| await worker.DoWorkAsync(request, cancellationToken); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using TypeConst.Cli.Interfaces; | |
| using TypeConst.Cli.Models; | |
| namespace TypeConst.Cli.Services; | |
| public class Entity2ServiceWorker : IServiceWorker<Entity2> | |
| { | |
| public async Task DoWorkAsync(Entity2 entity, CancellationToken cancellationToken = default) | |
| { | |
| Console.WriteLine("Entity2: {0}", entity.Name); | |
| await Task.Delay(2000, cancellationToken); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace TypeConst.Cli.Interfaces; | |
| public interface IName | |
| { | |
| string Name { get; set; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace TypeConst.Cli.Interfaces; | |
| public interface IServiceWorker<TEntity> where TEntity : IName | |
| { | |
| Task DoWorkAsync(TEntity entity, CancellationToken cancellationToken = default); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // See https://aka.ms/new-console-template for more information | |
| using MediatR; | |
| using Microsoft.Extensions.DependencyInjection; | |
| using TypeConst.Cli.Interfaces; | |
| using TypeConst.Cli.Models; | |
| using TypeConst.Cli.Services; | |
| Console.WriteLine("Hello, World!"); | |
| var services = new ServiceCollection() | |
| .AddSingleton<IServiceWorker<Entity1>>(_ => new Entity1ServiceWorker()) | |
| .AddSingleton<IServiceWorker<Entity2>>(_ => new Entity2ServiceWorker()); | |
| services.AddMediatR(c => c.RegisterServicesFromAssembly(typeof(Program).Assembly)); | |
| using var provider = services.BuildServiceProvider(); | |
| List<IName> names = [ | |
| new Entity1 { Name = "Entity 1" }, | |
| new Entity2 { Name = "Entity 2" }, | |
| ]; | |
| var mediator = provider.GetRequiredService<IMediator>(); | |
| foreach (var name in names) | |
| { | |
| await mediator.Send(name); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment