PMunin.com Navigation

Google ads

Friday, January 3, 2014

Data Access Tracing in .NET (Universal for any provider)

Sometimes I'm surprised to see in .NET some very obvious, required functionality not available. I have to implement it by my own. When you develop database-driven web application you face a lot of issues and you need to diagnose if the issue is in database logic or on middle tier or on client. You can trace client and middle tier easily, but for some reason Microsoft thinks that you don't need the same with database access. Solution they provide to trace SQL requests are supercomplex and heavy (SQL Extended Events,...).

So I had to spend couple of days to research and implement generic DbConnection tracing approach that supposed to work with any DbProvider. This approach should work with direct DbConnection as well as with ORM tools (EntityFramework). The idea is to replace standard DbProviderFactories with a wrappers, that will trace all necessary information. The Idea was taken from Glimpse.Ado.

Here is the code:

    public abstract class TraceableDbProviderFactory:DbProviderFactory
    {
        static TraceSource trace = new TraceSource("TraceableDbProviderFactory");
        public static TraceSource Trace { get { return trace; } }
 
        static bool isEnabled = false;
        public static bool IsEnabled
        {
            get { return isEnabled; }
            set 
            {
                if (isEnabled == valuereturn;
                if (value)
                    Register();
                else 
                    Unregister();
                isEnabled = value;
            }
        }
 
        static TraceableDbProviderFactory()
        {
        }
 
        static void Register()
        {
            var providers = FindDbProviderFactoryTable();
            providers.Columns["AssemblyQualifiedName"].ReadOnly = false;
 
            foreach (DataRow registeredFactory in providers.Rows)
            {
                var currentType = Type.GetType(registeredFactory["AssemblyQualifiedName"as string);
                if (currentType.IsSubclassOf(typeof(TraceableDbProviderFactory)))
                    continue;
                Type type = typeof(TraceableDbProviderFactory<>).MakeGenericType(new Type[]
    {
                    currentType
    });
 
                registeredFactory["AssemblyQualifiedName"] = type.AssemblyQualifiedName;
            }
 
            providers.Columns["AssemblyQualifiedName"].ReadOnly = true;
        }
 
        static void Unregister()
        {
            var providers = FindDbProviderFactoryTable();
            providers.Columns["AssemblyQualifiedName"].ReadOnly = false;
 
 
            foreach (DataRow registeredFactory in providers.Rows)
            {
                var factory = DbProviderFactories.GetFactory(registeredFactory) as TraceableDbProviderFactory;
                if (factory == nullcontinue;
                var innerType = factory.Inner.GetType();
                registeredFactory["AssemblyQualifiedName"] = innerType.AssemblyQualifiedName;
            }
 
            providers.Columns["AssemblyQualifiedName"].ReadOnly = true;
        }
 
        static DataTable FindDbProviderFactoryTable()
        {
            DbProviderFactories.GetFactoryClasses();
            Type typeFromHandle = typeof(DbProviderFactories);
            FieldInfo fieldInfo = typeFromHandle.GetField("_configTable"BindingFlags.Static | BindingFlags.NonPublic) ?? typeFromHandle.GetField("_providerTable"BindingFlags.Static | BindingFlags.NonPublic);
            object value = fieldInfo.GetValue(null);
            if (!(value is DataSet))
            {
                return (DataTable)value;
            }
            return ((DataSet)value).Tables["DbProviderFactories"];
        }
 
        protected static DbProviderFactory GetDbProviderInstance(Type type)
        {
            var fld = type.GetField("Instance"BindingFlags.Public | BindingFlags.Static);
            return fld.GetValue(nullas DbProviderFactory;
        }
 
        public DbProviderFactory Inner { getprotected set; }
 
    }
 
    public class TraceableDbProviderFactory<T>
        : TraceableDbProviderFactory 
        , IServiceProvider
        where T: DbProviderFactory
    {
 
        public static readonly TraceableDbProviderFactory<T> Instance 
            = new TraceableDbProviderFactory<T>();
 
        public TraceableDbProviderFactory()
        {
            Inner = GetDbProviderInstance(typeof(T)) as T;
        }
 
        public override DbConnection CreateConnection()
        {
            if (!IsEnabled) return Inner.CreateConnection();
            return new TraceableConnection(this);
        }
 
        public object GetService(Type serviceType)
        {
            if (!IsEnabled) return ((IServiceProvider)Inner).GetService(serviceType);
 
 
            if (serviceType == typeof(DbProviderServices))
                return new TraceableDbProviderServices(this);
 
            var r = Inner as IServiceProvider;
            var innerRes = r.GetService(serviceType);
 
            return innerRes;
        }
 
        public override DbCommand CreateCommand()
        {
            if (!IsEnabled) return Inner.CreateCommand();
            var res = new TraceableCommand(this);
            return res;
        }
        public override DbParameter CreateParameter()
        {
            return Inner.CreateParameter();
        }
        public override DbCommandBuilder CreateCommandBuilder()
        {
            return Inner.CreateCommandBuilder();
        }
    }
 
    public class TraceableConnection:DbConnection
    {
        protected override DbProviderFactory DbProviderFactory
        {
            get
            {
                return Parent;
            }
        }
 
        public DbConnection Inner { getprotected set; }
        public TraceableDbProviderFactory Parent { getprotected set; }
        public TraceableConnection(TraceableDbProviderFactory parent, DbConnection inner=null)
        {
            this.Parent = parent;
            Inner = inner??parent.Inner.CreateConnection();
        }
 
        protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
        {
            return Inner.BeginTransaction(isolationLevel);
        }
 
        public override void ChangeDatabase(string databaseName)
        {
            Inner.ChangeDatabase(databaseName);
        }
 
        public override void Close()
        {
            Inner.Close();
        }
 
        public override string ConnectionString
        {
            get
            {
                return Inner.ConnectionString;
            }
            set
            {
                Inner.ConnectionString = value;
            }
        }
 
        protected override DbCommand CreateDbCommand()
        {
            return new TraceableCommand(this);
        }
 
        public override string DataSource
        {
            get { return Inner.DataSource; }
        }
 
        public override string Database
        {
            get { return Inner.Database; }
        }
 
        public override void Open()
        {
            Inner.Open();
        }
 
        public override string ServerVersion
        {
            get { return Inner.ServerVersion; }
        }
 
        public override ConnectionState State
        {
            get { return Inner.State; }
        }
    }
 
    public class TraceableDbProviderServices : DbProviderServices
    {
        public TraceableDbProviderFactory Parent { getprotected set; }
 
        public DbProviderServices Inner { getprotected set; }
 
        public TraceableDbProviderServices(TraceableDbProviderFactory parent)
        {
            this.Parent = parent;
            var innerServiceProvider = Parent.Inner as IServiceProvider;
            this.Inner = innerServiceProvider.GetService(typeof(DbProviderServices)) 
                as DbProviderServices;
        }
 
        protected override DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, System.Data.Common.CommandTrees.DbCommandTree commandTree)
        {
            var innerRes = Inner.CreateCommandDefinition(providerManifest, commandTree);
            var res = new TraceableCommandDefinition(this, innerRes);
            return res;
        }
 
        protected override DbProviderManifest GetDbProviderManifest(string manifestToken)
        {
            var res = Inner.GetProviderManifest(manifestToken);
            return res;
        }
 
        protected override string GetDbProviderManifestToken(DbConnection connection)
        {
            var res = Inner.GetProviderManifestToken(connection);
            return res;
        }
    }
 
    public class TraceableCommandDefinition : DbCommandDefinition
    {
        public TraceableDbProviderServices Parent { getprotected set; }
 
        public DbCommandDefinition Inner { getprotected set; }
 
        public TraceableCommandDefinition(TraceableDbProviderServices parent, DbCommandDefinition inner)
        {
            this.Parent = parent;
            this.Inner = inner;
        }
 
        public override DbCommand CreateCommand()
        {
            var res = new TraceableCommand(Parent.Parent, Inner.CreateCommand());
            return res;
        }
    }
 
 
 
    public class TraceableCommand:DbCommand
    {
        public TraceableDbProviderFactory Parent{get;protected set;}
        public DbCommand Inner { getprotected set; }
        public TraceableCommand(TraceableDbProviderFactory parent, DbCommand inner=null)
        {
            this.Parent = parent;
            this.Inner = inner?? parent.Inner.CreateCommand();
        }
 
        public TraceableCommand(TraceableConnection connection, DbCommand inner=null)
        {
            this.traceableConnection = connection;
            this.Inner = inner?? connection.Inner.CreateCommand();
        }
 
        public override void Cancel()
        {
            Inner.Cancel();
        }
 
        public override string CommandText
        {
            get
            {
                return Inner.CommandText;
            }
            set
            {
                Inner.CommandText = value;
            }
        }
 
        public override int CommandTimeout
        {
            get
            {
                return Inner.CommandTimeout;
            }
            set
            {
                Inner.CommandTimeout = value;
            }
        }
 
        public override CommandType CommandType
        {
            get
            {
                return Inner.CommandType;
            }
            set
            {
                Inner.CommandType = value;
            }
        }
 
        protected override DbParameter CreateDbParameter()
        {
            return Inner.CreateParameter();
        }
 
        TraceableConnection traceableConnection;
        protected override DbConnection DbConnection
        {
            get
            {
                return traceableConnection;
            }
            set
            {
                if(value==null)
                {
                    traceableConnection = null;
                    Inner.Connection = null;
                    return;
                }
                traceableConnection = value as TraceableConnection;
                if (traceableConnection == null)
                {
                    traceableConnection = new TraceableConnection(Parent, value);
                }
                Inner.Connection = traceableConnection.Inner;
            }
        }
 
        protected override DbParameterCollection DbParameterCollection
        {
            get { return Inner.Parameters; }
        }
 
        protected override DbTransaction DbTransaction
        {
            get
            {
                return Inner.Transaction;
            }
            set
            {
                Inner.Transaction = value ;
            }
        }
 
        public override bool DesignTimeVisible
        {
            get
            {
                return Inner.DesignTimeVisible;
            }
            set
            {
                Inner.DesignTimeVisible = value;
            }
        }
 
        protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
        {
            if (TraceableDbProviderFactory.IsEnabled)
                Trace.TraceInformation("DbCommand.ExecuteDataReader \n" + ToString());
            return Inner.ExecuteReader(behavior);
        }
 
        public TraceSource Trace { get { return TraceableDbProviderFactory.Trace; } }
 
        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.AppendLine("===CommandText===");
            sb.AppendLine(CommandText);
            sb.AppendLine("=================");
            if (Parameters.Count > 0)
            {
                sb.AppendLine("===Parameters====");
                foreach (DbParameter parameter in Parameters)
                {
                    sb.AppendLine(string.Format("{0} = {1}", parameter.ParameterName, parameter.Value));
                }
                sb.AppendLine("=================");
            }
            //sb.AppendFormat("CommandText=\"{0}\", Parameters={1}"
            //    , CommandText
            //    , string.Join(", ", Parameters.Cast<DbParameter>()
            //    .Select(p => string.Format("{0}:{1}", p.ParameterName, p.Value)))
            //    );
            return sb.ToString();
        }
 
        public override int ExecuteNonQuery()
        {
            if(TraceableDbProviderFactory.IsEnabled)
                Trace.TraceInformation("DbCommand.ExecuteNonQuery \n" + ToString());
            return Inner.ExecuteNonQuery();
        }
 
        public override object ExecuteScalar()
        {
            if (TraceableDbProviderFactory.IsEnabled)
                Trace.TraceInformation("ExecuteScalarCommand: \n" + ToString());
            return Inner.ExecuteScalar();
        }
 
        public override void Prepare()
        {
            Inner.Prepare();
        }
 
        public override UpdateRowSource UpdatedRowSource
        {
            get
            {
                return Inner.UpdatedRowSource;
            }
            set
            {
                Inner.UpdatedRowSource = value;
            }
        }
    }





And here is what you need to add to web.config:

  <system.diagnostics>
    <sharedListeners>
      <add initializeData="FullTrace.svclog" IsEnabled="true" type="System.Diagnostics.XmlWriterTraceListener" name="fullTraceXml">
      </add>
    </sharedListeners>
    <sources>
      <source name="TraceableDbProviderFactory" switchValue="All">
        <listeners>
          <add name="fullTraceXml" />
        </listeners>
      </source>
    </sources>
    <trace autoflush="true">
    </trace>
  </system.diagnostics>


In application initialization module add line:
TraceableDbProviderFactory.IsEnabled = true;

Enjoy! 

Wednesday, October 16, 2013

Entity Framework Extensions to execute scalar queries (.First(), .Any(), .Count(), ...)

If you have used LINQ to SQL, Entity Framework you might noticed that when you use queryable extensions returning set of objects (like .Select(), .Where(), .Join(), Group...) you get defered-executable queries that will be converted to SQL end executed during first demand. You can combine those queries, join them together, and it eventually will be still one SQL request. However you don't get the same when you work with functions returning not Enumerable, but actual value (like .First(), .Any(), .Max(), ...). So thanks to people from Stackoverflow, here are extensions for DbContext and DbQuery (Entity Framework 5), that will allow you to execute those operations still getting back Queryable results:

public static class DbContextScalarQueryExtensions
{
 
    public static object GetInternalContext(this DbContext context)
    {
        var provProp = context.GetType().GetProperty("InternalContext"BindingFlags.Instance | BindingFlags.NonPublic);
        return provProp.GetValue(context, null);
    }
 
    public static IQueryProvider GetRootObjectQueryProvider(this DbContext context)
    {
        var ctx = (context as IObjectContextAdapter).ObjectContext;
 
        var provProp = ctx.GetType().GetProperty("QueryProvider"BindingFlags.Instance | BindingFlags.NonPublic);
        var provider = provProp.GetValue(ctx, nullas IQueryProvider//ObjectQueryProvider
        return provider;
    }
 
    static IQueryProvider CreateRootDbQueryProvider(DbContext context)
    {
        var dbQueryProviderType = Type.GetType("System.Data.Entity.Internal.Linq.DbQueryProvider, EntityFramework");
        var dbQueryProvider = Activator.CreateInstance(dbQueryProviderType, context.GetInternalContext(), context.GetRootObjectQueryProvider())
as IQueryProvider;
        return dbQueryProvider as IQueryProvider;
    }
 
 
    static Expression<Func<TResult>> RefactorExpression<TArgument, TResult>(Expression<Func<TArgument, TResult>> expOriginal, TArgument arg)
    {
        Expression<Func<TArgument>> expTemplate = () => arg;
 
        var arg2 = expTemplate.Body;
        var newBody = new Rewriter(expOriginal.Parameters[0], arg2).Visit(expOriginal.Body);
        return Expression.Lambda<Func<TResult>>(newBody);
    }
 
    class Rewriter : ExpressionVisitor
    {
        private readonly Expression candidate_;
        private readonly Expression replacement_;
 
        public Rewriter(Expression candidate, Expression replacement)
        {
            candidate_ = candidate;
            replacement_ = replacement;
        }
 
        public override Expression Visit(Expression node)
        {
            return node == candidate_ ? replacement_ : base.Visit(node);
        }
    }
 
    public static IQueryable<TResult> CreateScalarQuery<TDbContext, TResult>(this TDbContext context, Expression<Func<TDbContext, TResult>> expression)
        where TDbContext : DbContext
    {
        var resExp = RefactorExpression<TDbContext, TResult>(expression, (TDbContext)context);
        var realExp = Expression.Call(
                method: GetMethodInfo(() => Queryable.Select<int, TResult>(null, (Expression<Func<int, TResult>>)null)),
                arg0: Expression.Call(
                    method: GetMethodInfo(() => Queryable.AsQueryable<int>(null)),
                    arg0: Expression.NewArrayInit(typeof(int), Expression.Constant(1))),
                arg1: Expression.Lambda(body: resExp.Body, parameters: new[] { Expression.Parameter(typeof(int)) }));
 
        return CreateRootDbQueryProvider(context).CreateQuery<TResult>(realExp);
    }
 
    public static IQueryable<TResult> SelectScalar<TElement, TResult>(this IEnumerable<TElement> q, Expression<Func<IQueryable<TElement>, TResult>> expression)
    {
        IQueryProvider queryProvider = (q as IQueryable).Provider;
 
        var expr = RefactorExpression(expression, (IQueryable<TElement>)q);
 
        var realExp = Expression.Call(
                method: GetMethodInfo(() => Queryable.Select<int, TResult>(null, (Expression<Func<int, TResult>>)null)),
                arg0: Expression.Call(
                    method: GetMethodInfo(() => Queryable.AsQueryable<int>(null)),
                    arg0: Expression.NewArrayInit(typeof(int), Expression.Constant(1))),
                arg1: Expression.Lambda(body: expr.Body, parameters: new[] { Expression.Parameter(typeof(int)) }));
 
        return queryProvider.CreateQuery<TResult>(realExp);
    }
 
    static MethodInfo GetMethodInfo(Expression<Action> expression)
    {
        return ((MethodCallExpression)expression.Body).Method;
    }
 
}




Here are examples, how to use this code:
    //You can use DbContext extension CreateScalarQuery
    var queryFromDbContext = myDbContext.CreateScalarQuery((c) => c.Assignments.Count())
          .Concat(myDbContext.CreateScalarQuery((c) => c.Assignments.Count()));
 
    var xrun = queryFromDbContext.ToArray();
          
 
    //Or you can use SelectScalar extension of IQueryable<T>
    var queryFromIQueryable = myDbContext.Assignments.SelectScalar(q => q.Count())
.Concat(myDbContext.Assignments.SelectScalar(q => q.Count()));
    xrun = queryFromIQueryable.ToArray();
SQL generated for each statement is the same:

SELECT 
[UnionAll1].[C1] AS [C1]
FROM  (SELECT 
 [GroupBy1].[A1] AS [C1]
 FROM ( SELECT 
  COUNT(1) AS [A1]
  FROM [dbo].[Assignments] AS [Extent1]
 )  AS [GroupBy1]
UNION ALL
 SELECT 
 [GroupBy2].[A1] AS [C1]
 FROM ( SELECT 
  COUNT(1) AS [A1]
  FROM [dbo].[Assignments] AS [Extent2]
 )  AS [GroupBy2]) AS [UnionAll1]






Thursday, September 26, 2013

T4 for generating composable functions for DbContext of Entity Framework 5 model

If you try to Import Function which is composable using EF designer you will see error message :
Function imports cannot be created for composable functions.

However you can see that all data for those functions are imported to Storage space of the model. So there is enough information to generate functions wrappers automatically using T4 Template.
The following T4 template is modified version of original EF5 Db Context T4 template - it only generates wrappers for composable functions code to your partial db context class. I didn't test it for different scenarios, so this is the raw version of T4 template:


<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
 output extension=".cs"#><#
 
const string inputFile = @"Model1.edmx";
var textTransformation = this;
var textTransform = DynamicTextTransformation.Create(textTransformation);
var code = new CodeGenerationTools(textTransformation);
var ef = new MetadataTools(textTransformation);
var typeMapper = new TypeMapper(code, ef, textTransform.Errors);
var loader = new EdmMetadataLoader(textTransform.Host, textTransform.Errors);
var itemCollection = loader.CreateEdmItemCollection(inputFile);
var modelNamespace = loader.GetModelNamespace(inputFile);
var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);
var container = itemCollection.OfType<EntityContainer>().FirstOrDefault();
 
 
var mdl = new MetadataLoader(textTransformation);
MetadataWorkspace ws = null;
mdl.TryLoadAllMetadata(inputFile,out ws);
var storeItems = ws.GetItemCollection(DataSpace.SSpace);
var functions = storeItems.OfType<EdmFunction>().Where(x=>x.NamespaceName!="SqlServer" && typeMapper.IsComposable(x)).ToArray();
#>
//------------------------------------------------------------------------------
// <auto-generated>
// <#=GetResourceString("Template_GeneratedCodeCommentLine1")#>
//
// <#=GetResourceString("Template_GeneratedCodeCommentLine2")#>
// <#=GetResourceString("Template_GeneratedCodeCommentLine3")#>
// </auto-generated>
//------------------------------------------------------------------------------

<#
 
var codeNamespace = code.VsNamespaceSuggestion();
if (!String.IsNullOrEmpty(codeNamespace))
{
#>
namespace <#=code.EscapeNamespace(codeNamespace)#>
{
<#
    PushIndent("    ");
}
 
#>
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
<#
if (container.FunctionImports.Any())
{
#>
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
<#
}
#>
<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : DbContext
{
 
 <#
    foreach (var edmFunction in functions)
    {
        WriteFunctionImport(typeMapper, codeStringGenerator, edmFunction, modelNamespace, includeMergeOption: false);
    }
#>
}
<#
 
if (!String.IsNullOrEmpty(codeNamespace))
{
    PopIndent();
#>
}
<#
}
#>
<#+
 
 
 
private void WriteFunctionImport(TypeMapper typeMapper, CodeStringGenerator codeStringGenerator, EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
{
#>
 
    [EdmFunction("<#=edmFunction.NamespaceName#>""<#=edmFunction.Name#>")]
    <#=codeStringGenerator.ComposableStoreFunctionMethod(edmFunction, modelNamespace)#>
    {
<#+
        codeStringGenerator.WriteStoreFunctionParameters(edmFunction, WriteFunctionParameter);
#>
        <#=codeStringGenerator.ComposableCreateStoreQuery(edmFunction, modelNamespace)#>
    }
<#+
 
}
 
 
 
public void WriteFunctionParameter(string name, string isNotNull, string notNullInit, string nullInit)
{
#>
        var <#=name#> = <#=isNotNull#> ?
            <#=notNullInit#> :
            <#=nullInit#>;
 
<#+
}
 
public const string TemplateId = "CSharp_DbContext_Context_EF5";
 
public class CodeStringGenerator
{
 
 public string ComposableCreateStoreQuery(EdmFunction edmFunction, string modelNamespace)
    {
        var parameters = _typeMapper.GetParameters(edmFunction);
 
        return string.Format(
            CultureInfo.InvariantCulture,
            "return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<{0}>(\"select [{1}].[{2}]({3})\"{4});",
            _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace),
            //edmFunction.NamespaceName,
            edmFunction.MetadataProperties["Schema"].Value,
            edmFunction.Name,
            string.Join(", ", parameters.Select(p => "@" + p.EsqlParameterName).ToArray()),
            _code.StringBefore(", "string.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray())));
    }
 
    public void WriteStoreFunctionParameters(EdmFunction edmFunction, Action<stringstringstringstring> writeParameter)
    {
        var parameters = FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef);
        foreach (var parameter in parameters.Where(p => p.NeedsLocalVariable))
        {
            var isNotNull = parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null";
            var notNullInit = "new SqlParameter(\"" + parameter.EsqlParameterName + "\", " + parameter.FunctionParameterName + ")";
            var nullInit = "new SqlParameter(\"" + parameter.EsqlParameterName + "\", typeof(" + parameter.RawClrTypeName + "))";
            writeParameter(parameter.LocalVariableName, isNotNull, notNullInit, nullInit);
        }
    }
 
 
    public string ComposableStoreFunctionMethod(EdmFunction edmFunction, string modelNamespace)
    {
        var parameters = _typeMapper.GetParameters(edmFunction);
        
        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} ObjectResult<{1}> {2}({3})",
            AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)),
            _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace),
            _code.Escape(edmFunction),
            string.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray()));
    }
 
 
    private readonly CodeGenerationTools _code;
    private readonly TypeMapper _typeMapper;
    private readonly MetadataTools _ef;
 
    public CodeStringGenerator(CodeGenerationTools code, TypeMapper typeMapper, MetadataTools ef)
    {
        ArgumentNotNull(code, "code");
        ArgumentNotNull(typeMapper, "typeMapper");
        ArgumentNotNull(ef, "ef");
 
        _code = code;
        _typeMapper = typeMapper;
        _ef = ef;
    }
 
    public string Property(EdmProperty edmProperty)
    {
        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1} {2} {{ {3}get; {4}set; }}",
            Accessibility.ForProperty(edmProperty),
            _typeMapper.GetTypeName(edmProperty.TypeUsage),
            _code.Escape(edmProperty),
            _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
            _code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
    }
 
    public string NavigationProperty(NavigationProperty navigationProperty)
    {
        var endType = _typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType());
        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1} {2} {{ {3}get; {4}set; }}",
            AccessibilityAndVirtual(Accessibility.ForProperty(navigationProperty)),
            navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType,
            _code.Escape(navigationProperty),
            _code.SpaceAfter(Accessibility.ForGetter(navigationProperty)),
            _code.SpaceAfter(Accessibility.ForSetter(navigationProperty)));
    }
    
    public string AccessibilityAndVirtual(string accessibility)
    {
        return accessibility + (accessibility != "private" ? " virtual" : "");
    }
    
    public string EntityClassOpening(EntityType entity)
    {
        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1}partial class {2}{3}",
            Accessibility.ForType(entity),
            _code.SpaceAfter(_code.AbstractOption(entity)),
            _code.Escape(entity),
            _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
    }
    
    public string EnumOpening(SimpleType enumType)
    {
        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} enum {1} : {2}",
            Accessibility.ForType(enumType),
            _code.Escape(enumType),
            _code.Escape(_typeMapper.UnderlyingClrType(enumType)));
        }
    
    public void WriteFunctionParameters(EdmFunction edmFunction, Action<stringstringstringstring> writeParameter)
    {
        var parameters = FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef);
        foreach (var parameter in parameters.Where(p => p.NeedsLocalVariable))
        {
            var isNotNull = parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null";
            var notNullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", " + parameter.FunctionParameterName + ")";
            var nullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", typeof(" + parameter.RawClrTypeName + "))";
            writeParameter(parameter.LocalVariableName, isNotNull, notNullInit, nullInit);
        }
    }
    
    public string ComposableFunctionMethod(EdmFunction edmFunction, string modelNamespace)
    {
        var parameters = _typeMapper.GetParameters(edmFunction);
        
        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} IQueryable<{1}> {2}({3})",
            AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)),
            _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace),
            _code.Escape(edmFunction),
            string.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray()));
    }
    
    public string ComposableCreateQuery(EdmFunction edmFunction, string modelNamespace)
    {
        var parameters = _typeMapper.GetParameters(edmFunction);
        
        return string.Format(
            CultureInfo.InvariantCulture,
            "return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<{0}>(\"[{1}].[{2}]({3})\"{4});",
            _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace),
            edmFunction.NamespaceName,
            edmFunction.Name,
            string.Join(", ", parameters.Select(p => "@" + p.EsqlParameterName).ToArray()),
            _code.StringBefore(", "string.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray())));
    }
    
    public string FunctionMethod(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
    {
        var parameters = _typeMapper.GetParameters(edmFunction);
        var returnType = _typeMapper.GetReturnType(edmFunction);
 
        var paramList = String.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray());
        if (includeMergeOption)
        {
            paramList = _code.StringAfter(paramList, ", ") + "MergeOption mergeOption";
        }
 
        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1} {2}({3})",
            AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)),
            returnType == null ? "int" : "ObjectResult<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">",
            _code.Escape(edmFunction),
            paramList);
    }
    
    public string ExecuteFunction(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
    {
        var parameters = _typeMapper.GetParameters(edmFunction);
        var returnType = _typeMapper.GetReturnType(edmFunction);
 
        var callParams = _code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()));
        if (includeMergeOption)
        {
            callParams = ", mergeOption" + callParams;
        }
        
        return string.Format(
            CultureInfo.InvariantCulture,
            "return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction{0}(\"{1}\"{2});",
            returnType == null ? "" : "<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">",
            edmFunction.Name,
            callParams);
    }
    
    public string DbSet(EntitySet entitySet)
    {
        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} DbSet<{1}> {2} {{ get; set; }}",
            Accessibility.ForReadOnlyProperty(entitySet),
            _typeMapper.GetTypeName(entitySet.ElementType),
            _code.Escape(entitySet));
    }
 
    public string UsingDirectives(bool inHeader, bool includeCollections = true)
    {
        return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
            ? string.Format(
                CultureInfo.InvariantCulture,
                "{0}using System;{1}" +
                "{2}",
                inHeader ? Environment.NewLine : "",
                includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
                inHeader ? "" : Environment.NewLine)
            : "";
    }
}
 
