Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Another solution is to create an <code>ActionFilter</code> to decorate the actions in the controllers like this:</p> <pre><code> [GenericActionFilter(ModelType=typeof(ShowModel))] public ActionResult Edit(ShowQuery query) { return View(); } </code></pre> <p>and this is the <code>ActionFilter</code></p> <pre><code>public class GenericActionFilter : ActionFilterAttribute { public Type ModelType { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); IMediator mediator = null; if(filterContext.Controller is BaseController) { mediator = ((BaseController)filterContext.Controller).GetMediator(); object paramValue = filterContext.ActionParameters["query"]; var method = mediator.GetType().GetMethod("Request").MakeGenericMethod(new Type[] { ModelType }); var model = method.Invoke(mediator, new object[] { paramValue }); filterContext.Controller.ViewData.Model = model; } } } </code></pre> <p>and the <code>BaseController</code></p> <pre><code>public class BaseController : Controller { private readonly IMediator mediator; public BaseController():this(new Mediator()) { } public BaseController(IMediator mediator) { this.mediator = mediator; } public IMediator GetMediator() { return mediator; } } </code></pre> <p>this is based on an assumption which the <code>Request</code> method of the <code>Mediator</code> is a generic method like this:</p> <pre><code>public interface IMediator { TResponse Request&lt;TResponse&gt;(IQuery&lt;TResponse&gt; query); } </code></pre> <p>and </p> <pre><code>public class ShowQuery : IQuery&lt;ShowModel&gt; { public string EventName { get; set; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload