Source Code for DataAnnotationsModelMetadataProvider - asp.net-mvc-2

For some reason, .NET Reflector throws an exception when I try to reflect on this class. It works for everything else.
What is the source code of DataAnnotationsModelMetadataProvider, please?

Feel free to download the ASP.NET MVC source code and inspect the implementation. Here's how it looks in ASP.NET MVC 3 RTM:
namespace System.Web.Mvc {
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
public class DataAnnotationsModelMetadataProvider : AssociatedMetadataProvider {
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName) {
List<Attribute> attributeList = new List<Attribute>(attributes);
DisplayColumnAttribute displayColumnAttribute = attributeList.OfType<DisplayColumnAttribute>().FirstOrDefault();
DataAnnotationsModelMetadata result = new DataAnnotationsModelMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);
// Do [HiddenInput] before [UIHint], so you can override the template hint
HiddenInputAttribute hiddenInputAttribute = attributeList.OfType<HiddenInputAttribute>().FirstOrDefault();
if (hiddenInputAttribute != null) {
result.TemplateHint = "HiddenInput";
result.HideSurroundingHtml = !hiddenInputAttribute.DisplayValue;
}
// We prefer [UIHint("...", PresentationLayer = "MVC")] but will fall back to [UIHint("...")]
IEnumerable<UIHintAttribute> uiHintAttributes = attributeList.OfType<UIHintAttribute>();
UIHintAttribute uiHintAttribute = uiHintAttributes.FirstOrDefault(a => String.Equals(a.PresentationLayer, "MVC", StringComparison.OrdinalIgnoreCase))
?? uiHintAttributes.FirstOrDefault(a => String.IsNullOrEmpty(a.PresentationLayer));
if (uiHintAttribute != null) {
result.TemplateHint = uiHintAttribute.UIHint;
}
DataTypeAttribute dataTypeAttribute = attributeList.OfType<DataTypeAttribute>().FirstOrDefault();
if (dataTypeAttribute != null) {
result.DataTypeName = dataTypeAttribute.ToDataTypeName();
}
EditableAttribute editable = attributes.OfType<EditableAttribute>().FirstOrDefault();
if (editable != null) {
result.IsReadOnly = !editable.AllowEdit;
}
else {
ReadOnlyAttribute readOnlyAttribute = attributeList.OfType<ReadOnlyAttribute>().FirstOrDefault();
if (readOnlyAttribute != null) {
result.IsReadOnly = readOnlyAttribute.IsReadOnly;
}
}
DisplayFormatAttribute displayFormatAttribute = attributeList.OfType<DisplayFormatAttribute>().FirstOrDefault();
if (displayFormatAttribute == null && dataTypeAttribute != null) {
displayFormatAttribute = dataTypeAttribute.DisplayFormat;
}
if (displayFormatAttribute != null) {
result.NullDisplayText = displayFormatAttribute.NullDisplayText;
result.DisplayFormatString = displayFormatAttribute.DataFormatString;
result.ConvertEmptyStringToNull = displayFormatAttribute.ConvertEmptyStringToNull;
if (displayFormatAttribute.ApplyFormatInEditMode) {
result.EditFormatString = displayFormatAttribute.DataFormatString;
}
if (!displayFormatAttribute.HtmlEncode && String.IsNullOrWhiteSpace(result.DataTypeName)) {
result.DataTypeName = DataTypeUtil.HtmlTypeName;
}
}
ScaffoldColumnAttribute scaffoldColumnAttribute = attributeList.OfType<ScaffoldColumnAttribute>().FirstOrDefault();
if (scaffoldColumnAttribute != null) {
result.ShowForDisplay = result.ShowForEdit = scaffoldColumnAttribute.Scaffold;
}
DisplayAttribute display = attributes.OfType<DisplayAttribute>().FirstOrDefault();
string name = null;
if (display != null) {
result.Description = display.GetDescription();
result.ShortDisplayName = display.GetShortName();
result.Watermark = display.GetPrompt();
result.Order = display.GetOrder() ?? ModelMetadata.DefaultOrder;
name = display.GetName();
}
if (name != null) {
result.DisplayName = name;
}
else {
DisplayNameAttribute displayNameAttribute = attributeList.OfType<DisplayNameAttribute>().FirstOrDefault();
if (displayNameAttribute != null) {
result.DisplayName = displayNameAttribute.DisplayName;
}
}
RequiredAttribute requiredAttribute = attributeList.OfType<RequiredAttribute>().FirstOrDefault();
if (requiredAttribute != null) {
result.IsRequired = true;
}
return result;
}
}
}

