From 357f2bbdfb5080212960246ba1a78e47f90bbec4 Mon Sep 17 00:00:00 2001 From: Andrew Veresov Date: Wed, 26 Mar 2014 04:58:04 +0400 Subject: [PATCH 1/3] Added DisplayAttribute and DisplayNameAttribute parsing and exposing displayName metadata property out of parsed attributes --- .../EFContextProvider.cs | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/Breeze.ContextProvider.EF6/EFContextProvider.cs b/Breeze.ContextProvider.EF6/EFContextProvider.cs index bdb81f711..a7b125c30 100644 --- a/Breeze.ContextProvider.EF6/EFContextProvider.cs +++ b/Breeze.ContextProvider.EF6/EFContextProvider.cs @@ -20,6 +20,7 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Runtime.Remoting.Messaging; using System.Xml; using System.Xml.Linq; using Newtonsoft.Json; @@ -531,6 +532,7 @@ public static String GetMetadataFromDbFirstAssembly(Assembly assembly, String re // This is needed because the raw edmx has a different namespace than the CLR types that it references. xDoc = UpdateCSpaceOSpaceMapping(xDoc, assembly, resourcePrefix); + AddUpdateDisplayNames(xDoc, assembly); return XDocToJson(xDoc); } @@ -570,10 +572,47 @@ private static String GetMetadataFromDbContext(Object context) { var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext; // This is needed because the raw edmx has a different namespace than the CLR types that it references. xDoc = UpdateCSpaceOSpaceMapping(xDoc, objectContext); + AddUpdateDisplayNames(xDoc, dbContext.GetType().Assembly); return XDocToJson(xDoc); } - private static String GetMetadataFromObjectContext(Object context) { + private static void AddUpdateDisplayNames(XDocument xDoc, Assembly assembly) + { + XNamespace ns = "http://schemas.microsoft.com/ado/2009/11/edm"; + + XNamespace annotationNs = xDoc.Root.Attribute(XNamespace.Xmlns + "customannotation").Value; + foreach (var entityType in xDoc.Root.Elements(ns + "EntityType")) + { + //string className = entityType.Attribute("Name").Value; + //string typeName = string.Format("{0}.{1}, {2}", nameSpace, className, dbContext.GetType().Assembly.FullName); + //string typeName = string.Format("{0}.{1}", nameSpace, className); + + //var ass = dbContext.GetType().Assembly; + //Type type = Type.GetType(nameSpace + "." + className, assemblyName => dbContext.GetType().Assembly, (assembly, tn, _) => ass.GetType(typeName)); + var typeName = entityType.Attribute(annotationNs + "ClrType").Value; + typeName = typeName.Substring(0, typeName.IndexOf(',')); + Type type = assembly.GetType(typeName); + foreach (var property in entityType.Elements(ns + "Property")) + { + var typeProperty = type.GetProperty(property.Attribute("Name").Value); + var displayAttribute = (DisplayAttribute)Attribute.GetCustomAttribute(typeProperty, typeof(DisplayAttribute)); + if (displayAttribute != null && displayAttribute.Name != null) + { + property.SetAttributeValue("DisplayName", displayAttribute.Name); + } + else + { + var displayNameAttribute = (DisplayNameAttribute)Attribute.GetCustomAttribute(typeProperty, typeof(DisplayNameAttribute)); + if (displayNameAttribute != null && displayNameAttribute.DisplayName != null) + { + property.SetAttributeValue("DisplayName", displayNameAttribute.DisplayName); + } + } + } + } + } + + private static String GetMetadataFromObjectContext(Object context) { var ocAssembly = context.GetType().Assembly; var ocNamespace = context.GetType().Namespace; @@ -584,6 +623,8 @@ private static String GetMetadataFromObjectContext(Object context) { // This is needed because the raw edmx has a different namespace than the CLR types that it references. xDoc = UpdateCSpaceOSpaceMapping(xDoc, objectContext); + + AddUpdateDisplayNames(xDoc, context.GetType().Assembly); return XDocToJson(xDoc); } From 151115e063ebb4aeff1289047e4bb0253d5c62f9 Mon Sep 17 00:00:00 2001 From: Andrew Veresov Date: Wed, 26 Mar 2014 05:33:44 +0400 Subject: [PATCH 2/3] Set entityType displayName property to displayName metadata property value if present --- Breeze.Client/Scripts/IBlade/a40_entityMetadata.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Breeze.Client/Scripts/IBlade/a40_entityMetadata.js b/Breeze.Client/Scripts/IBlade/a40_entityMetadata.js index f53946419..e8fb62eaf 100644 --- a/Breeze.Client/Scripts/IBlade/a40_entityMetadata.js +++ b/Breeze.Client/Scripts/IBlade/a40_entityMetadata.js @@ -898,6 +898,9 @@ var CsdlMetadataParser = (function () { } } if (dp) { + if (csdlProperty.displayName != undefined) { + dp.displayName = csdlProperty.displayName; + } parentType.addProperty(dp); addValidators(dp); } From d30a0e2daef87d9ecae861f5a96275c19642b3e7 Mon Sep 17 00:00:00 2001 From: Andrew Veresov Date: Wed, 26 Mar 2014 13:26:18 +0400 Subject: [PATCH 3/3] Removed unused namespace --- Breeze.ContextProvider.EF6/EFContextProvider.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Breeze.ContextProvider.EF6/EFContextProvider.cs b/Breeze.ContextProvider.EF6/EFContextProvider.cs index a7b125c30..2684055df 100644 --- a/Breeze.ContextProvider.EF6/EFContextProvider.cs +++ b/Breeze.ContextProvider.EF6/EFContextProvider.cs @@ -20,7 +20,6 @@ using System.IO; using System.Linq; using System.Reflection; -using System.Runtime.Remoting.Messaging; using System.Xml; using System.Xml.Linq; using Newtonsoft.Json;