Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / C#

Parsing the Entity Framework EDMX file

Rate me:
Please Sign up or sign in to vote.
4.38/5 (7 votes)
3 Jun 2010CPOL4 min read 58.4K   1.2K   26  
Looking at ways to parse an Entity Framework EDMX file
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="EdmxLibrary" #>
<#@ import namespace="EdmxLibrary.DataObjects" #>

<#@ include file="Source\Global.tt" #>
<#@ include file="Source\Container.Model.tt" #>
<#@ include file="Source\Container.PocoAdapters.tt" #>
<#@ include file="Source\Model.tt" #>
<#@ include file="Source\PocoAdapters.tt" #>
<#@ include file="Source\PocoProxies.tt" #>

<#+
// <copyright file="EFPocoGenerator.tt" company="Webbert Solutions, LLC">
//  Copyright � Webbert Solutions, LLC. All Rights Reserved.
// </copyright>

/// <summary>
/// 
/// </summary>
public class EFPocoGenerator : Generator
{
	public Csdl Csdl;
	
	private Global _globalTemplate = new Global();
	private ContainerModel _containerModelTemplate = new ContainerModel();
	private ContainerPocoAdapters _containerPocoAdaptersTemplate = new ContainerPocoAdapters();
	private Model _modelTemplate = new Model();
	private PocoAdapters _pocoAdaptersTemplate = new PocoAdapters();
	private PocoProxies _pocoProxiesTemplate = new PocoProxies();

    #region protected

	/// <summary>
	/// Generates output files
	/// </summary>
	protected override void RunCore()
	{
		foreach (Container container in Csdl.Containers)
		{
			RunGlobalTemplate();
			RunContainerModelTemplate(container);
			RunContainerPocoAdaptersTemplate(container);
			RunModelTemplate(container);
			RunPocoAdaptersTemplate(container);
			RunPocoProxiesTemplate(container);
		}
	}

	private void RunGlobalTemplate()
	{
		// Initialize Template and Render
		this._globalTemplate.Output.File = @"Output\Global.cs";
		this._globalTemplate.Csdl = Csdl;
		this._globalTemplate.Render();
	}

	private void RunContainerModelTemplate(Container container)
	{
		// Initialize Template and Render
		this._containerModelTemplate.Output.File = string.Format(@"Output\{0}\{1}.cs",
					Csdl.Containers[0].Entities[0].Entity.EntityTypeNamespace, Csdl.Containers[0].Name);
		this._containerModelTemplate.container = container;
		this._containerModelTemplate.Render();
	}

	private void RunContainerPocoAdaptersTemplate(Container container)
	{
		// Initialize Template and Render
		this._containerPocoAdaptersTemplate.Output.File = string.Format(@"Output\{0}.PocoAdapters\{1}.cs",
					Csdl.Containers[0].Entities[0].Entity.EntityTypeNamespace, Csdl.Containers[0].Name);
		this._containerPocoAdaptersTemplate.container = container;
		this._containerPocoAdaptersTemplate.Render();
	}

	private void RunModelTemplate(Container container)
	{
		foreach (EntitySet entitySet in container.Entities)
		{
			// Initialize Template and Render
			this._modelTemplate.Output.File = string.Format(@"Output\{0}\{1}.cs",
				entitySet.Entity.EntityTypeNamespace, entitySet.Entity.EntityTypeName);
			this._modelTemplate.entity = entitySet.Entity;
			this._modelTemplate.Render();
		}
		
		foreach (EntityType entityType in container.Structures)
		{
			// Initialize Template and Render
			this._modelTemplate.Output.File = string.Format(@"Output\{0}\{1}.cs",
				entityType.EntityTypeNamespace, entityType.EntityTypeName);
			this._modelTemplate.entity = entityType;
			this._modelTemplate.Render();
		}
	}

	private void RunPocoAdaptersTemplate(Container container)
	{
		foreach (EntitySet entitySet in container.Entities)
		{
			// Initialize Template and Render
			this._pocoAdaptersTemplate.Output.File = string.Format(@"Output\{0}.PocoAdapters\{1}Adapter.cs",
				entitySet.Entity.EntityTypeNamespace, entitySet.Entity.EntityTypeName);
			this._pocoAdaptersTemplate.entity = entitySet.Entity;
			this._pocoAdaptersTemplate.Render();
		}
		
		foreach (EntityType entityType in container.Structures)
		{
			// Initialize Template and Render
			this._pocoAdaptersTemplate.Output.File = string.Format(@"Output\{0}.PocoAdapters\{1}Adapter.cs",
				entityType.EntityTypeNamespace, entityType.EntityTypeName);
			this._pocoAdaptersTemplate.entity = entityType;
			this._pocoAdaptersTemplate.Render();
		}
	}

	private void RunPocoProxiesTemplate(Container container)
	{
		foreach (EntitySet entitySet in container.Entities)
		{
			// Initialize Template and Render
			this._pocoProxiesTemplate.Output.File = string.Format(@"Output\{0}.PocoProxies\{1}Proxy.cs",
				entitySet.Entity.EntityTypeNamespace, entitySet.Entity.EntityTypeName);
			this._pocoProxiesTemplate.entity = entitySet.Entity;
			this._pocoProxiesTemplate.Render();
		}
		
		foreach (EntityType entityType in container.Structures)
		{
			// Initialize Template and Render
			this._pocoProxiesTemplate.Output.File = string.Format(@"Output\{0}.PocoProxies\{1}Proxy.cs",
				entityType.EntityTypeNamespace, entityType.EntityTypeName);
			this._pocoProxiesTemplate.entity = entityType;
			this._pocoProxiesTemplate.Render();
		}
	}

	/// <summary>
	/// Validates code generation parameters
	/// </summary>
	protected override void Validate()
	{
        Validate(this._globalTemplate,					"Global");
        Validate(this._containerModelTemplate,			"ContainerModel");
        Validate(this._containerPocoAdaptersTemplate,	"ContainerPocoAdapters");
        Validate(this._modelTemplate,					"Model");
        Validate(this._pocoAdaptersTemplate,			"PocoAdapters");
        Validate(this._pocoProxiesTemplate,				"PocoProxies");
	}
	
	private void Validate(object template, string name)
	{
	   if (template == null)
			throw new TransformationException(name + " property must be assigned");
	}

    #endregion protected
}
#>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Webbert Solutions
United States United States
Dave is an independent consultant working in a variety of industries utilizing Microsoft .NET technologies.

Comments and Discussions