Skip to content

Query bus feature #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Library/Extensions/CommandHandlersRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace Library.Command
{
public static class CommandHandlersRegister
public static class CommandQueryHandlersRegister
{
public static IServiceCollection AddCommandHandlers(this IServiceCollection services, Type assemblyType)
public static IServiceCollection AddCommandQueryHandlers(this IServiceCollection services, Type assemblyType, Type commandQueryHandler)
{
var handlers = assemblyType.Assembly.GetTypes()
.Where(t => t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandHandler<>)));
.Where(t => t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == commandQueryHandler));

foreach (var handler in handlers)
services.AddScoped(handler.GetInterfaces()
.First(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandHandler<>)), handler);
.First(i => i.IsGenericType && i.GetGenericTypeDefinition() == commandQueryHandler), handler);

return services;
}
Expand Down
7 changes: 7 additions & 0 deletions Library/Query/IQueryBus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Library.Query
{
public interface IQueryBus
{
Task<TResponse> Dispatch<TRequest, TResponse>(TRequest command);
}
}
7 changes: 7 additions & 0 deletions Library/Query/IQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Library.Query
{
public interface IQueryHandler<TRequest, TResponse>
{
Task<TResponse> Handle(TRequest command);
}
}
19 changes: 19 additions & 0 deletions Library/Query/QueryBus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.Extensions.DependencyInjection;

namespace Library.Query
{
public class QueryBus : IQueryBus
{
private readonly IServiceProvider _provider;
public QueryBus(IServiceProvider provider)
{
_provider = provider;
}

public async Task<TResponse> Dispatch<TRequest, TResponse>(TRequest query)
{
var service = _provider.GetRequiredService(typeof(IQueryHandler<TRequest, TResponse>)) as IQueryHandler<TRequest, TResponse>;
return await service.Handle(query);
}
}
}
7 changes: 7 additions & 0 deletions WebApi/Application/Query/ProductQueryRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace WebApi.Application.Query
{
public class ProductQueryRequest
{
public int Id { get; set; }
}
}
7 changes: 7 additions & 0 deletions WebApi/Application/Query/ProductQueryResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace WebApi.Application.Query
{
public class ProductQueryResponse
{
public string ProductName { get; set; }
}
}
21 changes: 21 additions & 0 deletions WebApi/Application/QueryHandlers/ProductQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Library.Query;
using WebApi.Application.Query;

namespace WebApi.Application.QueryHandler
{
public class ProductQueryHandler : IQueryHandler<ProductQueryRequest, ProductQueryResponse>
{
public async Task<ProductQueryResponse> Handle(ProductQueryRequest query)
{
//Query from database
//_repository.GetProductName(query.Id);

var productName = "Laptop";

return new ProductQueryResponse
{
ProductName = productName,
};
}
}
}
19 changes: 15 additions & 4 deletions WebApi/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using Library.Command;
using Library.Query;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using WebApi.Application.Command;
using WebApi.Application.Query;
using WebApi.Models;

namespace WebApi.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly ICommandBus _bus;
public HomeController(ILogger<HomeController> logger, ICommandBus bus)
private readonly ICommandBus _commandBus;
private readonly IQueryBus _queryBus;
public HomeController(ILogger<HomeController> logger, ICommandBus commandBus, IQueryBus queryBus)
{
_logger = logger;
_bus = bus;
_commandBus = commandBus;
_queryBus = queryBus;
}

public IActionResult Index()
Expand All @@ -24,7 +28,14 @@ public IActionResult Index()
Name = "Laptop;"
};

_bus.Dispatch(productCommand);
_commandBus.Dispatch(productCommand);

var productQueryRequest = new ProductQueryRequest
{
Id = 1
};

_queryBus.Dispatch<ProductQueryRequest, ProductQueryResponse>(productQueryRequest);

return View();
}
Expand Down
5 changes: 4 additions & 1 deletion WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Library.Command;
using Library.Query;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();

builder.Services.AddCommandHandlers(typeof(Program));
builder.Services.AddCommandQueryHandlers(typeof(Program), typeof(ICommandHandler<>));
builder.Services.AddCommandQueryHandlers(typeof(Program), typeof(IQueryHandler<,>));
builder.Services.AddScoped<ICommandBus, CommandBus>();
builder.Services.AddScoped<IQueryBus, QueryBus>();

var app = builder.Build();

Expand Down