Exemplo n.º 1
0
        public static IOfflineEntity GetObjectForType(EntryInfoWrapper wrapper, Type[] knownTypes)
        {
            Type entityType;

            ConstructorInfo ctorInfo;

            // See if its cached first.
            if (!_stringToCtorInfoMapping.TryGetValue(wrapper.TypeName, out ctorInfo))
            {
                // Its not cached. Try to look for it then in list of known types.
                if (knownTypes != null)
                {
                    entityType = knownTypes.FirstOrDefault(e => e.FullName.Equals(wrapper.TypeName, StringComparison.CurrentCultureIgnoreCase));

                    if (entityType == null)
                        throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unable to find a matching type for entry '{0}' in list of KnownTypes.", wrapper.TypeName));

                }
                else
                {
                    throw new InvalidOperationException(string.Format("Unable to find a matching type for entry '{0}' in the loaded assemblies. Specify the type name in the KnownTypes argument to the SyncReader instance.", wrapper.TypeName));
                }

                // Reflect this entity and get necessary info
                GetPropertyInfoMapping(entityType);
                ctorInfo = _stringToCtorInfoMapping[wrapper.TypeName];
            }
            else
            {
                entityType = ctorInfo.DeclaringType;
            }

            // Invoke the ctor
            object obj = ctorInfo.Invoke(null);

            // Set the parameters only for non tombstone items
            if (!wrapper.IsTombstone)
            {
                IEnumerable<PropertyInfo> props = GetPropertyInfoMapping(entityType);
                foreach (PropertyInfo pinfo in props)
                {
                    string value;
                    if (wrapper.PropertyBag.TryGetValue(pinfo.Name, out value))
                        pinfo.SetValue(obj, GetValueFromType(pinfo.PropertyType, value), null);
                }
            }

            IOfflineEntity entity = (IOfflineEntity)obj;
            entity.SetServiceMetadata(new OfflineEntityMetadata(wrapper.IsTombstone, wrapper.Id, wrapper.ETag,
                                                                wrapper.EditUri));
            return entity;
        }
Exemplo n.º 2
0
        protected static Entity CreateEntity(EntryInfoWrapper wrapper, EntityType[] knownTypes)
        {
            string[] split = wrapper.TypeName.Split('.');
            string schema = split[1];
            string name = split[2];

            EntityType entityType = knownTypes.First(val => val.Name == name && val.Schema == schema);

            var entity = new Entity(entityType);
            foreach (EntityField entityField in entityType.Fields)
            {
                string value;
                if (entityField.Type == typeof(IDbRef))
                    entity.SetDbRefValue(entityField.Name, entityField.DbRefTable, wrapper.PropertyBag["__" + entityField.Name]);

                else if (wrapper.PropertyBag.TryGetValue(entityField.Name, out value))
                    entity.SetValue(entityField.Name, value);
                else
                    throw new Exception(string.Format("Property {0} not exists in response", entityField.Name));
            }
            entity.ServiceMetadata = new OfflineEntityMetadata(wrapper.IsTombstone, wrapper.Id, wrapper.ETag, wrapper.EditUri);

            return entity;
        }