public class TypeMapper
{
    private const string ExternalTypeNameAttributeName = @"http://schemas.microsoft.com/ado/2006/04/codegeneration:ExternalTypeName";
 
    private readonly System.Collections.IList _errors;
    private readonly CodeGenerationTools _code;
    private readonly MetadataTools _ef;
 
    public TypeMapper(CodeGenerationTools code, MetadataTools ef, System.Collections.IList errors)
    {
        ArgumentNotNull(code, "code");
        ArgumentNotNull(ef, "ef");
        ArgumentNotNull(errors, "errors");
 
        _code = code;
        _ef = ef;
        _errors = errors;
    }
 
    public string GetTypeName(TypeUsage typeUsage)
    {
        return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace: null);
    }
 
    public string GetTypeName(EdmType edmType)
    {
        return GetTypeName(edmType, isNullable: null, modelNamespace: null);
    }
 
    public string GetTypeName(TypeUsage typeUsage, string modelNamespace)
    {
        return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace);
    }
 
    public string GetTypeName(EdmType edmType, string modelNamespace)
    {
        return GetTypeName(edmType, isNullable: null, modelNamespace: modelNamespace);
    }
 
    public string GetTypeName(EdmType edmType, bool? isNullable, string modelNamespace)
    {
        if (edmType == null)
        {
            return null;
        }
 
        var collectionType = edmType as CollectionType;
        if (collectionType != null)
        {
            return String.Format(CultureInfo.InvariantCulture, "ICollection<{0}>", GetTypeName(collectionType.TypeUsage, modelNamespace));
        }
 
        var typeName = _code.Escape(edmType.MetadataProperties
                                .Where(p => p.Name == ExternalTypeNameAttributeName)
                                .Select(p => (string)p.Value)
                                .FirstOrDefault())
            ?? (modelNamespace != null && edmType.NamespaceName != modelNamespace ?
                _code.CreateFullName(_code.EscapeNamespace(edmType.NamespaceName), _code.Escape(edmType)) :
                _code.Escape(edmType));
 
        if (edmType is StructuralType)
        {
            return typeName;
        }
 
        if (edmType is SimpleType)
        {
            var clrType = UnderlyingClrType(edmType);
            if (!IsEnumType(edmType))
            {
                typeName = _code.Escape(clrType);
            }
 
            return clrType.IsValueType && isNullable == true ?
                String.Format(CultureInfo.InvariantCulture, "Nullable<{0}>", typeName) :
                typeName;
        }
 
        throw new ArgumentException("edmType");
    }
    
    public Type UnderlyingClrType(EdmType edmType)
    {
        ArgumentNotNull(edmType, "edmType");
 
        var primitiveType = edmType as PrimitiveType;
        if (primitiveType != null)
        {
            return primitiveType.ClrEquivalentType;
        }
 
        if (IsEnumType(edmType))
        {
            return GetEnumUnderlyingType(edmType).ClrEquivalentType;
        }
 
        return typeof(object);
    }
    
    public object GetEnumMemberValue(MetadataItem enumMember)
    {
        ArgumentNotNull(enumMember, "enumMember");
        
        var valueProperty = enumMember.GetType().GetProperty("Value");
        return valueProperty == null ? null : valueProperty.GetValue(enumMember, null);
    }
    
    public string GetEnumMemberName(MetadataItem enumMember)
    {
        ArgumentNotNull(enumMember, "enumMember");
        
        var nameProperty = enumMember.GetType().GetProperty("Name");
        return nameProperty == null ? null : (string)nameProperty.GetValue(enumMember, null);
    }
 
    public System.Collections.IEnumerable GetEnumMembers(EdmType enumType)
    {
        ArgumentNotNull(enumType, "enumType");
 
        var membersProperty = enumType.GetType().GetProperty("Members");
        return membersProperty != null 
            ? (System.Collections.IEnumerable)membersProperty.GetValue(enumType, null)
            : Enumerable.Empty<MetadataItem>();
    }
    
    public bool EnumIsFlags(EdmType enumType)
    {
        ArgumentNotNull(enumType, "enumType");
        
        var isFlagsProperty = enumType.GetType().GetProperty("IsFlags");
        return isFlagsProperty != null && (bool)isFlagsProperty.GetValue(enumType, null);
    }
 
    public bool IsEnumType(GlobalItem edmType)
    {
        ArgumentNotNull(edmType, "edmType");
 
        return edmType.GetType().Name == "EnumType";
    }
 
    public PrimitiveType GetEnumUnderlyingType(EdmType enumType)
    {
        ArgumentNotNull(enumType, "enumType");
 
        return (PrimitiveType)enumType.GetType().GetProperty("UnderlyingType").GetValue(enumType, null);
    }
 
    public string CreateLiteral(object value)
    {
        if (value == null || value.GetType() != typeof(TimeSpan))
        {
            return _code.CreateLiteral(value);
        }
 
        return string.Format(CultureInfo.InvariantCulture, "new TimeSpan({0})", ((TimeSpan)value).Ticks);
    }
    
    public bool VerifyCaseInsensitiveTypeUniqueness(IEnumerable<string> types, string sourceFile)
    {
        ArgumentNotNull(types, "types");
        ArgumentNotNull(sourceFile, "sourceFile");
        
        var hash = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
        if (types.Any(item => !hash.Add(item)))
        {
            _errors.Add(
                new CompilerError(sourceFile, -1, -1, "6023",
                    String.Format(CultureInfo.CurrentCulture, GetResourceString("Template_CaseInsensitiveTypeConflict"))));
            return false;
        }
        return true;
    }
    
    public IEnumerable<SimpleType> GetEnumItemsToGenerate(IEnumerable<GlobalItem> itemCollection)
    {
        return GetItemsToGenerate<SimpleType>(itemCollection)
            .Where(e => IsEnumType(e));
    }
    
    public IEnumerable<T> GetItemsToGenerate<T>(IEnumerable<GlobalItem> itemCollection) where T: EdmType
    {
        return itemCollection
            .OfType<T>()
            .Where(i => !i.MetadataProperties.Any(p => p.Name == ExternalTypeNameAttributeName))
            .OrderBy(i => i.Name);
    }
 
    public IEnumerable<string> GetAllGlobalItems(IEnumerable<GlobalItem> itemCollection)
    {
        return itemCollection
            .Where(i => i is EntityType || i is ComplexType || i is EntityContainer || IsEnumType(i))
            .Select(g => GetGlobalItemName(g));
    }
 
    public string GetGlobalItemName(GlobalItem item)
    {
        if (item is EdmType)
        {
            return ((EdmType)item).Name;
        }
        else
        {
            return ((EntityContainer)item).Name;
        }
    }
 
    public IEnumerable<EdmProperty> GetSimpleProperties(EntityType type)
    {
        return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type);
    }
    
    public IEnumerable<EdmProperty> GetSimpleProperties(ComplexType type)
    {
        return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type);
    }
    
    public IEnumerable<EdmProperty> GetComplexProperties(EntityType type)
    {
        return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type);
    }
    
    public IEnumerable<EdmProperty> GetComplexProperties(ComplexType type)
    {
        return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type);
    }
 
    public IEnumerable<EdmProperty> GetPropertiesWithDefaultValues(EntityType type)
    {
        return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null);
    }
    
    public IEnumerable<EdmProperty> GetPropertiesWithDefaultValues(ComplexType type)
    {
        return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null);
    }
 
    public IEnumerable<NavigationProperty> GetNavigationProperties(EntityType type)
    {
        return type.NavigationProperties.Where(np => np.DeclaringType == type);
    }
    
    public IEnumerable<NavigationProperty> GetCollectionNavigationProperties(EntityType type)
    {
        return type.NavigationProperties.Where(np => np.DeclaringType == type && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many);
    }
    
    public FunctionParameter GetReturnParameter(EdmFunction edmFunction)
    {
        ArgumentNotNull(edmFunction, "edmFunction");
 
        var returnParamsProperty = edmFunction.GetType().GetProperty("ReturnParameters");
        return returnParamsProperty == null
            ? edmFunction.ReturnParameter
            : ((IEnumerable<FunctionParameter>)returnParamsProperty.GetValue(edmFunction, null)).FirstOrDefault();
    }
 
    public bool IsComposable(EdmFunction edmFunction)
    {
        ArgumentNotNull(edmFunction, "edmFunction");
 
        var isComposableProperty = edmFunction.GetType().GetProperty("IsComposableAttribute");
        return isComposableProperty != null && (bool)isComposableProperty.GetValue(edmFunction, null);
    }
 
    public IEnumerable<FunctionImportParameter> GetParameters(EdmFunction edmFunction)
    {
        return FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef);
    }
 
    public TypeUsage GetReturnType(EdmFunction edmFunction)
    {
        var returnParam = GetReturnParameter(edmFunction);
        return returnParam == null ? null : _ef.GetElementType(returnParam.TypeUsage);
    }
    
    public bool GenerateMergeOptionFunction(EdmFunction edmFunction, bool includeMergeOption)
    {
        var returnType = GetReturnType(edmFunction);
        return !includeMergeOption && returnType != null && returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType;
    }
}
 
