Пример #1
0
        public static void PopulateData()
        {
            ClearData();

            // populate data here...
            Item i = new Item()
            {
                ID          = NextItemID++,
                Description = "Default Item0",
                Name        = "Item0",
            };

            _items.Add(i);

            Photo p = new Photo()
            {
                ID          = NextItemID++,
                Description = "Default Photo1",
                Name        = "Photo1",
                Rating      = 3,
            };

            _items.Add(p);

            DerivedFromPhoto p2 = new DerivedFromPhoto()
            {
                ID          = NextItemID++,
                Description = "Derived Photo2",
                Name        = "Photo2",
                Rating      = 3,
            };

            _items.Add(p2);

            DerivedFromPhoto p3 = new DerivedFromPhoto()
            {
                ID          = NextItemID++,
                Description = "A Derived Photo3",
                Name        = "A Photo2",
                Rating      = 3,
            };

            _items.Add(p3);


            DataServiceStreamProvider.Init();

            using (Stream s = File.OpenWrite(DataServiceStreamProvider.GetStoragePath(p)))
            {
                byte[] buffer = new byte[] { 1, 2, 3, 4 };
                s.Write(buffer, 0, 4);
                s.Close();
            }

            Folder f = new Folder()
            {
                ID   = NextFolderID++,
                Name = "Folder1"
            };

            f.Items.Add(i);
            i.ParentFolder = f;
            f.Icon         = p;
            i.Icon         = p;

            f.Items.Add(p);
            p.ParentFolder = f;
            p.Icon         = p;

            f.Items.Add(p2);
            p2.ParentFolder = f;
            p2.Icon         = p;
            _folders.Add(f);

            Folder f1 = new Folder()
            {
                ID   = NextFolderID++,
                Name = "A Folder2"
            };

            f1.Items.Add(p3);
            p3.ParentFolder = f1;
            f1.Icon         = p;
            p3.Icon         = p3;
        }
Пример #2
0
        public Stream GetWriteStream(object entity, string etag, bool?checkETagForEquality, DataServiceOperationContext operationContext)
        {
            this.ThrowIfDisposed();
            CallOrderLog += "-GetWriteStream";
            bool returnEmptyStream;

            if (!operationContext.IsBatchRequest || !operationContext.AbsoluteServiceUri.MakeRelativeUri(operationContext.AbsoluteRequestUri).OriginalString.StartsWith("$"))
            {
                CheckETag(entity, etag, checkETagForEquality, operationContext, out returnEmptyStream);
            }

            string slug = operationContext.RequestHeaders["Slug"];

            if (entity.GetType() == typeof(Photo))
            {
                Photo p = (Photo)entity;

                if (operationContext.RequestMethod.Equals("POST", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(slug))
                {
                    int id;
                    if (int.TryParse(slug, out id))
                    {
                        p.ID = id;
                    }
                    else
                    {
                        p.Description = slug;
                    }
                }
            }
            else if (entity.GetType() == typeof(NorthwindModel.Customers))
            {
                NorthwindModel.Customers c = (NorthwindModel.Customers)entity;

                if (operationContext.RequestMethod.Equals("POST", StringComparison.OrdinalIgnoreCase))
                {
                    Random rand = TestUtil.Random;
                    c.CustomerID  = "C" + rand.Next(1000, 9999).ToString();
                    c.CompanyName = "Microsoft";

                    if (!string.IsNullOrEmpty(slug))
                    {
                        c.CustomerID = slug;
                    }
                }
            }
            else if (entity.GetType() == typeof(NorthwindModel.Orders))
            {
                NorthwindModel.Orders o = (NorthwindModel.Orders)entity;
                if (operationContext.RequestMethod.Equals("POST", StringComparison.OrdinalIgnoreCase))
                {
                    Random rand = TestUtil.Random;
                    o.OrderID = rand.Next(1000, 9999);
                }
            }
            else if (entity.GetType() == typeof(RowEntityTypeWithIDAsKey) && (((RowEntityTypeWithIDAsKey)entity).TypeName == CustomRowBasedContext.CustomerFullName || ((RowEntityTypeWithIDAsKey)entity).TypeName == CustomRowBasedContext.CustomerWithBirthdayFullName))
            {
                int id;
                if (int.TryParse(slug, out id))
                {
                    ((RowEntityTypeWithIDAsKey)entity).ID = id;
                }
                else
                {
                    ((RowEntityTypeWithIDAsKey)entity).ID = TestUtil.Random.Next(1000, 9999);
                }
            }
            else if (entity.GetType() == typeof(RowComplexType) && (((RowComplexType)entity).TypeName == "AstoriaUnitTests.Stubs.Customer" || ((RowComplexType)entity).TypeName == "AstoriaUnitTests.Stubs.CustomerWithBirthday"))
            {
                int id;
                if (int.TryParse(slug, out id))
                {
                    ((RowComplexType)entity).Properties["ID"] = id;
                }
                else
                {
                    ((RowComplexType)entity).Properties["ID"] = TestUtil.Random.Next(1000, 9999);
                }
            }

            ValidateArguments(entity, operationContext);
            SetCustomResponseHeaders(operationContext);

            Stream s;

            switch (DataServiceStreamProvider.ProviderStorageMode)
            {
            case StorageMode.Disk:
                s = File.Open(GetStoragePath(entity), FileMode.Create, FileAccess.Write);
                break;

            case StorageMode.LargeStream:
                s = new LargeStream();
                LargeStreamStorage[GetStoragePath(entity)] = (LargeStream)s;
                break;

            default:
                throw new InvalidOperationException("Unknown StorageMode!");
            }

            GetWriteStreamCalled = true;
            return(s);
        }