Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#@ 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 = @"Entities.edmx";
- EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
- string namespaceName = code.VsNamespaceSuggestion();
- EntityContainer container = ItemCollection.GetItems<EntityContainer>().FirstOrDefault();
- if (container == null)
- {
- return "// No EntityContainer exists in the model, so no code was generated";
- }
- #>
- //------------------------------------------------------------------------------
- // <auto-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.
- // </auto-generated>
- //------------------------------------------------------------------------------
- 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<EntityType>().OrderBy(e => e.Name))
- {
- #>
- [KnownType(typeof(<#=code.Escape(entity)#>))]
- <#
- }
- #>
- <#=Accessibility.ForType(container)#> class <#=code.Escape(container)#>Iterator : IEnumerable<object>
- {
- [DataMember(Name = "Items")]
- private List<object> _items;
- private List<object> _leafsToExploreFurther = new List<object>();
- #region Constructors
- private <#=code.Escape(container)#>Iterator()
- {
- }
- #endregion
- #region Factory Methods
- public static <#=code.Escape(container)#>Iterator Create<T>(T entity)
- {
- <#=code.Escape(container)#>Iterator iterator = new <#=code.Escape(container)#>Iterator();
- iterator.Visit(entity, 0);
- while (iterator._leafsToExploreFurther.Count > 0)
- {
- var leaf = iterator._leafsToExploreFurther[0];
- iterator._leafsToExploreFurther.Remove(leaf);
- iterator.Visit(leaf, 0);
- }
- return iterator;
- }
- #endregion
- #region Execute Methods
- public void Execute<TFilter>(Action<TFilter> action)
- {
- foreach (var item in Items.OfType<TFilter>())
- {
- action(item);
- }
- }
- public static void Execute<TFilter>(<#=code.Escape(container)#>Iterator iterator, Action<TFilter> action)
- {
- foreach (var item in iterator.Items.OfType<TFilter>())
- {
- action(item);
- }
- }
- public static void Execute<TEntity, TFilter>(TEntity entity, Action<TFilter> action)
- {
- <#=code.Escape(container)#>Iterator iterator = <#=code.Escape(container)#>Iterator.Create(entity);
- foreach (var item in iterator.Items.OfType<TFilter>())
- {
- action(item);
- }
- }
- #endregion
- #region Properties
- public ReadOnlyCollection<object> Items
- {
- get
- {
- return WritableItems.AsReadOnly();
- }
- }
- protected List<object> WritableItems
- {
- get
- {
- if (_items == null)
- {
- _items = new List<object>();
- }
- return _items;
- }
- }
- #endregion
- #region Visit Method
- internal void Visit(dynamic entity, int depth)
- {
- // Prevent stack overflows by tracking the call depth.
- // If over a certain value, then store that leaf node for further exploration in a later loop.
- if (depth >= 20)
- {
- _leafsToExploreFurther.Add(entity);
- return;
- }
- if (entity != null && !WritableItems.Contains(entity))
- {
- WritableItems.Add(entity);
- <#=code.Escape(container)#>Extensions.Traverse(entity, this, depth + 1);
- }
- }
- #endregion
- #region IEnumerable Implementation
- IEnumerator<object> IEnumerable<object>.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<EntityType>().OrderBy(e => e.Name))
- {
- #>
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "entity"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "visitor")]
- internal static void Traverse(this <#=code.Escape(entity)#> entity, <#=code.Escape(container)#>Iterator visitor, int depth)
- {
- <#
- if (entity.BaseType != null)
- {
- #>
- Traverse((<#=code.Escape(entity.BaseType)#>)entity, visitor, depth + 1);
- <#
- }
- #>
- <#
- 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, depth + 1);
- }
- }
- <#
- }
- else
- {
- #>
- if (entity.<#=code.Escape(navProperty)#> != null)
- {
- visitor.Visit(entity.<#=code.Escape(navProperty)#>, depth + 1);
- }
- <#
- }
- }
- #>
- }
- <#
- }
- #>
- #endregion
- }
- <#
- if (!String.IsNullOrEmpty(namespaceName))
- {
- PopIndent();
- #>
- }
- <#
- }
- #>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement