Example #1
0
        public EntityError Resolve(EntityManager em)
        {
            IsServerError = true;
            try {
                EntityType entityType = null;
                if (EntityTypeName != null)
                {
                    var stName = StructuralType.ClrTypeNameToStructuralTypeName(EntityTypeName);
                    entityType = MetadataStore.Instance.GetEntityType(stName);
                    var ek = new EntityKey(entityType, KeyValues);
                    Entity = em.FindEntityByKey(ek);
                }

                if (PropertyName != null)
                {
                    PropertyName = MetadataStore.Instance.NamingConvention.ServerPropertyNameToClient(PropertyName);
                }
                if (Entity != null)
                {
                    Property = entityType.GetProperty(PropertyName);
                    var vc = new ValidationContext(this.Entity);
                    vc.Property = this.Property;
                    var veKey = (ErrorName ?? ErrorMessage) + (PropertyName ?? "");
                    var ve    = new ValidationError(null, vc, ErrorMessage, veKey);
                    ve.IsServerError = true;
                    this.Entity.EntityAspect.ValidationErrors.Add(ve);
                }
            } catch (Exception e) {
                ErrorMessage = (ErrorMessage ?? "") + ":  Unable to Resolve this error: " + e.Message;
            }
            return(this);
        }
        private Tuple <EntityKey, EntityKey> ToEntityKeys(KeyMapping keyMapping)
        {
            var entityTypeName = StructuralType.ClrTypeNameToStructuralTypeName(keyMapping.EntityTypeName);
            var et             = MetadataStore.Instance.GetEntityType(entityTypeName);
            var oldKey         = new EntityKey(et, keyMapping.TempValue);
            var newKey         = new EntityKey(et, keyMapping.RealValue);

            return(Tuple.Create(oldKey, newKey));
        }
Example #3
0
        protected virtual Object CreateAndPopulate(JsonContext jsonContext)
        {
            var jObject = jsonContext.JObject;

            JToken refToken = null;

            if (jObject.TryGetValue("$ref", out refToken))
            {
                return(_refMap[refToken.Value <String>()]);
            }

            EntityType entityType;
            Type       objectType;
            JToken     typeToken = null;

            if (jObject.TryGetValue("$type", out typeToken))
            {
                var clrTypeName    = typeToken.Value <String>();
                var entityTypeName = StructuralType.ClrTypeNameToStructuralTypeName(clrTypeName);
                entityType = _metadataStore.GetEntityType(entityTypeName);
                objectType = entityType.ClrType;
                if (!jsonContext.ObjectType.IsAssignableFrom(objectType))
                {
                    throw new Exception("Unable to convert returned type: " + objectType.Name + " into type: " + jsonContext.ObjectType.Name);
                }
                jsonContext.ObjectType = objectType;
            }
            else
            {
                objectType = jsonContext.ObjectType;
                entityType = _metadataStore.GetEntityType(objectType);
            }

            // an entity type
            jsonContext.StructuralType = entityType;
            var keyValues = entityType.KeyProperties
                            .Select(p => jObject[p.Name].ToObject(p.ClrType))
                            .ToArray();
            var entityKey = EntityKey.Create(entityType, keyValues);
            var entity    = _entityManager.FindEntityByKey(entityKey);

            if (entity == null)
            {
                entity = (IEntity)Activator.CreateInstance(objectType);
            }
            // must be called before populate
            UpdateRefMap(jObject, entity);
            _allEntities.Add(entity);
            return(PopulateEntity(jsonContext, entity));
        }