Skip to content

Instantly share code, notes, and snippets.

@CallumVass
Created March 13, 2014 15:12
Show Gist options
  • Save CallumVass/9530312 to your computer and use it in GitHub Desktop.
Save CallumVass/9530312 to your computer and use it in GitHub Desktop.
Error: No registration for type IQueryHandler<EnrollmentDateQuery, List<EnrollmentDateGroup>> could be found.
container.RegisterSingle<IQueryProcessor, DynamicQueryProcessor>();
container.RegisterManyForOpenGeneric(typeof(IQueryHandler<,>), Assembly.GetExecutingAssembly());
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();
}
}
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