<#@ template language="C#" debug="false" hostspecific="true"#> <#@ include file="EF.Utility.CS.ttinclude"#><#@ output extension=".cs"#><# // Copyright (c) Microsoft Corporation. All rights reserved. CodeGenerationTools code = new CodeGenerationTools(this); MetadataTools ef = new MetadataTools(this); MetadataLoader loader = new MetadataLoader(this); CodeRegion region = new CodeRegion(this); string inputFile = @"Model.edmx"; EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string namespaceName = code.VsNamespaceSuggestion(); EntityContainer container = ItemCollection.GetItems().FirstOrDefault(); if (container == null) { return "// No EntityContainer exists in the model, so no code was generated"; } #> //------------------------------------------------------------------------------ // // This code was generated from a template. // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // //------------------------------------------------------------------------------ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Runtime.Serialization; <# if (!String.IsNullOrEmpty(namespaceName)) { #> namespace <#=code.EscapeNamespace(namespaceName)#> { <# PushIndent(CodeRegion.GetIndent(1)); } #> [DataContract(IsReference = true)] <# foreach (EntityType entity in ItemCollection.GetItems().OrderBy(e => e.Name)) { #> [KnownType(typeof(<#=code.Escape(entity)#>))] <# } #> <#=Accessibility.ForType(container)#> class <#=code.Escape(container)#>Iterator : IEnumerable { [DataMember(Name = "Items")] protected List _items; #region Constructors private <#=code.Escape(container)#>Iterator() { } #endregion #region Factory Methods public static <#=code.Escape(container)#>Iterator Create(T entity) { <#=code.Escape(container)#>Iterator iterator = new <#=code.Escape(container)#>Iterator(); iterator.Visit(entity); return iterator; } #endregion #region Execute Methods public void Execute(Action action) { foreach (var item in Items.OfType()) { action(item); } } public static void Execute(<#=code.Escape(container)#>Iterator iterator, Action action) { foreach (var item in iterator.Items.OfType()) { action(item); } } public static void Execute(TEntity entity, Action action) { <#=code.Escape(container)#>Iterator iterator = <#=code.Escape(container)#>Iterator.Create(entity); foreach (var item in iterator.Items.OfType()) { action(item); } } #endregion #region Properties public ReadOnlyCollection Items { get { return WritableItems.AsReadOnly(); } } protected List WritableItems { get { if (_items == null) { _items = new List(); } return _items; } } #endregion #region Visit Method internal void Visit(dynamic entity) { if (entity != null && !WritableItems.Contains(entity)) { WritableItems.Add(entity); <#=code.Escape(container)#>Extensions.Traverse(entity, this); } } #endregion #region IEnumerable Implementation IEnumerator IEnumerable.GetEnumerator() { return Items.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return Items.GetEnumerator(); } #endregion } <#=Accessibility.ForType(container)#> static partial class <#=code.Escape(container)#>Extensions { #region Traverse Methods <# foreach (EntityType entity in ItemCollection.GetItems().OrderBy(e => e.Name)) { #> internal static void Traverse(this <#=code.Escape(entity)#> entity, <#=code.Escape(container)#>Iterator visitor) { <# if (entity.BaseType != null) { #> Traverse((<#=code.Escape(entity.BaseType)#>)entity, visitor); <# } #> <# foreach (var navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity)) { if(navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many) { #> if (entity.<#=code.Escape(navProperty)#> != null) { foreach (var value in entity.<#=code.Escape(navProperty)#>) { visitor.Visit(value); } } <# } else { #> if (entity.<#=code.Escape(navProperty)#> != null) { visitor.Visit(entity.<#=code.Escape(navProperty)#>); } <# } } #> } <# } #> #endregion } <# if (!String.IsNullOrEmpty(namespaceName)) { PopIndent(); #> } <# } #>