Created
March 13, 2014 15:12
-
-
Save CallumVass/9530312 to your computer and use it in GitHub Desktop.
Error: No registration for type IQueryHandler<EnrollmentDateQuery, List<EnrollmentDateGroup>> could be found.
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
container.RegisterSingle<IQueryProcessor, DynamicQueryProcessor>(); | |
container.RegisterManyForOpenGeneric(typeof(IQueryHandler<,>), Assembly.GetExecutingAssembly()); |
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
public class HomeController : Controller | |
{ | |
private readonly IQueryProcessor _queryProcessor; | |
public HomeController(IQueryProcessor queryProcessor) | |
{ | |
_queryProcessor = queryProcessor; | |
} | |
public ActionResult Index() | |
{ | |
return View(); | |
} | |
public ActionResult About() | |
{ | |
var result = _queryProcessor.Process(new EnrollmentDateQuery()); | |
return View(result); | |
} | |
} | |
public class EnrollmentDateQuery : IQuery<List<EnrollmentDateGroup>> | |
{ | |
} | |
public class EnrollmentDateGroup | |
{ | |
public DateTime EnrollmentDate { get; set; } | |
public int Students { get; set; } | |
} | |
public class EnrollmentDateQueryHandler : IQueryHandler<EnrollmentDateQuery, List<EnrollmentDateGroup>> | |
{ | |
private readonly SchoolContext _schoolContext; | |
public EnrollmentDateQueryHandler(SchoolContext schoolContext) | |
{ | |
_schoolContext = schoolContext; | |
} | |
public List<EnrollmentDateGroup> Handle(EnrollmentDateQuery query) | |
{ | |
return _schoolContext.Students.GroupBy(x => x.EnrollmentDate).Select(x => new EnrollmentDateGroup | |
{ | |
EnrollmentDate = x.Key, | |
Students = x.Count() | |
}).ToList(); | |
} | |
} |
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
public class DynamicQueryProcessor : IQueryProcessor | |
{ | |
private readonly Container _container; | |
public DynamicQueryProcessor(Container container) | |
{ | |
_container = container; | |
} | |
[DebuggerStepThrough] | |
public TResult Process<TResult>(IQuery<TResult> query) | |
{ | |
var handlerType = typeof(IQueryHandler<,>) | |
.MakeGenericType(query.GetType(), typeof(TResult)); | |
dynamic handler = _container.GetInstance(handlerType); | |
return handler.Handle((dynamic)query); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment