Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mausch committed Aug 12, 2012
1 parent cd6195b commit e7ba055
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions QuartzNetWebConsole/Controllers/SchedulerController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
Expand All @@ -11,12 +13,23 @@ public class SchedulerController : Controller {
private readonly IScheduler scheduler = Setup.Scheduler();
private static readonly MethodInfo[] methods = typeof (IScheduler).GetMethods();

public override void Execute(HttpContextBase context) {
var qs = context.Request.QueryString;
public class MethodParameters {
public readonly MethodInfo method;
public readonly string redirect;
public readonly IEnumerable<object> parameters;

public MethodParameters(MethodInfo method, string redirect, IEnumerable<object> parameters) {
this.method = method;
this.redirect = redirect;
this.parameters = parameters;
}
}

public static MethodParameters GetMethodParameters(NameValueCollection qs) {
var methodName = qs["method"].ToLowerInvariant();
var redirect = qs["next"] ?? "index.ashx";
var parameterNames = qs.AllKeys
.Except(new[] {"method", "next"})
.Except(new[] { "method", "next" })
.Select(a => a.ToLowerInvariant())
.ToArray();
var method = methods
Expand All @@ -28,11 +41,17 @@ public override void Execute(HttpContextBase context) {
var parameters = method.GetParameters()
.Select(p => Convert(qs[p.Name], p.ParameterType))
.ToArray();
method.Invoke(scheduler, parameters);
context.Response.Redirect(redirect);
return new MethodParameters(method, redirect, parameters);
}

public override void Execute(HttpContextBase context) {
var qs = context.Request.QueryString;
var p = GetMethodParameters(qs);
p.method.Invoke(scheduler, p.parameters.ToArray());
context.Response.Redirect(p.redirect);
}

public object Convert(string s, Type t) {
public static object Convert(string s, Type t) {
var converter = TypeDescriptor.GetConverter(t);
if (converter != null && converter.CanConvertFrom(typeof (string)))
return converter.ConvertFromInvariantString(s);
Expand Down

0 comments on commit e7ba055

Please sign in to comment.