private static RowEntityTypeWithIDAsKey TryGetEntity(IList<RowEntityTypeWithIDAsKey> collection, RowEntityTypeWithIDAsKey entity)
        {
            RowEntityTypeWithIDAsKey matchingEntity = null;

            foreach (RowEntityTypeWithIDAsKey element in collection)
            {
                // check if there is not another instance with the same id
                if (element.ID == entity.ID)
                {
                    matchingEntity = element;
                    break;
                }
            }

            return matchingEntity;
        }
        private static void AddResource(RowEntityTypeWithIDAsKey resource, bool throwIfDuplicate)
        {
            IList<RowEntityTypeWithIDAsKey> entitySetInstance = GetEntitySet(resource);

            foreach (RowEntityTypeWithIDAsKey entity in entitySetInstance)
            {
                // check if there is not another instance with the same id
                if (resource.ID == entity.ID)
                {
                    if (throwIfDuplicate)
                    {
                        throw new Exception(String.Format("Entity with the same key already present. EntityType: '{0}'",
                            resource.GetType().Name));
                    }

                    // if its already there, do not add it to the global context
                    return;
                }
            }
            entitySetInstance.Add(resource);
        }
        private RowEntityTypeWithIDAsKey DeleteEntity(IList<RowEntityTypeWithIDAsKey> collection, RowEntityTypeWithIDAsKey entity, bool throwIfNotPresent)
        {
            RowEntityTypeWithIDAsKey entityToBeDeleted = TryGetEntity(collection, entity);

            if (entityToBeDeleted == null && throwIfNotPresent)
            {
                throw new Exception("No entity found with the given ID");
            }

            if (entityToBeDeleted != null)
            {
                collection.Remove(entity);
            }

            return entityToBeDeleted;
        }
        private static IList<RowEntityTypeWithIDAsKey> GetEntitySet(RowEntityTypeWithIDAsKey entity)
        {
            if (IsCustomerInstance(entity))
            {
                return customers;
            }

            if (IsOrderInstance(entity))
            {
                return orders;
            }

            if (IsProductInstance(entity))
            {
                return products;
            }

            if (IsRegionInstance(entity))
            {
                return regions;
            }

            throw new Exception(String.Format("Unexpected EntityType '{0}' encountered", entity.TypeName));
        }
        public object ResetResource(object resource)
        {
            RowEntityTypeWithIDAsKey existingEntity = (RowEntityTypeWithIDAsKey)this.TokenToResource(resource);
            RowEntityTypeWithIDAsKey newEntity = new RowEntityTypeWithIDAsKey(existingEntity.TypeName);
            newEntity.ID = existingEntity.ID;

            DeleteResource(resource);
            this.PendingChanges.Add(new KeyValuePair<object, EntityState>(newEntity, EntityState.Added));
            return this.ResourceToToken(newEntity);
        }
        public object CreateResource(string containerName, string fullTypeName)
        {
            object resource = null;
            if (containerName == "Customers")
            {
                if (fullTypeName == CustomRowBasedContext.CustomerFullName)
                {
                    resource = new RowEntityTypeWithIDAsKey(CustomRowBasedContext.CustomerFullName);
                }

                if (fullTypeName == CustomRowBasedContext.CustomerWithBirthdayFullName)
                {
                    resource = new RowEntityTypeWithIDAsKey(CustomRowBasedContext.CustomerWithBirthdayFullName);
                }
            }
            else if (containerName == "Orders")
            {
                if (fullTypeName == CustomRowBasedContext.OrderFullName)
                {
                    resource = new RowEntityTypeWithIDAsKey(CustomRowBasedContext.OrderFullName);
                }
            }
            else if (containerName == "Regions")
            {
                if (fullTypeName == CustomRowBasedContext.RegionFullName)
                {
                    resource = new RowEntityType(CustomRowBasedContext.RegionFullName);
                }
            }
            else if (fullTypeName == CustomRowBasedContext.AddressFullName)
            {
                // no need to add this to the pending changelist. Only entities need to be added
                return this.ResourceToToken(new RowComplexType(CustomRowBasedContext.AddressFullName));
            }

            if (resource == null)
            {
                throw new Exception(String.Format("Invalid container name '{0}' or type name specified '{1}'", containerName, fullTypeName));
            }
            else
            {
                this.PendingChanges.Add(new KeyValuePair<object, EntityState>(resource, EntityState.Added));
            }

            return this.ResourceToToken(resource);
        }
 public RowEntityTypeWithIDAsKey InsertCustomer(int id, string name)
 {
     RowEntityTypeWithIDAsKey customer = new RowEntityTypeWithIDAsKey(CustomRowBasedContext.CustomerFullName);
     customer.ID = id;
     customer.Properties["Name"] = name;
     AddResource(customer, true);
     return customer;
 }
        internal static void PopulateData()
        {
            int orderCount = 2;
            int customerCount = 3;

            for (int i = 0; i < customerCount; i++)
            {
                RowEntityTypeWithIDAsKey region = new RowEntityTypeWithIDAsKey(CustomRowBasedContext.RegionFullName);
                region.ID = i;
                region.Properties["Name"] = "Region" + i.ToString();
                regions.Add(region);

                RowEntityTypeWithIDAsKey customer = (i % 2 == 0) ? new RowEntityTypeWithIDAsKey(CustomRowBasedContext.CustomerFullName) : new RowEntityTypeWithIDAsKey(CustomRowBasedContext.CustomerWithBirthdayFullName);
                customer.ID = i;
                customer.Properties["Name"] = "Customer " + i.ToString();
                customer.Properties["NameAsHtml"] = "<html><body>" + customer.Properties["Name"] + "</body></html>";
                customer.Properties["BestFriend"] = (i == 0) ? null : customers[i - 1];
                customer.Properties["Region"] = region;

                if (i % 2 != 0)
                {
                    customer.Properties["Birthday"] = DateTime.Today.AddYears(-30);
                }

                customer.Properties.Add("Orders", new List<RowEntityTypeWithIDAsKey>());

                for (int j = 0; j < orderCount; j++)
                {
                    int orderID = i + 100 * j;
                    int productID = i + 100 * j;
                    double orderDollarAmount = Math.Round(20.1 + 10.1 * j, 2);

                    RowEntityTypeWithIDAsKey o = new RowEntityTypeWithIDAsKey(CustomRowBasedContext.OrderFullName) { ID = orderID };
                    o.Properties["DollarAmount"] = orderDollarAmount;
                    o.Properties["OrderDetails"] = new List<RowEntityType>();
                    o.Properties["Customer"] = customer;
                    ((IList<RowEntityTypeWithIDAsKey>)customer.Properties["Orders"]).Add(o);
                    orders.Add(o);

                    RowEntityTypeWithIDAsKey p = new RowEntityTypeWithIDAsKey(CustomRowBasedContext.ProductFullName) { ID = productID };
                    p.Properties["ProductName"] = "Product #" + p.ID.ToString();
                    p.Properties["Discontinued"] = false;
                    p.Properties["OrderDetails"] = new List<RowEntityType>();
                    products.Add(p);

                    RowEntityType od = new RowEntityType(CustomRowBasedContext.OrderDetailsFullName);
                    od.Properties["ProductID"] = p.ID;
                    od.Properties["OrderID"] = o.ID;
                    od.Properties["UnitPrice"] = orderDollarAmount;
                    od.Properties["Quantity"] = (short)1;
                    orderDetails.Add(od);
                    ((IList<RowEntityType>)o.Properties["OrderDetails"]).Add(od);
                    ((IList<RowEntityType>)p.Properties["OrderDetails"]).Add(od);
                }

                customers.Add(customer);

                if (OpenWebDataServiceHelper.EnableBlobServer)
                {
                    DataServiceStreamProvider.Init();
                    using (System.IO.Stream s = System.IO.File.OpenWrite(DataServiceStreamProvider.GetStoragePath(customer)))
                    {
                        byte[] buffer = new byte[] { 1, 2, 3, 4 };
                        s.Write(buffer, 0, 4);
                        s.Close();
                    }
                }
            }
        }