While working on a relatively large WPF 4.0 project, I ran into a relatively common problem. The Entity-Framework doesn’t expose the “Property-Changed” event in all of the right places. Additionally, when I needed an “ObservableCollection” for updating a data-grid or list control of some type… I wasn’t provided with one out-of-the-box.
So enter T4-templates. What I did was definitely NOT a complete POCO class or “Repository pattern”. (Which you can learn more about here… “Using Repository Pattern with Entity Framework“) Instead, I simply wanted to expose the exact entities that EF 4.0 generates out-of-the-box… with some encapsulation classes that added “PropertyChanged” notifications and other useful things for my WPF project. This is REALLY useful because I can then make programmatic updates and see them reflected in the UI… regardless of whether I’m data-binding to a reference-type property of the entity-framework, or a scalar value-type property. Additionally, when I have an entity that contains a collection of sub-entities, I can now leverage the “ObservableCollection” concept to get the UI to automatically update if an item is programmatically added to the collection.
I guess the point here is to provide a stupid-simple way of turning any entity-framework EDMX file into a PropertyChanged/ObservableCollection aware series of Plain-Old-CLR-Object (POCO) entities whereby changes will still be able to be persisted back to the database via the “SaveChanges()” method and the WPF UI will update according to programmatic changes to the data.
So the T4-template is show below… and is also included as a link on this page. I know it’s only a fit for certain scenarios… but I tend to think it works well for simple scenarios. Simply change the file-name within the T4-template to point to your project’s EDMX file… and it will generate a series of “POCO” classes that encapsulate the functionality of the actual entity-classes for you.
Cool. Here’s the link…
[Note*** This T4 template distinguishes between some relationships in the EF model and others. It's selectively creating POCO classes and POCO class references depending on the multiplicity of the relationship between the entities. In other words... simple lookup-table relationships will not result in a POCO-class property data-type.]
Here’s the file contents :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 | <#
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the Microsoft Public License.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#>
<#@ 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);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"../YourEFModelHere.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
// Write out support code to primary template output file
WriteHeader(fileManager);
BeginNamespace(namespaceName, code);
WriteCustomObservableCollection();
EndNamespace(namespaceName);
// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
fileManager.StartNewFile(entity.Name + "Poco.cs");
BeginNamespace(namespaceName, code);
bool entityHasNullableFKs = entity.NavigationProperties.Any(np => np.GetDependentProperties().Any(p=>ef.IsNullable(p)));
#>
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#>Poco : INotifyPropertyChanged
{
<#region.Begin("Poco Initialization");#>
private <#=code.Escape(entity)#> _context;
private CollisionTransEntities _dbEntities;
public <#=code.Escape(entity)#> Context{ get { return _context; } internal set { _context = value; } }
public event PropertyChangedEventHandler PropertyChanged;
public <#=code.Escape(entity)#>Poco()
{
_context = new <#=code.Escape(entity)#>();
_context.PropertyChanged += context_PropertyChanged;
Initialize(true);
}
public <#=code.Escape(entity)#>Poco(<#=code.Escape(entity)#> context, CollisionTransEntities dbEntities)
{
_context = context;
DbEntities = dbEntities;
_context.PropertyChanged += context_PropertyChanged;
Initialize(false);
}
private void Initialize(bool skipSubCollectionInitialization)
{
<#
foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
{
NavigationProperty inverse = ef.Inverse(navProperty);
if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
{
WriteLine(" if (!skipSubCollectionInitialization)");
WriteLine(" CreateNew" + navProperty.Name + "Collection();");
}
if ((inverse != null || ((AssociationType)navProperty.RelationshipType).IsForeignKey) &&
navProperty.ToEndMember.RelationshipMultiplicity != RelationshipMultiplicity.Many)
{
WriteLine(" _context." + navProperty.Name + "Reference.AssociationChanged += " + navProperty.Name + "_AssociationChanged;");
}
}
#>
}
private void InitializeSubCollections()
{
<#
foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
{
NavigationProperty inverse = ef.Inverse(navProperty);
if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
{
WriteLine(" CreateNew" + navProperty.Name + "Collection();");
}
}
#>
}
public CollisionTransEntities DbEntities
{
get
{
return _dbEntities;
}
set
{
if(_dbEntities == null)
{
_dbEntities = value;
//Now also "Initialize" the sub-collections...
InitializeSubCollections();
}
}
}
void context_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(e.PropertyName));
}
<#region.End();#>
<#
region.Begin("Primitive Properties");
foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity))
{
bool isForeignKey = entity.NavigationProperties.Any(np=>np.GetDependentProperties().Contains(edmProperty));
bool isDefaultValueDefinedInModel = (edmProperty.DefaultValue != null);
bool generateAutomaticProperty = false;
#>
<#=PropertyVirtualModifier(Accessibility.ForProperty(edmProperty))#> <#=code.Escape(edmProperty.TypeUsage)#> <#=code.Escape(edmProperty)#>
{
<#
if (isForeignKey)
{
#>
<#=code.SpaceAfter(Accessibility.ForGetter(edmProperty))#>get { return _context.<#=code.Escape(edmProperty)#>; } //qwer1 Type:<#=code.Escape(edmProperty.TypeUsage)#>
<#=code.SpaceAfter(Accessibility.ForSetter(edmProperty))#>set
{
if(_context.<#=code.Escape(edmProperty)#> != value)
{
_context.PropertyChanged -= context_PropertyChanged;
_context.<#=code.Escape(edmProperty)#> = value;
_context.PropertyChanged += context_PropertyChanged;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("<#=code.Escape(edmProperty)#>"));
<#=entityHasNullableFKs?"//Entity property has nullable-FKs":""#>
}
}
<#
}
else if (isDefaultValueDefinedInModel)
{
#>
<#=code.SpaceAfter(Accessibility.ForGetter(edmProperty))#>get qwer2{ return <#=code.FieldName(edmProperty)#>; }
<#=code.SpaceAfter(Accessibility.ForSetter(edmProperty))#>set { <#=code.FieldName(edmProperty)#> = value; }
<#
}
else
{
generateAutomaticProperty = true;
#>
<#=code.SpaceAfter(Accessibility.ForGetter(edmProperty))#>get { return _context.<#=code.Escape(edmProperty)#>; } //qwer3 Type:<#=code.Escape(edmProperty.TypeUsage)#>
<#=code.SpaceAfter(Accessibility.ForSetter(edmProperty))#>set
{
if(_context.<#=code.Escape(edmProperty)#> != value)
{
_context.PropertyChanged -= context_PropertyChanged;
_context.<#=code.Escape(edmProperty)#> = value;
_context.PropertyChanged += context_PropertyChanged;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("<#=code.Escape(edmProperty)#>"));
}
}
<#
}
#>
}
<#
if (!generateAutomaticProperty)
{
#>
<#
}
}
region.End();
region.Begin("Complex Properties");
foreach(EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity))
{
#>
<#=PropertyVirtualModifier(Accessibility.ForProperty(edmProperty))#> <#=code.Escape(edmProperty.TypeUsage)#> <#=code.Escape(edmProperty)#>
{
<#=code.SpaceAfter(Accessibility.ForGetter(edmProperty))#>get dfgh{ return <#=code.FieldName(edmProperty)#>; }
<#=code.SpaceAfter(Accessibility.ForSetter(edmProperty))#>set { <#=code.FieldName(edmProperty)#> = value; }
}
private <#=code.Escape(edmProperty.TypeUsage)#> <#=code.FieldName(edmProperty)#> = new <#=code.Escape(edmProperty.TypeUsage)#>();
<#
}
region.End();
////////
//////// Write Navigation properties -------------------------------------------------------------------------------------------
////////
region.Begin("Navigation Properties");
foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
{
NavigationProperty inverse = ef.Inverse(navProperty);
if (inverse != null && !IsReadWriteAccessibleProperty(inverse))
{
inverse = null;
}
#>
<#
if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
{
#>
<#=PropertyVirtualModifier(Accessibility.ForReadOnlyProperty(navProperty))#> ObservableCollection<<#=code.Escape(navProperty.ToEndMember.GetEntityType())#>Poco> <#=code.Escape(navProperty)#>
{
get //qwer4
{
CreateNew<#=code.Escape(navProperty)#>Collection();
return <#=code.FieldName(navProperty)#>;
}
set
{
<#
if (inverse != null || ((AssociationType)navProperty.RelationshipType).IsForeignKey)
{
#>
if (!Object.ReferenceEquals(<#=code.FieldName(navProperty)#>, value))
{
CreateNew<#=code.Escape(navProperty)#>Collection();
}
<#
}
else
{
#>
CreateNew<#=code.Escape(navProperty)#>Collection();
<#
}
#>
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("<#=code.Escape(navProperty)#>"));
}
}
private ObservableCollection<<#=code.Escape(navProperty.ToEndMember.GetEntityType())#>Poco> <#=code.FieldName(navProperty)#>;
private void CreateNew<#=code.Escape(navProperty)#>Collection()
{
if (<#=code.FieldName(navProperty)#> == null && _context.<#=code.Escape(navProperty)#> != null)
{
<#=code.FieldName(navProperty)#> = new ObservableCollection<<#=code.Escape(navProperty.ToEndMember.GetEntityType())#>Poco>();
IEnumerable<<#=code.Escape(navProperty.ToEndMember.GetEntityType())#>> efEntities = DbEntities.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Unchanged | EntityState.Deleted).Where(i => i.Entity is <#=code.Escape(navProperty.ToEndMember.GetEntityType())#>).Select(i => i.Entity).Cast<<#=code.Escape(navProperty.ToEndMember.GetEntityType())#>>();
foreach (var item in efEntities)
<#=code.FieldName(navProperty)#>.Add(new <#=code.Escape(navProperty.ToEndMember.GetEntityType())#>Poco(item, DbEntities));
//This will handle changes originating from the db-entities...
_context.<#=code.Escape(navProperty)#>.AssociationChanged -= <#=code.Escape(navProperty)#>_AssociationChanged;
_context.<#=code.Escape(navProperty)#>.AssociationChanged += <#=code.Escape(navProperty)#>_AssociationChanged;
//This will handle changes originating from the User-Interface...
<#=code.FieldName(navProperty)#>.CollectionChanged -= <#=code.Escape(navProperty)#>Collection_CollectionChanged;
<#=code.FieldName(navProperty)#>.CollectionChanged += <#=code.Escape(navProperty)#>Collection_CollectionChanged;
}
else if (<#=code.FieldName(navProperty)#> != null && _context.<#=code.Escape(navProperty)#> == null)
<#=code.FieldName(navProperty)#> = null;
}
void <#=code.Escape(navProperty)#>Collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null && e.NewItems.Count > 0)
{
_context.<#=code.Escape(navProperty)#>.AssociationChanged -= <#=code.Escape(navProperty)#>_AssociationChanged;
foreach (var newItem in e.NewItems)
_context.<#=code.Escape(navProperty)#>.Add((newItem as <#=code.Escape(navProperty.ToEndMember.GetEntityType())#>Poco).Context);
_context.<#=code.Escape(navProperty)#>.AssociationChanged += <#=code.Escape(navProperty)#>_AssociationChanged;
}
else if(e.OldItems != null && e.OldItems.Count > 0)
{
_context.<#=code.Escape(navProperty)#>.AssociationChanged -= <#=code.Escape(navProperty)#>_AssociationChanged;
foreach (var removedItem in e.OldItems)
_dbEntities.DeleteObject((removedItem as <#=code.Escape(navProperty.ToEndMember.GetEntityType())#>Poco).Context);
//_context.<#=code.Escape(navProperty)#>.Remove((removedItem as <#=code.Escape(navProperty.ToEndMember.GetEntityType())#>Poco).Context);
_context.<#=code.Escape(navProperty)#>.AssociationChanged += <#=code.Escape(navProperty)#>_AssociationChanged;
}
}
void <#=code.Escape(navProperty)#>_AssociationChanged(object sender, CollectionChangeEventArgs e)
{
<#=code.Escape(navProperty.ToEndMember.GetEntityType())#> elem = e.Element as <#=code.Escape(navProperty.ToEndMember.GetEntityType())#>;
if (elem != null)
{
if (e.Action == CollectionChangeAction.Add)
{
this.<#=code.FieldName(navProperty)#>.CollectionChanged -= <#=code.Escape(navProperty)#>Collection_CollectionChanged;
this.<#=code.FieldName(navProperty)#>.Add(new <#=code.Escape(navProperty.ToEndMember.GetEntityType())#>Poco(elem, DbEntities));
this.<#=code.FieldName(navProperty)#>.CollectionChanged += <#=code.Escape(navProperty)#>Collection_CollectionChanged;
}
else if (e.Action == CollectionChangeAction.Remove)
{
this.<#=code.FieldName(navProperty)#>.CollectionChanged -= <#=code.Escape(navProperty)#>Collection_CollectionChanged;
<#=code.Escape(navProperty.ToEndMember.GetEntityType())#>Poco pocoToRemove = this.<#=code.FieldName(navProperty)#>.Where(i => i.Context.Equals(elem)).SingleOrDefault();
this.<#=code.FieldName(navProperty)#>.Remove(pocoToRemove);
this.<#=code.FieldName(navProperty)#>.CollectionChanged += <#=code.Escape(navProperty)#>Collection_CollectionChanged;
}
else if (e.Action == CollectionChangeAction.Refresh)
{
<#=code.FieldName(navProperty)#>.CollectionChanged -= <#=code.Escape(navProperty)#>Collection_CollectionChanged;
new List<<#=code.Escape(navProperty.ToEndMember.GetEntityType())#>Poco>(<#=code.FieldName(navProperty)#>).ForEach(i => <#=code.FieldName(navProperty)#>.Remove(i));
<#=code.FieldName(navProperty)#>.CollectionChanged += <#=code.Escape(navProperty)#>Collection_CollectionChanged;
IEnumerable<<#=code.Escape(navProperty.ToEndMember.GetEntityType())#>> efEntities = DbEntities.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Unchanged | EntityState.Deleted).Where(i => i.Entity is <#=code.Escape(navProperty.ToEndMember.GetEntityType())#>).Select(i => i.Entity).Cast<<#=code.Escape(navProperty.ToEndMember.GetEntityType())#>>();
foreach (var item in efEntities)
<#=code.FieldName(navProperty)#>.Add(new <#=code.Escape(navProperty.ToEndMember.GetEntityType())#>Poco(item, DbEntities));
_context.<#=code.Escape(navProperty)#>.AssociationChanged -= <#=code.Escape(navProperty)#>_AssociationChanged;
_context.<#=code.Escape(navProperty)#>.AssociationChanged += <#=code.Escape(navProperty)#>_AssociationChanged;
}
}
}
<#
}
else
{
#>
<#=PropertyVirtualModifier(Accessibility.ForProperty(navProperty))#> <#=code.Escape(navProperty.ToEndMember.GetEntityType())#> <#=code.Escape(navProperty)#>
{
<#
if (inverse != null || ((AssociationType)navProperty.RelationshipType).IsForeignKey)
{
#>
<#=code.SpaceAfter(Accessibility.ForGetter(navProperty))#>get { return _context.<#=code.Escape(navProperty)#>; } //dfgh2 Type:<#=code.Escape(navProperty.ToEndMember.GetEntityType())#>
<#=code.SpaceAfter(Accessibility.ForSetter(navProperty))#>set
{
if (!Object.ReferenceEquals(_context.<#=code.Escape(navProperty)#>, value))
{
_context.<#=code.Escape(navProperty)#>Reference.AssociationChanged -= <#=code.Escape(navProperty)#>_AssociationChanged;
_context.<#=code.Escape(navProperty)#> = value;
_context.<#=code.Escape(navProperty)#>Reference.AssociationChanged += <#=code.Escape(navProperty)#>_AssociationChanged;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("<#=code.Escape(navProperty)#>"));
}
}
}
void <#=code.Escape(navProperty)#>_AssociationChanged(object sender, CollectionChangeEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("<#=code.Escape(navProperty)#>"));
}
<#
}
else
{
#>
<#=code.SpaceAfter(Accessibility.ForGetter(navProperty))#>get dfgh3;
<#=code.SpaceAfter(Accessibility.ForSetter(navProperty))#>set;
}
<#
}
}
}
region.End();
region.Begin("Association Fixup");
region.End();
#>
}
<#
EndNamespace(namespaceName);
}
foreach (ComplexType complex in ItemCollection.GetItems<ComplexType>().OrderBy(e => e.Name))
{
fileManager.StartNewFile(complex.Name + ".cs");
BeginNamespace(namespaceName, code);
#>
<#=Accessibility.ForType(complex)#> partial class <#=code.Escape(complex)#>
{
<#
region.Begin("Primitive Properties");
foreach(EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == complex))
{
bool isDefaultValueDefinedInModel = (edmProperty.DefaultValue != null);
#>
<#=Accessibility.ForProperty(edmProperty)#> <#=code.Escape(edmProperty.TypeUsage)#> <#=code.Escape(edmProperty)#>
<#
if (isDefaultValueDefinedInModel)
{
#>
{
<#=code.SpaceAfter(Accessibility.ForGetter(edmProperty))#>get dfgh4{ return <#=code.FieldName(edmProperty)#>; }
<#=code.SpaceAfter(Accessibility.ForSetter(edmProperty))#>set { <#=code.FieldName(edmProperty)#> = value; }
}
private <#=code.Escape(edmProperty.TypeUsage)#> <#=code.FieldName(edmProperty)#><#=code.StringBefore(" = ", code.CreateLiteral(edmProperty.DefaultValue))#>;
<#
}
else
{
#>
{
<#=code.SpaceAfter(Accessibility.ForGetter(edmProperty))#>get dfgh5;
<#=code.SpaceAfter(Accessibility.ForSetter(edmProperty))#>set;
}
<#
}
}
region.End();
region.Begin("Complex Properties");
foreach(EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex))
{
#>
<#=Accessibility.ForProperty(edmProperty)#> <#=code.Escape(edmProperty.TypeUsage)#> <#=code.Escape(edmProperty)#>
{
<#=code.SpaceAfter(Accessibility.ForGetter(edmProperty))#>get dfgh6{ return <#=code.FieldName(edmProperty)#>; }
<#=code.SpaceAfter(Accessibility.ForSetter(edmProperty))#>set { <#=code.FieldName(edmProperty)#> = value; }
}
private <#=code.Escape(edmProperty.TypeUsage)#> <#=code.FieldName(edmProperty)#> = new <#=code.Escape(edmProperty.TypeUsage)#>();
<#
}
region.End();
#>
}
<#
EndNamespace(namespaceName);
}
if (!VerifyTypesAreCaseInsensitiveUnique(ItemCollection))
{
return "";
}
fileManager.Process();
#>
<#+
void WriteHeader(EntityFrameworkTemplateFileManager fileManager, params string[] extraUsings)
{
fileManager.StartHeader();
#>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template written by Chad W. Stoker.
// 10/18/2011
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Data;
using System.ComponentModel;
<#=String.Join(String.Empty, extraUsings.Select(u => "using " + u + ";" + Environment.NewLine).ToArray())#>
<#+
fileManager.EndBlock();
}
void BeginNamespace(string namespaceName, CodeGenerationTools code)
{
CodeRegion region = new CodeRegion(this);
if (!String.IsNullOrEmpty(namespaceName))
{
#>
namespace <#=code.EscapeNamespace(namespaceName)#>
{
<#+
PushIndent(CodeRegion.GetIndent(1));
}
}
void EndNamespace(string namespaceName)
{
if (!String.IsNullOrEmpty(namespaceName))
{
PopIndent();
#>
}
<#+
}
}
bool IsReadWriteAccessibleProperty(EdmMember member)
{
string setter = Accessibility.ForWriteOnlyProperty(member);
string getter = Accessibility.ForReadOnlyProperty(member);
return getter != "private" && getter != "protected" && setter != "private" && setter != "protected";
}
string PropertyVirtualModifier(string accessibility)
{
return accessibility + (accessibility != "private" ? " virtual" : "");
}
void WriteCustomObservableCollection()
{
#>
// An System.Collections.ObjectModel.ObservableCollection that raises
// individual item removal notifications on clear and prevents adding duplicates.
public class FixupCollection<T> : ObservableCollection<T>
{
protected override void ClearItems()
{
new List<T>(this).ForEach(t => Remove(t));
}
protected override void InsertItem(int index, T item)
{
if (!this.Contains(item))
{
base.InsertItem(index, item);
}
}
}
<#+
}
bool VerifyTypesAreCaseInsensitiveUnique(EdmItemCollection itemCollection)
{
Dictionary<string, bool> alreadySeen = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
foreach(StructuralType type in itemCollection.GetItems<StructuralType>())
{
if (!(type is EntityType || type is ComplexType))
{
continue;
}
if (alreadySeen.ContainsKey(type.FullName))
{
Error(String.Format(CultureInfo.CurrentCulture, "This template does not support types that differ only by case, the types {0} are not supported", type.FullName));
return false;
}
else
{
alreadySeen.Add(type.FullName, true);
}
}
return true;
}
#> |




