-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathQueryBus.cs
35 lines (32 loc) · 1 KB
/
QueryBus.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using Autofac;
using Serilog;
using System;
using System.Threading.Tasks;
namespace Watchman.Cqrs
{
public class QueryBus : IQueryBus
{
private readonly IComponentContext _context;
public QueryBus(IComponentContext context)
{
this._context = context;
}
public W Execute<W>(IQuery<W> query) where W : IQueryResult
{
//Log.Debug("Query: {query}", query);
if (query == null)
{
throw new ArgumentNullException(nameof(query),
$"Query: '{typeof(W)}' can not be null.");
}
var handlerType = typeof(IQueryHandler<,>)
.MakeGenericType(query.GetType(), typeof(W));
dynamic handler = this._context.Resolve(handlerType);
return handler.Handle((dynamic) query);
}
public Task<W> ExecuteAsync<W>(IQuery<W> query) where W : IQueryResult
{
return Task.FromResult(this.Execute(query));
}
}
}