Created
April 10, 2011 14:35
-
-
Save geoffreysmith/912387 to your computer and use it in GitHub Desktop.
Sharpified controller
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
<#@ template language="C#" HostSpecific="True" inherits="DynamicTransform" #> | |
<#@ Output Extension="cs" #> | |
<#@ import namespace="System.Collections" #> | |
<#@ import namespace="System.Collections.Generic" #> | |
<#@ import namespace="System" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ import namespace="System.Text.RegularExpressions" #> | |
<#@ import namespace="EnvDTE" #> | |
namespace <#= Model.ControllerNamespace #> | |
{ | |
using System.Linq; | |
using System.Web.Mvc; | |
<# if(!string.IsNullOrEmpty(Model.ModelTypeNamespace)) { #> | |
using <#= Model.ModelTypeNamespace #>; | |
<# } #> | |
using MvcContrib; | |
using SharpArch.Core.PersistenceSupport; | |
using SharpArch.Web.NHibernate; | |
<# | |
var modelType = (CodeType)Model.ModelType; | |
var modelName = modelType.Name; | |
var modelNamePlural = Model.ModelTypePluralized; | |
var modelVariable = modelName.ToLower(); | |
var relatedEntities = ((IEnumerable)Model.RelatedEntities).OfType<RelatedEntityInfo>(); | |
var primaryKeyProperty = modelType.VisibleMembers().OfType<CodeProperty>().Single(x => x.Name == Model.PrimaryKey); | |
var routingName = Regex.Replace(Model.ControllerName, "Controller$", "", RegexOptions.IgnoreCase); | |
var repository = new String(modelName.Select((ch, index) => (index == 0) ? Char.ToLower(ch) : ch).ToArray()) + "Repository"; | |
#> | |
[HandleError] | |
public class <#= modelType.Name #>Controller : Controller | |
{ | |
private readonly IRepository<<#= modelType.Name #>> <#= repository #>; | |
public <#= modelType.Name #>Controller(IRepository<<#= modelType.Name #>> <#= repository #>) | |
{ | |
this.<#= repository #> = <#= repository #>; | |
} | |
public ViewResult Index() | |
{ | |
<# | |
var propertiesToInclude = relatedEntities.Select(relation => relation.LazyLoadingProperty).Where(x => x != null); | |
var includeExpressions = String.Join("", propertiesToInclude.Select(x => String.Format(".Include({0} => {0}.{1})", modelVariable, x.Name))); | |
#> | |
return View(this.<#= repository #>.GetAll()<#= includeExpressions #>.ToList()); | |
} | |
public ViewResult Details(<#= primaryKeyProperty.Type.AsString #> id) | |
{ | |
var <#= modelVariable #> = this.<#= repository #>.Get(id); | |
return View(<#= modelVariable #>); | |
} | |
public ActionResult Create() | |
{ | |
<# foreach(var relatedEntity in relatedEntities.Where(x => x.RelationType == RelationType.Parent)) { #> | |
ViewBag.Possible<#= relatedEntity.RelationNamePlural #> = context.<#= relatedEntity.RelatedEntityTypeNamePlural #>; | |
<# } #> | |
return View(); | |
} | |
[HttpPost] | |
[Transaction] | |
public ActionResult Create(<#= modelName #> <#= modelVariable #>) | |
{ | |
if (ModelState.IsValid) | |
{ | |
this.<#= repository #>.SaveOrUpdate(<#= modelVariable #>); | |
return this.RedirectToAction(c => c.Index()); | |
} | |
<# foreach(var relatedEntity in relatedEntities.Where(x => x.RelationType == RelationType.Parent)) { #> | |
ViewBag.Possible<#= relatedEntity.RelationNamePlural #> = context.<#= relatedEntity.RelatedEntityTypeNamePlural #>; | |
<# } #> | |
return View(<#= modelVariable #>); | |
} | |
public ActionResult Edit(<#= primaryKeyProperty.Type.AsString #> id) | |
{ | |
var <#= modelVariable #> = this.<#= repository #>.Get(id); | |
<# foreach(var relatedEntity in relatedEntities.Where(x => x.RelationType == RelationType.Parent)) { #> | |
ViewBag.Possible<#= relatedEntity.RelationNamePlural #> = context.<#= relatedEntity.RelatedEntityTypeNamePlural #>; | |
<# } #> | |
return View(<#= modelVariable #>); | |
} | |
[HttpPost] | |
public ActionResult Edit(<#= modelName #> <#= modelVariable #>) | |
{ | |
if (ModelState.IsValid) | |
{ | |
this.<#= repository #>.SaveOrUpdate(<#= modelVariable #>); | |
return this.RedirectToAction(c => c.Index()); | |
} | |
<# foreach(var relatedEntity in relatedEntities.Where(x => x.RelationType == RelationType.Parent)) { #> | |
ViewBag.Possible<#= relatedEntity.RelationNamePlural #> = context.<#= relatedEntity.RelatedEntityTypeNamePlural #>;<# } #> | |
return View(<#= modelVariable #>); | |
} | |
public ActionResult Delete(<#= primaryKeyProperty.Type.AsString #> id) | |
{ | |
var <#= modelVariable #> = this.<#= repository #>.Get(id); | |
return View(<#= modelVariable #>); | |
} | |
[HttpPost, ActionName("Delete")] | |
[Transaction] | |
public ActionResult DeleteConfirmed(<#= primaryKeyProperty.Type.AsString #> id) | |
{ | |
var <#= modelVariable #> = this.<#= repository #>.Get(id); | |
this.<#= repository #>.Delete(<#= modelVariable #>); | |
return this.RedirectToAction(c => c.Index()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment