Last active
August 29, 2015 14:07
-
-
Save Royce/519621353565e25c5c4e to your computer and use it in GitHub Desktop.
CQ Infrastructure
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 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>()); | |
} | |
} | |
} |
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 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>(); | |
} | |
} | |
} |
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 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