Last active
December 14, 2015 05:59
-
-
Save JefClaes/5039087 to your computer and use it in GitHub Desktop.
Queries
This file contains 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
namespace Infrastructure | |
{ | |
public interface IQueryHandler<TIn, TOut> | |
where TIn : class | |
where TOut : class | |
{ | |
TOut Execute(TIn query); | |
} | |
} | |
namespace Infrastructure | |
{ | |
public class SomethingLikeAFacade : ISomethingLikeAFacade | |
{ | |
private readonly IKernel _kernel; | |
public SomethingLikeAFacade(IKernel kernel) | |
{ | |
_kernel = kernel; | |
} | |
public void ExecuteCommand<T>(T command) where T : class | |
{ | |
var handler = _kernel.Get<ICommandHandler<T>>(); | |
handler.Handle(command); | |
_kernel.Get<IDocumentSession>().SaveChanges(); | |
} | |
public TOut ExecuteQuery<TIn, TOut>(TIn query) where TIn : class where TOut : class | |
{ | |
var handler = _kernel.Get<IQueryHandler<TIn, TOut>>(); | |
return handler.Execute(query); | |
} | |
} | |
} | |
namespace Queries | |
{ | |
public class GetCategoriesQuery | |
{ | |
public GetCategoriesQuery(string someSearchValue) | |
{ | |
Guard.ForEmptyString(someSearchValue); | |
SomeSearchValue = someSearchValue; | |
} | |
public string SomeSearchValue { get; private set; } | |
} | |
} | |
namespace Queries | |
{ | |
public class GetCategoriesQueriesHandler : IQueryHandler<GetCategoriesQuery, IEnumerable<Category>> | |
{ | |
private IDocumentSession _session; | |
public GetCategoriesQueriesHandler(IDocumentSession session) | |
{ | |
_session = session; | |
} | |
public IEnumerable<Category> Execute(GetCategoriesQuery query) | |
{ | |
return _session.Query<Category>().Where(x => x.SomeProperty == query.SomeSearchValue).OrderBy(x => x.Title); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment