Skip to content

Commit 9ba6335

Browse files
authored
Merge pull request #1 from aligholipour/QueryBusFeature
Query bus feature
2 parents 9035829 + 9eb2358 commit 9ba6335

File tree

9 files changed

+91
-9
lines changed

9 files changed

+91
-9
lines changed

Library/Extensions/CommandHandlersRegister.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
namespace Library.Command
44
{
5-
public static class CommandHandlersRegister
5+
public static class CommandQueryHandlersRegister
66
{
7-
public static IServiceCollection AddCommandHandlers(this IServiceCollection services, Type assemblyType)
7+
public static IServiceCollection AddCommandQueryHandlers(this IServiceCollection services, Type assemblyType, Type commandQueryHandler)
88
{
99
var handlers = assemblyType.Assembly.GetTypes()
10-
.Where(t => t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandHandler<>)));
10+
.Where(t => t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == commandQueryHandler));
1111

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

1616
return services;
1717
}

Library/Query/IQueryBus.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Library.Query
2+
{
3+
public interface IQueryBus
4+
{
5+
Task<TResponse> Dispatch<TRequest, TResponse>(TRequest command);
6+
}
7+
}

Library/Query/IQueryHandler.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Library.Query
2+
{
3+
public interface IQueryHandler<TRequest, TResponse>
4+
{
5+
Task<TResponse> Handle(TRequest command);
6+
}
7+
}

Library/Query/QueryBus.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
namespace Library.Query
4+
{
5+
public class QueryBus : IQueryBus
6+
{
7+
private readonly IServiceProvider _provider;
8+
public QueryBus(IServiceProvider provider)
9+
{
10+
_provider = provider;
11+
}
12+
13+
public async Task<TResponse> Dispatch<TRequest, TResponse>(TRequest query)
14+
{
15+
var service = _provider.GetRequiredService(typeof(IQueryHandler<TRequest, TResponse>)) as IQueryHandler<TRequest, TResponse>;
16+
return await service.Handle(query);
17+
}
18+
}
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace WebApi.Application.Query
2+
{
3+
public class ProductQueryRequest
4+
{
5+
public int Id { get; set; }
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace WebApi.Application.Query
2+
{
3+
public class ProductQueryResponse
4+
{
5+
public string ProductName { get; set; }
6+
}
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Library.Query;
2+
using WebApi.Application.Query;
3+
4+
namespace WebApi.Application.QueryHandler
5+
{
6+
public class ProductQueryHandler : IQueryHandler<ProductQueryRequest, ProductQueryResponse>
7+
{
8+
public async Task<ProductQueryResponse> Handle(ProductQueryRequest query)
9+
{
10+
//Query from database
11+
//_repository.GetProductName(query.Id);
12+
13+
var productName = "Laptop";
14+
15+
return new ProductQueryResponse
16+
{
17+
ProductName = productName,
18+
};
19+
}
20+
}
21+
}

WebApi/Controllers/HomeController.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
using Library.Command;
2+
using Library.Query;
23
using Microsoft.AspNetCore.Mvc;
34
using System.Diagnostics;
45
using WebApi.Application.Command;
6+
using WebApi.Application.Query;
57
using WebApi.Models;
68

79
namespace WebApi.Controllers
810
{
911
public class HomeController : Controller
1012
{
1113
private readonly ILogger<HomeController> _logger;
12-
private readonly ICommandBus _bus;
13-
public HomeController(ILogger<HomeController> logger, ICommandBus bus)
14+
private readonly ICommandBus _commandBus;
15+
private readonly IQueryBus _queryBus;
16+
public HomeController(ILogger<HomeController> logger, ICommandBus commandBus, IQueryBus queryBus)
1417
{
1518
_logger = logger;
16-
_bus = bus;
19+
_commandBus = commandBus;
20+
_queryBus = queryBus;
1721
}
1822

1923
public IActionResult Index()
@@ -24,7 +28,14 @@ public IActionResult Index()
2428
Name = "Laptop;"
2529
};
2630

27-
_bus.Dispatch(productCommand);
31+
_commandBus.Dispatch(productCommand);
32+
33+
var productQueryRequest = new ProductQueryRequest
34+
{
35+
Id = 1
36+
};
37+
38+
_queryBus.Dispatch<ProductQueryRequest, ProductQueryResponse>(productQueryRequest);
2839

2940
return View();
3041
}

WebApi/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using Library.Command;
2+
using Library.Query;
23

34
var builder = WebApplication.CreateBuilder(args);
45

56
builder.Services.AddControllersWithViews();
67

7-
builder.Services.AddCommandHandlers(typeof(Program));
8+
builder.Services.AddCommandQueryHandlers(typeof(Program), typeof(ICommandHandler<>));
9+
builder.Services.AddCommandQueryHandlers(typeof(Program), typeof(IQueryHandler<,>));
810
builder.Services.AddScoped<ICommandBus, CommandBus>();
11+
builder.Services.AddScoped<IQueryBus, QueryBus>();
912

1013
var app = builder.Build();
1114

0 commit comments

Comments
 (0)