示例#1
0
        static public DataServiceState Save(DataServiceContext context, Dictionary<string,object> collections)
        {
            List<Type> knownTypes = GetKnownTypes(context);

            Dictionary<EntityDescriptor, Guid> entityDescriptorToId;
            ContextState contextState = context.SaveState(out entityDescriptorToId);
            string contextAsString = SerializeContextToString(contextState, knownTypes);

            var collectionsState = new Dictionary<string, CollectionState>();
            if (collections != null)
            {
                foreach (KeyValuePair<string, object> kvp in collections)
                {
                    IDataServiceCollection collection = (IDataServiceCollection) kvp.Value;
                    CollectionState collectionState = collection.SaveState(context, entityDescriptorToId);                    
                    collectionsState.Add(kvp.Key, collectionState);
                }
            }

            DataServiceState state = new DataServiceState()
            {
                CollectionsState = collectionsState,
                ContextAsString = contextAsString,
                ContextTypeName = context.GetType().AssemblyQualifiedName,
                KnownTypeNames = knownTypes.Select(t => t.AssemblyQualifiedName).ToList()
            };

            return state;
        }
        public static DataServiceQuery CreateQuery(this DataServiceContext context, Type elementType, string entitySetName)
        {
            MethodInfo       method  = context.GetType().GetMethod("CreateQuery");
            MethodInfo       generic = method.MakeGenericMethod(elementType);
            DataServiceQuery query   = generic.Invoke(context, new object[] { entitySetName }) as DataServiceQuery;

            return(query);
        }
示例#3
0
        private void ProcessForeignKeys(DataServiceContext dataServiceContext, object dataObject, IDictionary values)
        {
            foreach (string key in values.Keys)
            {
                // Check if it looks like a FK, e.g. Category.CategoryID
                string[] parts = key.Split('.');
                if (parts.Length != 2)
                {
                    continue;
                }

                // Get the name of the entity ref property, e.g. Category
                string entityRefPropertyName = parts[0];

                // Create an 'empty' relaty entity, e.g a Category
                PropertyInfo propInfo        = dataObject.GetType().GetProperty(entityRefPropertyName);
                object       entityRefObject = Activator.CreateInstance(propInfo.PropertyType);

                // Set the PK in the related entity, e.g. set the CategoryID in the Category
                PropertyInfo subPropInfo = propInfo.PropertyType.GetProperty(parts[1]);
                subPropInfo.SetValue(
                    entityRefObject,
                    Convert.ChangeType(values[key], subPropInfo.PropertyType),
                    null);

                // Find the entity set property for the association
                var entitySetProp = DataServiceUtilities.FindEntitySetProperty(
                    dataServiceContext.GetType(), propInfo.PropertyType);

                // Attach the related entity and set it as the link on the main entity
                if (entitySetProp != null)
                {
                    dataServiceContext.AttachTo(entitySetProp.Name, entityRefObject);
                    dataServiceContext.SetLink(dataObject, entityRefPropertyName, entityRefObject);
                }
            }
        }
        private void ProcessForeignKeys(DataServiceContext dataServiceContext, object dataObject, IDictionary values)
        {
            foreach (string key in values.Keys) {
                // Check if it looks like a FK, e.g. Category.CategoryID
                string[] parts = key.Split('.');
                if (parts.Length != 2)
                    continue;

                // Get the name of the entity ref property, e.g. Category
                string entityRefPropertyName = parts[0];

                // Get the PropertyInfo for the entity ref property
                PropertyInfo propInfo = dataObject.GetType().GetProperty(entityRefPropertyName);

                object entityRefObject = null;

                if (values[key] != null) {
                    // Create an 'empty' related entity, e.g a Category
                    entityRefObject = Activator.CreateInstance(propInfo.PropertyType);

                    // Set the PK in the related entity, e.g. set the CategoryID in the Category
                    PropertyInfo subPropInfo = propInfo.PropertyType.GetProperty(parts[1]);
                    subPropInfo.SetValue(
                        entityRefObject,
                        Convert.ChangeType(values[key], subPropInfo.PropertyType),
                        null);
                }

                // Find the entity set property for the association table e.g. Categories
                PropertyInfo entitySetProp = DataServiceUtilities.FindEntitySetProperty(
                    dataServiceContext.GetType(), propInfo.PropertyType);

                // Attach the related entity and set it as the link on the main entity
                if (entitySetProp != null) {
                    if (entityRefObject != null) {
                        dataServiceContext.AttachTo(entitySetProp.Name, entityRefObject);
                    }
                    dataServiceContext.SetLink(dataObject, entityRefPropertyName, entityRefObject);
                }
            }
        }