Created
July 10, 2012 13:28
-
-
Save benfoster/3083223 to your computer and use it in GitHub Desktop.
ViewBuilder/ViewFactory
This file contains hidden or 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
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