<# /* * Sam Meacham (www.samscode.com) * Template to auto generate ToDto() and ToJson() methods on each of the entity classes in your entity data model (edmx). * The ToJson() method makes the assumption that there's a class named JsonHelper in the same namespace as your entities. * This JsonHelper class is availble here: http://samscode.com/index.php/2010/06/entity-framework-v4-end-to-end-application-strategy-part-2-data-layer/ * * This template also sets up some extension methods, to go to/from an IEnumerable of entities or dtos. * As in, MyDtos[] dtos = myEntities.ToDtos(); */ // // CONFIG SECTION. Change these values to be specific to your project. // // This should be the relative path from this template to your .edmx file string inputFile = "../PersonEntities.edmx"; // The namespace where your DTO classes are contained string dtoClassNamespace = "Test.DataAccess.DTOs"; #> <#@ template language="C#" debug="false" hostspecific="true"#> <#@ include file="EF.Utility.CS.ttinclude"#> <#@ output extension=".cs"#> <# CodeGenerationTools code = new CodeGenerationTools(this); MetadataLoader loader = new MetadataLoader(this); EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string namespaceName = code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this); #> using System.Collections.Generic; using <#= dtoClassNamespace #>; namespace <#= code.EscapeNamespace(namespaceName) #> { public static class EntityExtensionMethods { <# foreach (EntityType entity in ItemCollection.GetItems().OrderBy(e => e.Name)) { #> /// /// Easily create DTO objects from actual domain entity objects /// public static <#=code.Escape(entity)#>Dto[] ToDtos(this IEnumerable<<#=code.Escape(entity)#>> entities) { return AutoMapper.Mapper.Map>, <#=code.Escape(entity)#>Dto[]>(entities); } public static string ToJson(this IEnumerable<<#=code.Escape(entity)#>> entities, bool format = false) { return JsonHelper.Serialize(entities.ToDtos(), format); } <# } #> } } <# // ******************************************************************************** // *** Emit Entity Types ********************************************************** // ******************************************************************************** foreach (EntityType entity in ItemCollection.GetItems().OrderBy(e => e.Name)) { fileManager.StartNewFile(entity.Name + ".AutoGen.cs"); #> using <#= dtoClassNamespace #>; namespace <#= code.EscapeNamespace(namespaceName) #> { <#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#> <#=code.StringBefore(" : ", code.Escape(entity.BaseType))#> { public string ToJson(bool format = false) { return ToDto().ToJson(format); } public <#=code.Escape(entity)#>Dto ToDto() { return AutoMapper.Mapper.Map<<#=code.Escape(entity)#>, <#=code.Escape(entity)#>Dto>(this); } } } <# } // end foreach entity fileManager.Process(); #>