Skip to content

Instantly share code, notes, and snippets.

@benfoster
Created July 10, 2012 13:28
Show Gist options
  • Save benfoster/3083223 to your computer and use it in GitHub Desktop.
Save benfoster/3083223 to your computer and use it in GitHub Desktop.
ViewBuilder/ViewFactory
namespace Fabrik.Common.Web
{
public interface IViewBuilder<TView>
{
TView Build();
}
public interface IViewBuilder<TInput, TView>
{
TView Build(TInput input);
}
}
namespace Fabrik.Common.Web
{
public interface IViewFactory
{
TView CreateView<TView>();
TView CreateView<TInput, TView>(TInput input);
}
}
namespace Fabrik.Common.Web.Example.Controllers
{
public class HomeController : Controller
{
private readonly IViewFactory viewFactory;
public HomeController(IViewFactory viewFactory)
{
this.viewFactory = viewFactory;
}
[ImportModelStateFromTempData]
public ActionResult Index()
{
return View(viewFactory.CreateView<HomeView>());
}
[HttpPost]
[ValidateModelState]
public ActionResult Index(HomeCommand command)
{
// if we get here, ModelState is valid
// save to db etc.
return RedirectToAction("index")
.AndAlert(AlertType.Success, "Subscription Received.", "Thank you for subscribing, we now have your most personal details. Mwah ha ha ha haaa!");
}
[AutoFormatResult]
public ActionResult About()
{
return View(viewFactory.CreateView<AboutView>());
}
public ActionResult List(int page)
{
return View(viewFactory.CreateView<int, ListView>(page));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment