Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

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; }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...