<# //----------------------------------------------------------------------- - // Entity Framework T4 Template // // Use to generate trackable DTOs for the DAL in the service application. //----------------------------------------------------------------------- --

climbmoujeanteaSoftware and s/w Development

Dec 13, 2013 (3 years and 6 months ago)

95 views

<#

//
-----------------------------------------------------------------------
-

// Entity Framework T4 Template

//

// Use to generate trackable DTOs for the DAL in the service application.

//
-------------------------------------------------------------
----------
--


var version = "0.1";


#><#@ template language="C#" debug="false" hostspecific="true"#>

<#@ include file="EF.Utility.CS.ttinclude"#>

<#@ output extension = ".txt" #>

<#@ assembly name="System.Core" #>

<#@ assembly name="System.Data" #>

<#@ assembly name="System.Data.Entity" #>

<#@ assembly name="System.Data.Entity.Design" #>

<#@ assembly name="System.Xml" #>

<#@ assembly name="System.Xml.Linq" #>

<#@ assembly name="Microsoft.VisualStudio.OLE.Interop" #>

<#@ assembly name="Microsof
t.VisualStudio.Shell" #>

<#@ assembly name="Microsoft.VisualStudio.Shell.Interop" #>

<#@ assembly name="EnvDTE" #>

<#@ import namespace="System" #>

<#@ import namespace="System.Linq" #>

<#@ import namespace="System.Diagnostics" #>

<#@ import namespac
e="System.IO" #>

<#@ import namespace="System.Text" #>

<#@ import namespace="System.Collections.Generic" #>

<#@ import namespace="System.Data.Entity.Design" #>

<#@ import namespace="System.Data.Metadata.Edm" #>

<#@ import namespace="System.Data.Mappin
g" #>

<#@ import namespace="System.Xml" #>

<#@ import namespace="System.Xml.Linq" #>

<#@ import namespace="System.Collections.ObjectModel" #>

<#@ import namespace="System.CodeDom" #>

<#@ import namespace="System.CodeDom.Compiler" #>

<#@ import namesp
ace="Microsoft.VisualStudio.Shell" #>

<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>

<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>

<#

string inputFile =
@"C:
\
Projects
\
TZ.CenturionServer
\
trunk
\
TZ.CenturionPro
\
TZ.Centur
ionPro.Ab
stractions
\
TZ.CenturionPro.edmx";

var Edm = GetEdmItemCollection(inputFile);

var Code = new CSharpCodeLanguage();

CodeGenerationTools code = new CodeGenerationTools(this);

string namespaceName = code.VsNamespaceSuggestion();

#>



<#

forea
ch(EntityType entity in Edm.AllEntities.OrderBy(e => e.FullName))

{

#>

<#

List<string> namesToMatch = new List<string>();

namesToMatch.Add(entity.Name);

IterateEntityType(entity,entity.Name,1, namesToMatch);

}

#>




<#+

////////

//////// UTIL
ITY METHODS
-

do not modify anything below this point

////////


//

// Code for building up a convenient item collection for the CSDL from
the EDMX

//


private void IterateEntityType(EntityType entity, string currentPath, int
depth, List<string> type
NamesToMatch)

{


if(depth > 2) return;


foreach(NavigationProperty navProperty in
entity.NavigationProperties.Where(n => n.DeclaringType == entity))


{



if(navProperty.Name == "CreatedByContact"




|| navProperty.Name == "ModifiedByContact"




||
navProperty.Name == "CreatedByUser"




|| navProperty.Name == "ModifiedByUser"




|| navProperty.Name == "ParentAsset") continue;




string elementType =
GetEntityType(navProperty.ToEndMember).Name;



string relationshipName =
navProperty.ToEndMember
.DeclaringType.FullName;



string targetRoleName = navProperty.ToEndMember.Name;



if(typeNamesToMatch.Contains(targetRoleName))



{




this.WriteLine("
\
t" + currentPath + "." +
navProperty.Name + "(typeof:" + targetRoleName + ")");



}



typeNamesTo
Match.Add(entity.Name);



IterateEntityType(GetEntityType(navProperty.ToEndMember),currentPat
h + "." + navProperty.Name, depth+1,typeNamesToMatch);


}

}


private ItemCollectionWrapper<EdmItemCollection>
GetEdmItemCollection(string edmxFile)

{


Ed
mItemCollection edmItems = new EdmItemCollection(new XmlReader[] {
GetCSDLReader(edmxFile) });


return new ItemCollectionWrapper<EdmItemCollection>(edmItems);

}


public class ItemCollectionWrapper<T> where T : ItemCollection

