Adventures in .NET (and other M$ stuff)

May 2

CruddySmartDispatcherController

I realized most controllers (I tend to stick with a RESTful design, when possible) basically have the same actions and I wondered if a generic controller couldn’t fit the bill… so I came up with this:

[DefaultAction(“index”), Layout(“default”)]
  public class CruddySmartDispatcherController : SmartDispatcherController
    where AR : ActiveRecordBase, IDomainObject
  {
    public delegate void RunInAction();

    public delegate void RunAfterSingleAction(AR o);

    public delegate void RunAfterMultipleAction(IEnumerable objects);

    private readonly IDictionary> _beforeActions;
    private readonly IDictionary> _afterActions;
    private readonly IDictionary> _afterSingleActions;
    private readonly IDictionary> _afterMultipleActions;

    public CruddySmartDispatcherController()
    {
      _beforeActions = new Dictionary>()
                         {
                           {“index”, new List()},
                           {“show”, new List()},
                           {“new”, new List()},
                           {“create”, new List()},
                           {“edit”, new List()},
                           {“update”, new List()},
                           {“delete”, new List()}
                         };
      _afterActions = new Dictionary>()
                        {
                          {“index”, new List()},
                          {“show”, new List()},
                          {“new”, new List()},
                          {“create”, new List()},
                          {“edit”, new List()},
                          {“update”, new List()},
                          {“delete”, new List()}
                        };
      _afterSingleActions = new Dictionary>()
                        {
                          {“index”, new List()},
                          {“show”, new List()},
                          {“new”, new List()},
                          {“create”, new List()},
                          {“edit”, new List()},
                          {“update”, new List()},
                          {“delete”, new List()}
                        };
      _afterMultipleActions = new Dictionary>()
                        {
                          {“index”, new List()},
                          {“show”, new List()},
                          {“new”, new List()},
                          {“create”, new List()},
                          {“edit”, new List()},
                          {“update”, new List()},
                          {“delete”, new List()}
                        };
    }

    public virtual void Index()
    {
      RunBefores(“index”);
      IEnumerable objects = ActiveRecordBase.FindAll();
      PropertyBag.Add(“objects”, objects);
      RunAfters(“index”, objects);
    }

    public virtual void Show(int id)
    {
      RunBefores(“show”);
      var o = ActiveRecordBase.FindOne(Expression.Eq(“Id”, id));
      PropertyBag.Add(“object”, o);
      RunAfters(“show”, o);
    }

    public virtual void New()
    {
      RunBefores(“new”);
      RunAfters(“new”);
    }

    public virtual void Create([DataBind(“object”, Exclude = “id”)] AR o)
    {
      try
      {
        RunBefores(“create”);
        o.Save();
        PropertyBag.Add(“object”, o);
        this.AddInfo(o.ToString() + ” created.”);
        RunAfters(“create”, o);
      }
      catch (Exception ex)
      {
        this.AddError(ex.Message);
        Flash.Add(“object”, o);
        RedirectToAction(“new”);
      }
    }

    public virtual void Edit(int id)
    {
      RunBefores(“edit”);
      var o = ActiveRecordBase.FindOne(Expression.Eq(“Id”, id));
      PropertyBag.Add(“object”, o);
      RunAfters(“edit”, o);
    }

    public virtual void Update([DataBind(“object”)] AR o)
    {
      try
      {
        RunBefores(“update”);
        o.Update();
        PropertyBag.Add(“object”, o);
        this.AddInfo(o.ToString() + ” updated.”);
        RunAfters(“update”, o);
      }
      catch (Exception ex)
      {
        this.AddError(ex.Message);
        Flash.Add(“object”, o);
        RedirectToAction(“edit”, “id=” + o.Id.ToString());
      }
    }

    public virtual void Delete(int id)
    {
      try
      {
        RunBefores(“delete”);
        var o = ActiveRecordBase.FindOne(Expression.Eq(“Id”, id));
        o.Delete();
        this.AddInfo(o.ToString() + ” deleted.”);
        RunAfters(“delete”, o);
      }
      catch (Exception ex)
      {
        this.AddError(ex.Message);
        RedirectToAction(“index”);
      }
    }

    protected void Before(string action, RunInAction ria)
    {
      Before(new string[] { action }, ria);
    }

    protected void After(string action, RunInAction ria)
    {
      After(new string[] { action }, ria);
    }

    protected void After(string action, RunAfterSingleAction rasa)
    {
      After(new string[] { action }, rasa);
    }

    protected void After(string[] actions, RunAfterSingleAction rasa)
    {
      foreach (var a in actions)
        _afterSingleActions[a].Add(rasa);
    }

    protected void After(string action, RunAfterMultipleAction rasa)
    {
      After(new string[] { action }, rasa);
    }

    protected void After(string[] actions, RunAfterMultipleAction rasa)
    {
      foreach (var a in actions)
        _afterMultipleActions[a].Add(rasa);
    }

    protected void Before(string[] actions, RunInAction ria)
    {
      foreach (var a in actions)
        _beforeActions[a].Add(ria);
    }

    protected void After(string[] actions, RunInAction ria)
    {
      foreach (var a in actions)
        _afterActions[a].Add(ria);
    }

    protected virtual void RunBefores(string action)
    {
      foreach (var ria in _beforeActions[action])
        ria();
    }

    protected virtual void RunAfters(string action)
    {
      foreach (var ria in _afterActions[action])
        ria();
    }

    protected virtual void RunAfters(string action, AR o)
    {
      RunAfters(action);
      foreach (var rasa in _afterSingleActions[action])
        rasa(o);
    }

    protected virtual void RunAfters(string action, IEnumerable objects)
    {
      RunAfters(action);
      foreach (var rama in _afterMultipleActions[action])
        rama(objects);
    }
  }

This could prob be better, but it is a start. Here is a use example:

 public class ItemController : CruddySmartDispatcherController
  {
    public ItemController()
    {
            Before(new string[] {“new”,”edit”},PopulateDropDowns);

            After(
                new string[]{
                    ”create”,
                    ”delete”
                }, delegate()
                          {
                                    RedirectToAction(“index”);           
                                    CancelView();
                          });
            After(“update”, item => RedirectToAction(“show”, “id=” + item.Id.ToString()));
    }

      private void PopulateDropDowns()
      {
          PropertyBag.Add(“departments”,ActiveRecordBase.FindAll());
      }
  }


Mar 6

eval(jsonResponse): invalid label

eval(“(” + jsonResponse + “)”) fixed it for me.

Feb 11

Focus TextBox onfocus

onfocus=”this.select()”

Feb 5

Mock<IPerson>

A person who implements an interface without anything under the surface to really backup the methods they expose.

Jan 18

Change SQL Server Port

SQL Server Config Manager

Network Config -> Protocols -> Enable TCP/IP

    -> IP Addresses

        Change each TCP Port to WHATEVER

Services -> Restart SQL Server


Page 1 of 1