Exemplo n.º 3
0
        public static IOfflineEntity GetObjectForType(EntryInfoWrapper wrapper, Type[] knownTypes)
        {
            Type entityType = null;

            ConstructorInfo ctorInfo = null;

            // See if its cached first.
            if (!_stringToCtorInfoMapping.TryGetValue(wrapper.TypeName, out ctorInfo))
            {
                // Its not cached. Try to look for it then in list of known types.
                if (knownTypes != null)
                {
                    entityType = knownTypes.Where((e) => e.FullName.Equals(wrapper.TypeName, StringComparison.InvariantCulture)).FirstOrDefault();

                    if (entityType == null)
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unable to find a matching type for entry '{0}' in list of KnownTypes.", wrapper.TypeName));
                    }
                }
                else
                {
                    // Try to look for the type in the list of all loaded assemblies.
#if SERVER
                    Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
#elif CLIENT
                    Assembly[] loadedAssemblies = new Assembly[] { Assembly.GetExecutingAssembly(), Assembly.GetCallingAssembly()};
#endif
                    foreach (Assembly assembly in loadedAssemblies)
                    {
#if SERVER
                        entityType = assembly.GetTypes().Where((e) => e.FullName.Equals(wrapper.TypeName, StringComparison.InvariantCulture) &&
                            e.GetCustomAttributes(typeof(SyncEntityTypeAttribute), true).Count() > 0).FirstOrDefault();
#elif CLIENT
                        entityType = assembly.GetTypes().Where((e) => e.FullName.Equals(wrapper.TypeName, StringComparison.InvariantCulture) &&
                            e.GetInterface("IOfflineEntity", false) != null).FirstOrDefault();
#endif 
                        if (entityType != null)
                        {
                            break;
                        }
                    }

                    if (entityType == null)
                    {
                        throw new InvalidOperationException(string.Format("Unable to find a matching type for entry '{0}' in the loaded assemblies. Specify the type name in the KnownTypes argument to the SyncReader instance.", wrapper.TypeName));
                    }
                }

                // Reflect this entity and get necessary info
                ReflectionUtility.GetPropertyInfoMapping(entityType);
                ctorInfo = _stringToCtorInfoMapping[wrapper.TypeName];
            }
            else
            {
                entityType = ctorInfo.ReflectedType;
            }

            // Invoke the ctor
            object obj = ctorInfo.Invoke(null);

            // Set the parameters only for non tombstone items
            if (!wrapper.IsTombstone)
            {
                PropertyInfo[] props = GetPropertyInfoMapping(entityType);
                foreach (PropertyInfo pinfo in props)
                {
                    string value = null;
                    if (wrapper.PropertyBag.TryGetValue(pinfo.Name, out value))
                    {
                        pinfo.SetValue(obj, GetValueFromType(pinfo.PropertyType, value), null);
                    }
                }
            }

            IOfflineEntity entity = (IOfflineEntity)obj;
            entity.ServiceMetadata = new OfflineEntityMetadata(wrapper.IsTombstone, wrapper.Id, wrapper.ETag, wrapper.EditUri);
            return entity;
        }
Exemplo n.º 4
0
        protected static IsolatedStorageOfflineEntity CreateEntity(EntryInfoWrapper wrapper, Dictionary<string, EntityType> knownTypes)
        {
            EntityType entityType = knownTypes[wrapper.TypeName];

            int i = 0;

            var entity = new Entity(entityType);
            if (!wrapper.IsTombstone)
                foreach (IEntityField entityField in entityType.Fields.OrderBy(val => val.Name))
                {
                    if (wrapper.PropertyBag.Count > 0)
                    {
                        string value;
                        if (entityField.Type == typeof(IDbRef))
                            entity.SetDbRefValue(entityField.Name, entityField.DbRefTable, wrapper.PropertyBag["__" + entityField.Name]);

                        else if (wrapper.PropertyBag.TryGetValue(entityField.Name, out value))
                            entity.SetValue(entityField.Name, value);
                        else
                            throw new Exception(string.Format("Property {0} not exists in response", entityField.Name));
                    }
                    else if (wrapper.Values.Count > 0)
                    {
                        string value = wrapper.Values[i];
                        if (entityField.Type == typeof(IDbRef))
                            entity.SetDbRefValue(entityField.Name, entityField.DbRefTable, value);
                        else
                            entity.SetValue(entityField.Name, value);
                        i++;
                    }
                    else
                        throw new Exception("Values and PropertyBag is empty");
                }
            else if (wrapper.Values.Count == 1)
                entity.SetDbRefValue(entity.EntityType.IdFieldName, entity.EntityType.TableName, wrapper.Values[0]);

            string id = ((IDbRef)entity.GetValue(entity.EntityType.IdFieldName)).Id.ToString();

            entity.ServiceMetadata = new OfflineEntityMetadata(wrapper.IsTombstone, id, wrapper.ETag, wrapper.EditUri);

            return entity;
        }