Skip to content

Instantly share code, notes, and snippets.

@Royce
Last active August 29, 2015 14:07
Show Gist options
  • Save Royce/519621353565e25c5c4e to your computer and use it in GitHub Desktop.
Save Royce/519621353565e25c5c4e to your computer and use it in GitHub Desktop.
CQ Infrastructure
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Prject.Config;
using Project.Database.Entities;
using NHibernate;
namespace Project.Queries
{
// I just want to get all the units from the db.
public class UnitsQuery : IQuery<IEnumerable<Unit>>
{
}
public class UnitsQueryHandler : IQueryHandler<UnitsQuery, IEnumerable<Unit>>
{
private readonly ISession _session;
public UnitsQueryHandler(ISession session)
{
_session = session;
}
public async Task<IEnumerable<Unit>> ExecuteAsync(UnitsQuery query)
{
return await Task.FromResult(_session.QueryOver<Unit>().List<Unit>());
}
}
}
using Autofac;
namespace Project.Config.Autofac
{
public class CommandQueryModule : Module
{
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
var thisAssembly = typeof(CommandQueryModule).Assembly;
builder.RegisterAssemblyTypes(thisAssembly)
.Where(t => t.IsClosedTypeOf(typeof(ICommandHandler<>)))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
builder.RegisterType<CommandExecutor>().As<ICommandExecutor>();
builder.RegisterAssemblyTypes(thisAssembly)
.Where(t => t.IsClosedTypeOf(typeof(IQueryHandler<,>)))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
builder.RegisterType<QueryExecutor>().As<IQueryExecutor>();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Autofac;
using NHibernate;
namespace Project.Config
{
public interface IQuery<TResult> { }
public interface IQueryHandler<in TQuery, TResult> where TQuery : IQuery<TResult>
{
Task<TResult> ExecuteAsync(TQuery query);
}
public interface IQueryExecutor
{
Task<TResult> ExecuteAsync<TQuery, TResult>(TQuery query) where TQuery : IQuery<TResult>;
}
public class QueryExecutor : IQueryExecutor
{
private readonly ILifetimeScope _currentScope;
private readonly ISession _databaseSession;
public QueryExecutor(ILifetimeScope currentScope, ISession databaseSession)
{
_currentScope = currentScope;
_databaseSession = databaseSession;
}
public async Task<TResult> ExecuteAsync<TQuery, TResult>(TQuery query) where TQuery : IQuery<TResult>
{
var asyncExecutors = _currentScope.Resolve<IEnumerable<IQueryHandler<TQuery, TResult>>>().ToList();
if (!asyncExecutors.Any())
throw new Exception(string.Format("No query handlers were available for {0} returning {1}", typeof(TQuery).Name, typeof(TResult).Name));
if (asyncExecutors.Count > 1)
throw new Exception(string.Format("Too many query handlers were available for {0} returning {1}", typeof(TQuery).Name, typeof(TResult).Name));
return await asyncExecutors.First().ExecuteAsync(query);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment