コード例 #1
0
        protected object TransformAttributeValueForLiquid(string entityLogicalName, string attributeLogicalName, object value)
        {
            if (value == null)
            {
                return(null);
            }

            var aliasedValue = value as AliasedValue;

            if (aliasedValue != null)
            {
                return(TransformAttributeValueForLiquid(aliasedValue.EntityLogicalName, aliasedValue.AttributeLogicalName, aliasedValue.Value));
            }

            if (value is Guid)
            {
                return(((Guid)value).ToString());
            }

            var entityReference = value as EntityReference;

            if (entityReference != null)
            {
                EntityAliasedAttributesDrop aliasedAttributesDrop;

                return(EntityAliasedAttributesDrops.TryGetValue(attributeLogicalName, out aliasedAttributesDrop)
                                        ? new EntityReferenceDrop(entityReference, aliasedAttributesDrop)
                                        : new EntityReferenceDrop(entityReference));
            }

            var optionSetValue = value as OptionSetValue;

            if (optionSetValue != null)
            {
                return(new OptionSetValueDrop(this, entityLogicalName, attributeLogicalName, optionSetValue, _languageCode));
            }

            var money = value as Money;

            if (money != null)
            {
                return(money.Value);
            }

            return(value);
        }
コード例 #2
0
        protected virtual object BeforeMethodInternal(string method)
        {
            if (string.Equals(method, "logicalname", StringComparison.OrdinalIgnoreCase))
            {
                return(LogicalName);
            }

            object value;

            if (Entity.Attributes.TryGetValue(method, out value))
            {
                return(TransformAttributeValueForLiquid(LogicalName, method, value));
            }

            EntityAliasedAttributesDrop aliasedAttributesDrop;

            if (EntityAliasedAttributesDrops.TryGetValue(method, out aliasedAttributesDrop))
            {
                return(aliasedAttributesDrop);
            }

            // Look up the dynamic method to see if there's a matching relationship name.

            var oneToMany = EntityMetadata.OneToManyRelationships
                            .FirstOrDefault(r => string.Equals(r.SchemaName, method, StringComparison.OrdinalIgnoreCase));

            var manyToOne = EntityMetadata.ManyToOneRelationships
                            .FirstOrDefault(r => string.Equals(r.SchemaName, method, StringComparison.OrdinalIgnoreCase));

            // If there's an ambiguous reflexive relationship, return a reflexive relationship drop so the user
            // can further dot-through to select which side of the relationship they want.
            if (oneToMany != null && manyToOne != null)
            {
                return(new EntityReflexiveRelationshipDrop(
                           new Lazy <EntityDrop[]>(() => GetRelatedEntities(new Relationship(oneToMany.SchemaName)
                {
                    PrimaryEntityRole = EntityRole.Referenced
                }), LazyThreadSafetyMode.None),
                           new Lazy <EntityDrop>(() => GetRelatedEntity(new Relationship(manyToOne.SchemaName)
                {
                    PrimaryEntityRole = EntityRole.Referencing
                }), LazyThreadSafetyMode.None)));
            }

            if (oneToMany != null)
            {
                return(GetRelatedEntities(new Relationship(oneToMany.SchemaName)));
            }

            if (manyToOne != null)
            {
                return(GetRelatedEntity(new Relationship(manyToOne.SchemaName)));
            }

            var manyToMany = EntityMetadata.ManyToManyRelationships
                             .FirstOrDefault(r => string.Equals(r.SchemaName, method, StringComparison.OrdinalIgnoreCase));

            if (manyToMany != null)
            {
                if (string.Equals(manyToMany.Entity1LogicalName, manyToMany.Entity2LogicalName, StringComparison.OrdinalIgnoreCase))
                {
                    return(new EntityManyToManyReflexiveRelationshipDrop(
                               new Lazy <EntityDrop[]>(() => GetRelatedEntities(new Relationship(manyToMany.SchemaName)
                    {
                        PrimaryEntityRole = EntityRole.Referenced
                    }), LazyThreadSafetyMode.None),
                               new Lazy <EntityDrop[]>(() => GetRelatedEntities(new Relationship(manyToMany.SchemaName)
                    {
                        PrimaryEntityRole = EntityRole.Referencing
                    }), LazyThreadSafetyMode.None)));
                }

                return(GetRelatedEntities(new Relationship(manyToMany.SchemaName)));
            }

            return(base.BeforeMethod(method));
        }