public class EdmMetadataLoader
{
    private readonly IDynamicHost _host;
    private readonly System.Collections.IList _errors;
 
    public EdmMetadataLoader(IDynamicHost host, System.Collections.IList errors)
    {
        ArgumentNotNull(host, "host");
        ArgumentNotNull(errors, "errors");
 
        _host = host;
        _errors = errors;
    }
 
    public IEnumerable<GlobalItem> CreateEdmItemCollection(string sourcePath)
    {
        ArgumentNotNull(sourcePath, "sourcePath");
 
        if (!ValidateInputPath(sourcePath))
        {
            return new EdmItemCollection();
        }
 
        var schemaElement = LoadRootElement(_host.ResolvePath(sourcePath));
        if (schemaElement != null)
        {
            using (var reader = schemaElement.CreateReader())
            {
                IList<EdmSchemaError> errors;
                var itemCollection = MetadataItemCollectionFactory.CreateEdmItemCollection(new[] { reader }, out errors);
 
                ProcessErrors(errors, sourcePath);
 
                return itemCollection;
            }
        }
        return new EdmItemCollection();
    }
 
    public string GetModelNamespace(string sourcePath)
    {
        ArgumentNotNull(sourcePath, "sourcePath");
 
        if (!ValidateInputPath(sourcePath))
        {
            return string.Empty;
        }
 
        var model = LoadRootElement(_host.ResolvePath(sourcePath));
        if (model == null)
        {
            return string.Empty;
        }
 
        var attribute = model.Attribute("Namespace");
        return attribute != null ? attribute.Value : "";
    }
 