Related

List Null after mapping the data Flutter

This Is my coding for mapping data and filter it by subs class:
Hi i have a problem where is my list is null after it mapping the data so can you help me for this solution
void onUserJobListData(Event e) {
Map data = Map.from(e.snapshot.value);
data?.forEach((a, b) {
print("key " + a);
// int i = _jobs.indexWhere((j) => j.jobId == a);
int i = _jobs == null ? 0 : _jobs.indexWhere((j) => j.jobId == a);
if (i >= 0) {
if (b['notification_date'] != null) {
_jobs[i].matchDetail.notificationDate = b['notification_date'];
}
if (b['status_date'] != null) {
_jobs[i].matchDetail.statusDate = b['status_date'];
}
if (b['event_status'] != null) {
_jobs[i].matchDetail.eventStatus = b['event_status'];
}
if (b['notification_status'] != null) {
_jobs[i].matchDetail.notificationStatus = b['notification_status'];
}
if (b['status'] != null) {
_jobs[i].matchDetail.status = b['status'];
}
}
});
print("Apa apa jerr");
jobState.setJobList(_jobs, JobState.USER_JOB);
}`

EF Core 3.x: duplicate value

I have the following code:
public class DeviceVerizon
{
public int DeviceId { get; set; }
public string InternalDeviceId { get; set; }
}
and
public class DeviceVerizonMap : IEntityTypeConfiguration<DeviceVerizon>
{
public void Configure(EntityTypeBuilder<DeviceVerizon> builder)
{
builder.ToTable(nameof(DeviceVerizon));
builder.HasKey(d => d.DeviceId);
builder.HasIndex(v => v.InternalDeviceId).IsUnique();
builder.HasOne(o => o.Device)
.WithOne(o => o.VerizonData)
.HasForeignKey<DeviceVerizon>(a => a.DeviceId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired()
;
}
}
so, InternalDeviceId is just unique index.
some code change InternalDeviceId for existing record and add new record with the previous InternalDeviceId.
So, localDevice.VerizonData.InternalDeviceId = 1420531689
I update it for new (remoteDevice.VerizonData.InternalDeviceId = '111111111')
localDevice.VerizonData = remoteDevice.VerizonData;
_context.Devices.Update(localDevice);
then add new record with remoteDevice.VerizonData.InternalDeviceId = 1420531689:
_context.Devices.Add(remoteDevice);
and after it I call:
await _context.SaveChangesAsync();
but I get an exception:
Cannot insert duplicate key row in object 'dbo.DeviceVerizon' with
unique index 'IX_DeviceVerizon_InternalDeviceId'. The duplicate key
value is (1420531689).
The full code:
foreach (var remoteDevice in remoteVerizonDevices)
{
var localDevice = allLocalDevicesWithIMEI.Where(a =>
(string.IsNullOrWhiteSpace(remoteDevice.IMEI)
&& a.VerizonData != null
&& a.VerizonData.InternalDeviceId == remoteDevice.VerizonData.InternalDeviceId
&& a.IMSI == remoteDevice.IMSI && a.ICCID == remoteDevice.ICCID)
|| a.IMEI == remoteDevice.IMEI).FirstOrDefault();
if (localDevice != null) // device with such imei already exists or IMEI is empty on Verizon, but InternalDeviceId is the same and ICCID with IMSI are the same
{
var verizonStatus = remoteDevice.VerizonData.State.ConvertToDeviceStatusFromVerizonStatus();
var existingRequest = _context.VerizonRequests.Where(a => a.DeviceId == localDevice.Id && a.Result == VerizonRequestResult.Pending).FirstOrDefault();
if (verizonStatus == DeviceStatus.Active && remoteDevice.IsVerizonLastActiveMoreThanDays(60))
{
// existing request for this device does not exist, create new
if (existingRequest == null)
{
localDevice.Status = DeviceStatus.PendingSuspend;
localDevice.DateStatusChanging = DateTime.UtcNow;
localDevice.LastStatusChangeSource = DeviceStatusChangeSource.Verizon;
// create verizon request to change status, because device is offline more than 60 days
verizonSuspensionList.Add(localDevice.Id);
}
}
else
{
// Is Verizon status of device is different than locally?
if (localDevice.Status != verizonStatus)
{
// check whether we have active verizon request, if not, then
if (existingRequest == null)
{
// device is suspended on suremdm, we should suspend on Verizon
if (localDevice.Status == DeviceStatus.Suspended
&& verizonStatus == DeviceStatus.Active)
{
if (localDevice.Tablet != null && localDevice.Tablet.TabletGroup.GroupName == SpecialSureMdmGroups.Suspended)
verizonSuspensionList.Add(localDevice.Id);
}
else if (localDevice.Status != DeviceStatus.Suspended && verizonStatus == DeviceStatus.Suspended)
{
// if device is suspended on verizon, we need to suspend on SureMdm
localDevice.Status = DeviceStatus.PendingSuspend;
localDevice.DateStatusChanging = DateTime.UtcNow;
localDevice.LastStatusChangeSource = DeviceStatusChangeSource.Verizon;
if (localDevice.Tablet != null)
{
localDevice.Tablet.SuspensionDate = DateTime.UtcNow;
sureMdmSuspensionList.Add(new Core.Dto.DeviceManagement.SureMdm.SureMdmMoveDeviceToSuspendedGroupDto
{
TabletId = localDevice.Tablet.TabletId,
CurrentGroupId = localDevice.Tablet.TabletGroupId
});
}
}
else
{
// other cases that device on verizon has different status, that locally
VerizonStatusIsDifferentEvent?.Invoke(
remoteDevice: remoteDevice,
verizonStatus: remoteDevice.VerizonData.State.ConvertToDeviceStatusFromVerizonStatus(),
localStatus: localDevice.Status);
}
}
}
}
var changes = DeviceIsChanged(remoteDevice, localDevice);
if (changes != null && changes.Count > 0)
modifiedDevices.Add((localDevice, changes));
var continueToUpdate = true;
// if verizon data does not exist, add it
if (localDevice.VerizonData == null)
{
var existingInternalVerizonDeviceIdRecord = allDeviceVerizons.Where(a => a.InternalDeviceId == remoteDevice.VerizonData.InternalDeviceId).FirstOrDefault();
if (existingInternalVerizonDeviceIdRecord != null)
{
DeviceWithTheSameVerizonDeviceIdAlreadyExistsEvent?.Invoke(remoteDevice: remoteDevice,
localDevice: existingInternalVerizonDeviceIdRecord.Device,
verizonDeviceId: remoteDevice.VerizonData.InternalDeviceId);
continueToUpdate = false;
}
}
if (continueToUpdate)
{
localDevice.VerizonData = remoteDevice.VerizonData;
if (!string.IsNullOrWhiteSpace(remoteDevice.ICCID))
localDevice.ICCID = remoteDevice.ICCID;
localDevice.EID = remoteDevice.EID;
localDevice.ESN = remoteDevice.ESN;
if (!string.IsNullOrWhiteSpace(remoteDevice.IMSI))
localDevice.IMSI = remoteDevice.IMSI;
if (!string.IsNullOrWhiteSpace(remoteDevice.MDN))
localDevice.MDN = remoteDevice.MDN;
localDevice.MEID = remoteDevice.MEID;
localDevice.MIN = remoteDevice.MIN;
localDevice.MSISDN = remoteDevice.MSISDN;
localDevice.SKU = remoteDevice.SKU;
localDevice.SyncVerizon = true;
if (string.IsNullOrWhiteSpace(localDevice.Name))
localDevice.Name = localDevice.IMEI;
if (!localDevice.DeviceModelId.HasValue)
{
var verizonGroup = allVerizonGroups.Where(a => a.VerizonGroupName == remoteDevice.VerizonData.GroupName).FirstOrDefault();
if (!verizonGroup.DeviceModelId.HasValue)
VerizonGroupIsNotLinkedWithDeviceModelEvent?.Invoke(remoteDevice);
else
localDevice.DeviceModelId = verizonGroup.DeviceModelId;
}
_context.Devices.Update(localDevice);
}
}
else
{
// validate by Internal Verizon DeviceId
var existingInternalVerizonDeviceIdRecord = allDeviceVerizons.Where(a => a.InternalDeviceId == remoteDevice.VerizonData.InternalDeviceId).FirstOrDefault();
if (existingInternalVerizonDeviceIdRecord != null)
{
DeviceWithTheSameVerizonDeviceIdAlreadyExistsEvent?.Invoke(remoteDevice: remoteDevice,
localDevice: existingInternalVerizonDeviceIdRecord.Device,
verizonDeviceId: remoteDevice.VerizonData.InternalDeviceId);
}
else
{
// add new record
if (!string.IsNullOrWhiteSpace(remoteDevice.IMEI))
remoteDevice.Name = remoteDevice.IMEI;
else if (!string.IsNullOrWhiteSpace(remoteDevice.VerizonData.PrimaryPlaceOfUseFirstName) || !string.IsNullOrWhiteSpace(remoteDevice.VerizonData.PrimaryPlaceOfUseLastName))
remoteDevice.Name = $"{remoteDevice.VerizonData.PrimaryPlaceOfUseFirstName} {remoteDevice.VerizonData.PrimaryPlaceOfUseLastName}";
else
remoteDevice.Name = Core.Constants.Common.Undefined;
// set device Model
var verizonGroup = allVerizonGroups.Where(a => a.VerizonGroupName == remoteDevice.VerizonData.GroupName).FirstOrDefault();
if (!verizonGroup.DeviceModelId.HasValue)
VerizonGroupIsNotLinkedWithDeviceModelEvent?.Invoke(remoteDevice);
else
remoteDevice.DeviceModelId = verizonGroup.DeviceModelId;
// set device status
remoteDevice.Status = remoteDevice.VerizonData.State.ConvertToDeviceStatusFromVerizonStatus();
if (!string.IsNullOrWhiteSpace(remoteDevice.VerizonData.CarrierName))
{
var verizonCarrier = allVerizonCarriers.Where(a => a.VerizonCarrierName == remoteDevice.VerizonData.CarrierName).FirstOrDefault();
if (verizonCarrier.CarrierId.HasValue)
remoteDevice.CarrierId = verizonCarrier.CarrierId;
else
{
// notify that carrier is not found locally
LocalCarrierIsNotFoundForNewDeviceEvent?.Invoke(remoteDevice);
}
}
remoteDevice.SyncVerizon = true;
_context.Devices.Add(remoteDevice);
newDevices.Add(remoteDevice);
}
}
}
await _context.SaveChangesAsync();
why so? only one record has 1420531689, old was updated for new value (111111111) and it saved in the one transaction... How to do it correctly?

swashbuckle openapi 3 write example and description for the dynamically generated model classes

My model properties definition is coming from a json file so using reflection to write the classes to be shown under schema on resulting swagger page.
foreach (var model in Models)
{
if (!ModelTypes.ContainsKey(model.Key))
{
anyNonCompiledModel = true;
BuildModelCodeClass(modelComponentBuilder, model.Value);//Build model classes
}
}
BuildModelCodeEnd(modelComponentBuilder);
if (anyNonCompiledModel)
{
CSharpCompiler compiler = new CSharpCompiler();
compiler.AddReference(typeof(object));
compiler.AddReference(typeof(ResourceFactory));
compiler.AddReference(typeof(System.Runtime.Serialization.DataContractResolver));
compiler.AddReference(typeof(System.Runtime.Serialization.DataContractAttribute));
var types = compiler.Compiler(modelComponentBuilder.ToString()); //write model classes
foreach (var type in types)
{
ModelTypes.Add(type.Name, type);
}
}
public void BuildModelCodeClass(StringBuilder modelComponentBuilder, MetadataModelEntity model)
{
modelComponentBuilder.AppendLine($"public class {model.Name} {{");
foreach (var p in model.Data.Properties)
{
if (p.Obsoleted) continue;
if (p.Type.Type == "array")
{
modelComponentBuilder.AppendLine($" public {p.Type.ArrayType.ObjectName}[] {p.Name} {{get;set;}}");
}
else
{
//primitive types
modelComponentBuilder.AppendLine($" public {p.Type.ObjectName} {p.Name} {{get;set;}}");
}
}
modelComponentBuilder.AppendLine(
#"}
");
}
If i provide the description and example like following (in BuildModelCodeClass, inside the loop) then the example and description displays for me.
if (!string.IsNullOrWhiteSpace((string)p.Example))
{
modelComponentBuilder.AppendLine($" ///<example>{p.Example}</example>");
}
if (!string.IsNullOrWhiteSpace((string)p.Description))
{
modelComponentBuilder.AppendLine($" ///<description>{p.Description}</description>");
}
However, i dont want to do above.
I want to write my models via the open api and not via the C# Compiler, is it possible?
I want to show example and description via schema (may be under paths some where). How can i do this? Context has my models info available that i can interact with here.
public class SwaggerDocumentFilter : IDocumentFilter
{
SwaggerDocument _swaggerDocument;
public SwaggerDocumentFilter(object apiConfigure)
{
_swaggerDocument = ((ApiGatewayConfiguration)apiConfigure).SwaggerDocument;
}
public void Apply(OpenApiDocument document, DocumentFilterContext context)
{
if (document.Info.Extensions == null || !document.Info.Extensions.ContainsKey(SwaggerEndpoint.ExtensionDocName)) return;
var openIdString = document.Info.Extensions[SwaggerEndpoint.ExtensionDocName] as OpenApiString;
if (openIdString == null) return;
var docName = openIdString.Value;
SwaggerEndpoint endpoint = _swaggerDocument.SwaggerEndpoints.SingleOrDefault(x => x.Name == docName);
if (endpoint == null) return;
//Add server objects
document.Servers = endpoint.ServerObjects;
//Add Tags objects
document.Tags = endpoint.Tags;
//Set swagger paths objects
var pathsObjects = _swaggerDocument.GetPathsObject(docName, context);
if (pathsObjects.IsValid())
{
pathsObjects.ToList().ForEach(
item => document.Paths.Add(item.Key, item.Value)
);
}
//Add Schema components
//Add Example/Examples
}
}
Following helped
https://github.com/domaindrivendev/Swashbuckle.WebApi/issues/162
AddSchemaExamples.cs
public class AddSchemaExamples : ISchemaFilter
{
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
{
if (type == typeof(Product))
{
schema.example = new Product
{
Id = 123,
Type = ProductType.Book,
Description = "Treasure Island",
UnitPrice = 10.0M
};
}
}
}
SwaggerConfig.cs
httpConfig
.EnableSwagger(c =>
{
c.SchemaFilter<AddSchemaExamples>()
});
My implementation for the Apply since model is dynamic
if (model != null)
{
schema.Description = model.Description;
foreach (var p in schema.Properties)
{
var mp = model.Data.Properties.SingleOrDefault(x => x.Name == p.Key);
if (mp != null)
{
if (!string.IsNullOrWhiteSpace(mp.Description))
{
p.Value.Description = mp.Description;
}
if(!string.IsNullOrWhiteSpace(mp.Example))
{
p.Value.Example =
new Microsoft.OpenApi.Any.OpenApiString(mp.Example.ToString());
}
}
}
}

Fill Picture content control in header of word doc using OpenXML

I want to fill my Picture Content Control which is located in the header of my word document with this code: (I have passed content control tag and the image stream via document parameter to this function)
public void FillDocument(Stream stream, XDocument document)
{
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Open(stream, true))
{
List<SdtElement> descendants = wordDocument.MainDocumentPart.Document.Descendants<SdtElement>().ToList();
foreach (var headerPart in wordDocument.MainDocumentPart.HeaderParts)
{
descendants.AddRange(headerPart.Header.Descendants<SdtElement>().ToList());
}
foreach (var footerPart in wordDocument.MainDocumentPart.FooterParts)
{
descendants.AddRange(footerPart.Footer.Descendants<SdtElement>().ToList());
}
XDocument doc = document;
foreach (SdtElement item in descendants)
{
SdtAlias alias = item.Descendants<SdtAlias>().FirstOrDefault();
if (alias != null)
{
string sdtTitle = alias.Val.Value;
//if Sdt Content Control is Picture
string imageContent = (from xElement in doc.Descendants("Picture") where xElement.Attribute("Id").Value == sdtTitle select xElement.Value).FirstOrDefault();
if (imageContent != null)
{
MemoryStream result = (MemoryStream)StringToStream(imageContent);
SdtProperties p = item.Elements<SdtProperties>().FirstOrDefault();
if (p != null)
{
// Is it a picture content control?
SdtContentPicture pict = p.Elements<SdtContentPicture>().FirstOrDefault();
// Get the alias.
SdtAlias a = p.Elements<SdtAlias>().FirstOrDefault();
if (pict != null && a.Val.Value == sdtTitle)
{
string embed = null;
Drawing dr = item.Descendants<Drawing>().FirstOrDefault();
if (dr != null)
{
D.Blip blip = dr.Descendants<D.Blip>().FirstOrDefault();
if (blip != null)
embed = blip.Embed;
if (embed != null)
{
IdPartPair idpp = wordDocument.MainDocumentPart.Parts
.Where(pa => pa.RelationshipId == embed).FirstOrDefault();
if (idpp != null)
{
ImagePart ip = (ImagePart)idpp.OpenXmlPart;
ip.FeedData(result);
}
}
}
}
}
continue;
}
}
}
It finds the content control in word document but in this line:
ImagePart ip = (ImagePart)idpp.OpenXmlPart;
I get this error:
Unable to cast object of type
‘DocumentFormat.OpenXml.Packaging.CustomXmlPart’ to type
‘DocumentFormat.OpenXml.Packaging.ImagePart’.
Could you please guide me?
I tried to find a way, here is the answer:
public void FillDocument(Stream stream, XDocument document)
{
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Open(stream, true))
{
List<SdtElement> descendants = wordDocument.MainDocumentPart.Document.Descendants<SdtElement>().ToList();
foreach (HeaderPart headerPart in wordDocument.MainDocumentPart.HeaderParts)
{
descendants.AddRange(headerPart.Header.Descendants<SdtElement>().ToList());
}
foreach (var footerPart in wordDocument.MainDocumentPart.FooterParts)
{
descendants.AddRange(footerPart.Footer.Descendants<SdtElement>().ToList());
}
XDocument doc = document;
foreach (SdtElement item in descendants)
{
SdtAlias alias = item.Descendants<SdtAlias>().FirstOrDefault();
if (alias != null)
{
string sdtTitle = alias.Val.Value;
//if Sdt Content Control is Picture
string imageContent = (from xElement in doc.Descendants("Picture") where xElement.Attribute("Id").Value == sdtTitle select xElement.Value).FirstOrDefault();
if (imageContent != null)
{
MemoryStream result = (MemoryStream)StringToStream(imageContent);
D.Blip blipElement = item.Descendants<D.Blip>().FirstOrDefault();
string imageId = "default value";
if (blipElement != null)
{
imageId = blipElement.Embed.Value;
ImagePartType imagePartType = ImagePartType.Png;
//Add image and change embeded id.
ImagePart imagePart = null;
Type p = item.Parent.GetType();
switch (p.Name)
{
case "Header":
HeaderPart headerPart = ((Header)(item.Parent)).HeaderPart;
imagePart = headerPart.AddImagePart(imagePartType);
imagePart.FeedData(result);
blipElement.Embed = headerPart.GetIdOfPart(imagePart);
break;
case "Body":
MainDocumentPart mainDocumentPart = wordDocument.MainDocumentPart;
imagePart = mainDocumentPart.AddImagePart(imagePartType);
imagePart.FeedData(result);
blipElement.Embed = mainDocumentPart.GetIdOfPart(imagePart);
break;
case "Footer":
FooterPart footerPart = ((Footer)(item.Parent)).FooterPart;
imagePart = footerPart.AddImagePart(imagePartType);
imagePart.FeedData(result);
blipElement.Embed = footerPart.GetIdOfPart(imagePart);
break;
default:
break;
}
}
continue;
}
}
}
}}
It works fine and can fill the picture content control in header or footer or body!

how to add extend breeze entity types with metadata pulled from property attributes

I want to get the custom attributes, mentioned below, in breeze dataService (client side).
namespace Tam.Framework.Web.Models
{
[ViewAttribute("app/views/Employee.html")]//this custom class attribute
public class Employee : BaseEntity
{
protected override string OnGetDescriptor()
{
return "some description";
}
public string FirstName { get; set; }
[Display(Name = "LAST NAME")]//this custom property attribute
public string LastName { get; set; }
}
}
On the server, add logic to the Metadata controller action to supplement the standard metadata with the display attribute properties:
[HttpGet]
public virtual string Metadata()
{
// Extend metadata with extra attributes
var metadata = JObject.Parse(this.ContextProvider.Metadata());
var ns = metadata["schema"]["namespace"].ToString();
foreach (var breezeEntityType in metadata["schema"]["entityType"])
{
var typeName = ns + "." + breezeEntityType["name"].ToString();
var entityType = BuildManager.GetType(typeName, true);
foreach (var propertyInfo in entityType.GetProperties())
{
var attributes = propertyInfo.GetAllAttributes();
var breezePropertyInfo = breezeEntityType["property"].SingleOrDefault(p => p["name"].ToString() == propertyInfo.Name);
if (breezePropertyInfo == null)
continue;
// handle display attribute...
var displayAttribute = attributes.OfType<DisplayAttribute>().FirstOrDefault();
if (displayAttribute != null)
{
var displayName = displayAttribute.GetName();
if (displayName != null)
breezePropertyInfo["displayName"] = displayName;
var displayOrder = displayAttribute.GetOrder();
if (displayOrder != null)
breezePropertyInfo["displayOrder"] = displayOrder;
var autogenerateField = displayAttribute.GetAutoGenerateField();
if (autogenerateField != null)
breezePropertyInfo["autoGenerateField"] = autogenerateField;
}
// allowEmptyStrings.
if (propertyInfo.PropertyType == typeof(string))
{
breezePropertyInfo["allowEmptyStrings"] = true;
var requiredAttribute = attributes.OfType<RequiredAttribute>().FirstOrDefault();
if (requiredAttribute != null && !requiredAttribute.AllowEmptyStrings)
breezePropertyInfo["allowEmptyStrings"] = false;
}
// todo: handle other types of attributes...
}
}
return metadata.ToString();
}
On the client, fetch the metadata and supplement the breeze entity type with the custom metadata.
function initializeMetadataStore(metadataStore, metadata) {
var metadataType, metadataProperty, entityProperty, i, j;
for (i = 0; i < metadata.schema.entityType.length; i++) {
metadataType = metadata.schema.entityType[i];
var entityType = metadataStore.getEntityType(metadataType.name);
for (j = 0; j < metadataType.property.length; j++) {
metadataProperty = metadataType.property[j];
entityProperty = entityType.getProperty(metadataProperty.name);
if (entityProperty) {
if (typeof metadataProperty.displayName !== 'undefined') {
entityProperty.displayName = metadataProperty.displayName;
}
if (typeof metadataProperty.displayOrder !== 'undefined') {
entityProperty.displayOrder = metadataProperty.displayOrder;
}
if (typeof metadataProperty.autoGenerateField !== 'undefined') {
entityProperty.autoGenerateField = metadataProperty.autoGenerateField;
}
if (typeof metadataProperty.allowEmptyStrings !== 'undefined') {
entityProperty.allowEmptyStrings = metadataProperty.allowEmptyStrings;
}
}
}
}
}
var entityManager = ....something...;
entityManager.fetchMetadata(function (metadata) {
return initializeMetadataStore(entityManager.metadataStore, metadata);
});
now the additional metadata is available in the breeze entity type...
var propertyDisplayName = myEntity.entityType.getProperty('lastName').displayName;
var manager = configureBreezeManager();
function configureBreezeManager() {
breeze.NamingConvention.camelCase.setAsDefault();
var mgr = new breeze.EntityManager('api/breeze');
model.configureMetadataStore(mgr.metadataStore);
mgr.fetchMetadata(function (metadata) {
return initializeMetadataStore(mgr.metadataStore, metadata);
});
return mgr;
};
function initializeMetadataStore(metadataStore, metadata) {
breeze.NamingConvention.defaultInstance = breeze.NamingConvention.none;
var metadataType, metadataProperty, entityProperty, i, j;
for (i = 0; i < metadata.schema.entityType.length; i++) {
metadataType = metadata.schema.entityType[i];
var entityType = metadataStore.getEntityType(metadataType.name);
for (j = 0; j < metadataType.property.length; j++) {
metadataProperty = metadataType.property[j];
entityProperty = entityType.getProperty(metadataProperty.name);
if (entityProperty) {
if (typeof metadataProperty.displayName !== 'undefined') {
entityProperty.displayName = metadataProperty.displayName;
}
if (typeof metadataProperty.displayOrder !== 'undefined') {
entityProperty.displayOrder = metadataProperty.displayOrder;
}
if (typeof metadataProperty.autoGenerateField !== 'undefined') {
entityProperty.autoGenerateField = metadataProperty.autoGenerateField;
}
if (typeof metadataProperty.allowEmptyStrings !== 'undefined') {
entityProperty.allowEmptyStrings = metadataProperty.allowEmptyStrings;
}
}
}
}
}
var readData = function (entityName, observableResults, showLog) {
if (!entityName || !observableResults)
return null;
var query = new breeze.EntityQuery()
.from(entityName);
return manager.executeQuery(query)
.then(querySucceeded)
.fail(queryFailed);
}
function readEmployee() {
return breezeDataService.readData("Employees", employees, true).then(function () {
var propertyDisplayName = employees()[0].entityType.getProperty('lastName').displayName;//error displayName undefined
}
}
when I get list of entity by readData function that list (observableResults[0]) have not any displayName but I add displayName and checked it by initializeMetadataStore function is correct
FINALLY!!!!! I found it it because of breeze.NamingConvention.camelCase.setAsDefault();