Skip to content

Instantly share code, notes, and snippets.

@schotime
Created April 16, 2012 01:19

Revisions

  1. schotime created this gist Apr 16, 2012.
    58 changes: 58 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@

    public class HomeController : DelegatingControllers.DelegatingController
    {
    public ActionResult Edit(UserEditQueryModel model)
    {
    var vm = Invoker.Execute<UserEditQueryModel, UserEditViewModel>(model);
    return View(vm);
    }

    public ActionResult Edit(UserEditInputModel model)
    {
    if (!ModelState.IsValid)
    {
    return Edit(new UserEditQueryModel());
    }

    Invoker.Execute(model);
    //There is an overload which allows data to be returned if you have to conditionally redirect somewhere else.

    return RedirectToAction("Edit");
    }
    }

    public class UserEditViewModel
    {
    public string Id { get; set; }
    public string Data { get; set; }
    public IList<string> RefData { get; set; }
    }

    public class UserEditInputModel
    {
    public string Id { get; set; }
    public string Data { get; set; }
    }

    public class UserEditQueryModel
    {
    public string Id { get; set; }
    }

    public class UserEditQueryHandler : IHandler<UserEditQueryModel, UserEditViewModel>
    {
    public UserEditViewModel Handle(UserEditQueryModel inputModel)
    {
    return new UserEditViewModel();
    }
    }

    public class UserEditSaveHandler : IHandler<UserEditInputModel>
    {
    public void Handle(UserEditInputModel command)
    {
    var user = Session.Find<User>(command.Id) ?? new User();
    user.Data = command.Data;
    Session.Save(user);
    }
    }