    private bool ValidateInputPath(string sourcePath)
    {
        if (sourcePath == "$" + "edmxInputFile" + "$")
        {
            _errors.Add(
                new CompilerError(_host.TemplateFile ?? sourcePath, 0, 0, string.Empty,
                    GetResourceString("Template_ReplaceVsItemTemplateToken")));
            return false;
        }
 
        return true;
    }
 
    public XElement LoadRootElement(string sourcePath)
    {
        ArgumentNotNull(sourcePath, "sourcePath");
 
        var root = XElement.Load(sourcePath, LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
        return root.Elements()
            .Where(e => e.Name.LocalName == "Runtime")
            .Elements()
            .Where(e => e.Name.LocalName == "ConceptualModels")
            .Elements()
            .Where(e => e.Name.LocalName == "Schema")
            .FirstOrDefault()
                ?? root;
    }
 
    private void ProcessErrors(IEnumerable<EdmSchemaError> errors, string sourceFilePath)
    {
        foreach (var error in errors)
        {
            _errors.Add(
                new CompilerError(
                    error.SchemaLocation ?? sourceFilePath,
                    error.Line,
                    error.Column,
                    error.ErrorCode.ToString(CultureInfo.InvariantCulture),
                    error.Message)
                {
                    IsWarning = error.Severity == EdmSchemaErrorSeverity.Warning
                });
        }
    }
    
    public bool IsLazyLoadingEnabled(EntityContainer container)
    {
        string lazyLoadingAttributeValue;
        var lazyLoadingAttributeName = MetadataConstants.EDM_ANNOTATION_09_02 + ":LazyLoadingEnabled";
        bool isLazyLoading;
        return !MetadataTools.TryGetStringMetadataPropertySetting(container, lazyLoadingAttributeName, out lazyLoadingAttributeValue)
            || !bool.TryParse(lazyLoadingAttributeValue, out isLazyLoading)
            || isLazyLoading;
    }
}
 
public static void ArgumentNotNull<T>(T arg, string name) where T : class
{
    if (arg == null)
    {
        throw new ArgumentNullException(name);
    }
}
    
private static readonly Lazy<System.Resources.ResourceManager> ResourceManager =
    new Lazy<System.Resources.ResourceManager>(
        () => new System.Resources.ResourceManager("System.Data.Entity.Design"typeof(MetadataItemCollectionFactory).Assembly), isThreadSafe: true);
 
public static string GetResourceString(string resourceName)
{
    ArgumentNotNull(resourceName, "resourceName");
 
    return ResourceManager.Value.GetString(resourceName, null);
}
 
#>