Skip to content

Instantly share code, notes, and snippets.

@jmarnold
Created February 3, 2011 02:56
Show Gist options
  • Save jmarnold/808960 to your computer and use it in GitHub Desktop.
Save jmarnold/808960 to your computer and use it in GitHub Desktop.
public void Invoke()
{
if (performInvoke() == DoNext.Continue && InsideBehavior != null)
{
InsideBehavior.Invoke();
}
afterInsideBehavior();
}
protected virtual DoNext performInvoke()
{
return DoNext.Continue;
}
protected virtual void afterInsideBehavior()
{
}
public class CreateEntityAction<TEntity, TInput>
{
private readonly IUnitOfWork _unitOfWork;
private readonly IMappingRegistry _mappingRegistry;
public CreateEntityAction(IUnitOfWork unitOfWork, IMappingRegistry mappingRegistry)
{
_unitOfWork = unitOfWork;
_mappingRegistry = mappingRegistry;
}
public void Execute(TInput input)
{
var entity = _mappingRegistry.Map<TInput, TEntity>(input);
_unitOfWork.Initialize();
_unitOfWork.Insert(entity);
_unitOfWork.Commit();
}
}
public interface IActionBehavior
{
void Invoke();
void InvokePartial();
}
public class ProjectsController
{
// dependencies...
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(CreateProjectInput inputModel)
{
var result = _createEntityService.Create<Project>(inputModel);
if(!result.Success)
{
result.Errors.Each(_errorService.Register);
return Create();
}
return View("Details", new { ProjectId = result.Project.ProjectId });
}
}
public void Invoke()
{
try
{
InsideBehavior.Invoke();
}
catch(Exception exc)
{
// we'd probably want better error reporting...
_errorService.RegisterError(exc);
// redirect
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment