Created
February 26, 2013 13:54
-
-
Save kristofclaes/5038556 to your computer and use it in GitHub Desktop.
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
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