{


T _collection;


public ItemCollectionWrapper(T collection)


{


_collection = collection;


}



public T Collection { get{ return _collection; }}



public IEnumerable<EntityContainer> AllEntityContainers


{


get


{



return _collection.GetItems<EntityContainer>();


}


}



public IEnumerable<EntityType> AllEntities


{


get


{


return _collection.GetItems<EntityType>();


}


}



public IEnumerable<Asso
ciationType> AllAssociations


{


get


{


return _collection.GetItems<AssociationType>();


}


}




public string NamespaceName


{



get



{




EntityType type = this.AllEntities.FirstOrDefault(e =>
e.N
amespaceName != null);




if (type != null && type.NamespaceName != null)




{





return type.NamespaceName;




}




return null;



}


}

}



private XmlReader GetCSDLReader(string edmxFile)

{


XNamespace edmxns = "http://schemas.microsoft.
com/ado/2008/10/edmx";


XNamespace csdlns = "http://schemas.microsoft.com/ado/2008/09/edm";


XDocument edmxDoc = XDocument.Load(edmxFile);


XElement edmxNode = edmxDoc.Element(edmxns + "Edmx");


XElement runtimeNode = edmxNode.Element(edmxn
s + "Runtime");


XElement conceptualModelsNode = runtimeNode.Element(edmxns +
"ConceptualModels");


XElement csdlContent = conceptualModelsNode.Element(csdlns +
"Schema");


return csdlContent.CreateReader();

}



//

// Convenience methods

//


public EntityType GetEntityType(RelationshipEndMember end)

{


return (EntityType)((RefType)end.TypeUsage.EdmType).ElementType;

}


public string GetEnumValueString(object enumValue)

{


Type type = enumValue.GetType();


Debug.Assert(ty
pe.IsEnum, "this type is not an enum");



return type.FullName + "." + enumValue.ToString();

}


bool IncludePropertyInFactoryMethod(EdmProperty property)

{


if (property.Nullable)


{



return false;


}


if (property.DefaultValue != n
ull)


{


return false;


}



return true;

}


string GetSetValidValueParameters(PrimitiveType type, bool nullable)

{


string parameters = "value";


if(type.ClrEquivalentType.IsClass)



parameters += ", " + nullable.ToString().ToLo
wer();





return parameters;

}


//

// language specific code generation classes

//



abstract class CodeLanguage

{


protected CodeDomProvider CodeDomProvider { get; set; }




protected CodeLanguage(CodeDomProvider codeDomProvider)


{



Co
deDomProvider = codeDomProvider;


}




public String Format(Type type)


{



return ShortenTypeRef(CodeDomProvider.GetTypeOutput(new
CodeTypeReference(type)));


}




public String Format(TypeUsage typeUsage)


{



if(typeUsage.EdmType is ComplexTy
pe)



{




return typeUsage.EdmType.Name;



}



else if (typeUsage.EdmType is PrimitiveType)



{




Type clrType =
((PrimitiveType)typeUsage.EdmType).ClrEquivalentType;




string elementType = Format(clrType);




if(clrType.IsValueType && IsNullabl
e(typeUsage))




{





return elementType + "?";




}




return elementType;



}






return "UnknownType";


}




public String FormatIdentifier(string identifier)


{



return CodeDomProvider.CreateEscapedIdentifier(identifier);


}




public

string FormatParameter(string identifier)


{



return FormatIdentifier(CamelCase(identifier));


}




public string FormatField(string identifier)


{



return FormatIdentifier("_" + identifier);


}




protected string CamelCase(string identifier)


{



if (string.IsNullOrEmpty(identifier))




return identifier;




if (identifier.Length == 1)




return
identifier[0].ToString(System.Globalization.CultureInfo.InvariantCulture)
.ToLowerInvariant();




return
identifier[0].ToString(System.Globali
zation.CultureInfo.InvariantCulture)
.ToLowerInvariant() + identifier.Substring(1);


}




protected virtual String ShortenTypeRef(String typeRef)


{



return (typeRef.LastIndexOf('.') != 6) ?
typeRef.Replace("System.Data.Linq.","") : typeRef.Replace("
System.","");


}




protected String Format(CodeTypeReference codeTypeRef)


{



return
ShortenTypeRef(CodeDomProvider.GetTypeOutput(codeTypeRef));


}




protected bool IsNullable(TypeUsage typeUsage)


{



return (bool)typeUsage.Facets["Nullable"
].Value;


}

}


class CSharpCodeLanguage : CodeLanguage

{


public CSharpCodeLanguage() : base(new
Microsoft.CSharp.CSharpCodeProvider()) { }



protected override String ShortenTypeRef(String typeRef)


{



if (typeRef.StartsWith("System.Nullable<
"))




typeRef =
typeRef.Replace("System.Nullable<","").Replace(">","?");



return base.ShortenTypeRef(typeRef);


}

}


class VBCodeLanguage : CodeLanguage

{


public VBCodeLanguage() : base(new
Microsoft.VisualBasic.VBCodeProvider()) { }

}

#>