Created
November 11, 2010 21:11
-
-
Save troygoode/673191 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IndividualController : BaseController{ | |
[HttpPost] | |
public ActionResult UpdateProfile(UpdateProfileInputModel inputModel) | |
{ | |
return Form(inputModel, View("UpdateSucceeded"), View("UpdateFailed")); | |
} | |
} | |
public class UpdateProfileInputModelHandler : IFormHandler<UpdateProfileInputModel> | |
{ | |
private IRepository<Individual> _repo; | |
public UpdateProfileInputModelHandler(IRepository<Individual> repo) | |
{ | |
_repo = repo; | |
} | |
public void Handle(UpdateProfileInputModel inputModel) | |
{ | |
var individual = _repo.Get(inputModel.Id); | |
Mapper.Map<ProfileInputModel, Individual>(input, individual); | |
foreach(var email in input.Emails) | |
individual.UpdateEmailAddress(email.Key, email.Value); | |
} | |
} | |
// ----------------------------------------------------------------------- | |
public abstract class BaseController : Controller | |
{ | |
protected FormActionResult<T> Form<T>(T form, ActionResult success) | |
{ | |
return Form(form, success, View(form)); | |
} | |
protected FormActionResult<T> Form<T>(T form, ActionResult success, ViewResult failure) | |
{ | |
return new FormActionResult<T>(form, () => success, ex => | |
{ | |
foreach (var error in ex.Errors) | |
ModelState.AddModelError(error.Key, error.Value); | |
return failure; | |
}); | |
} | |
} | |
public interface IFormHandler<in T> | |
{ | |
void Handle(T form); | |
} | |
public class FormActionResult<T> : ActionResult | |
{ | |
public FormActionResult(T form, Func<ActionResult> success, Func<FormErrorException, ActionResult> failure) | |
{ | |
Form = form; | |
OnSuccess = success; | |
OnFailure = failure; | |
} | |
public Func<FormErrorException, ActionResult> OnFailure { get; private set; } | |
public Func<ActionResult> OnSuccess { get; private set; } | |
public T Form { get; private set; } | |
public override void ExecuteResult(ControllerContext context) | |
{ | |
if (!context.Controller.ViewData.ModelState.IsValid) | |
{ | |
OnFailure(null).ExecuteResult(context); | |
return; | |
} | |
var handler = ServiceLocator.Locate<IFormHandler<T>>(); | |
try | |
{ | |
handler.Handle(Form); | |
} | |
catch (FormErrorException exception) | |
{ | |
OnFailure(exception).ExecuteResult(context); | |
return; | |
} | |
OnSuccess().ExecuteResult(context); | |
} | |
} | |
public class FormErrorException : Exception | |
{ | |
public FormErrorException(string errorMessage) | |
: this(string.Empty, errorMessage) | |
{ | |
} | |
public FormErrorException(string name, string errorMessage) | |
: this(new Dictionary<string, string>(1) | |
{ | |
{name, errorMessage} | |
}) | |
{ | |
} | |
public FormErrorException(IDictionary<string, string> errors) | |
{ | |
Errors = errors; | |
} | |
public IDictionary<string, string> Errors { get; private set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment