Skip to content

Instantly share code, notes, and snippets.

@kristofclaes
Created February 26, 2013 13:54
Show Gist options
  • Save kristofclaes/5038556 to your computer and use it in GitHub Desktop.
Save kristofclaes/5038556 to your computer and use it in GitHub Desktop.
public interface IQuery<out T>
{
T Execute(IDataContext dataContext);
}
public interface IQueryHandler
{
T Handle<T>(IQuery<T> query);
}
public class QueryHandler : IQueryHandler
{
private readonly IDataContext dataContext;
public QueryHandler(IDataContext dataContext)
{
this.dataContext = dataContext;
}
public T Handle<T>(IQuery<T> query)
{
return query.Execute(dataContext);
}
}
public class CountPeopleQuery : IQuery<int>
{
public int Execute(IDataContext dataContext)
{
return dataContext.People.Count();
}
}
public class TestController : Controller
{
private readonly IQueryHandler queryHandler;
public TestController(IQueryHandler queryHandler)
{
this.queryHandler = queryHandler;
}
public ActionResult Index()
{
ViewBag.PeopleCount = amountOfPeople = queryHandler.Handle(new CountPeopleQuery());
return View();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment