示例#1
0
        public void BindTwoEntitiesSaveChanges()
        {
            var odataRequestMessage  = new ODataTestMessage();
            var odataResponseMessage = new ODataTestMessage()
            {
                StatusCode = 204
            };
            DataServiceContextWithCustomTransportLayer context = new DataServiceContextWithCustomTransportLayer(DataServiceProtocolVersion.V3, () => odataRequestMessage, () => odataResponseMessage);

            WritingEntityReferenceLinkArgs args = null;

            context.Configurations.RequestPipeline.OnEntityReferenceLink((r => args = r));

            SimpleNorthwind.Product product = new SimpleNorthwind.Product()
            {
                ID = 1
            };
            SimpleNorthwind.Supplier supplier = new SimpleNorthwind.Supplier()
            {
                ID = 2
            };

            context.AttachTo("Products", product);
            context.AttachTo("Suppliers", supplier);

            context.SetLink(product, "Supplier", supplier);
            context.SaveChanges();

            // Note writing entity links is not triggered here as its not in the payload
            args.Should().BeNull();
            odataRequestMessage.Method.Should().Be("PUT");
            odataRequestMessage.Url.AbsoluteUri.Should().Be("http://somedummyuri/myService.svc/Products(1)/$links/Supplier");
        }
示例#2
0
        public void AttachEntityAddEntityAndBindSaveChanges()
        {
            var odataRequestMessage  = new ODataTestMessage();
            var odataResponseMessage = new ODataTestMessage()
            {
                StatusCode   = 202,
                MemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(@"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
<entry xml:base=""http://services.odata.org/OData/OData.svc/"" xmlns:d=""http://schemas.microsoft.com/ado/2007/08/dataservices"" xmlns:m=""http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"" xmlns=""http://www.w3.org/2005/Atom"">
    <id>http://services.odata.org/OData/OData.svc/Suppliers(2)</id>
    <category term=""ODataDemo.Product"" scheme=""http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"" />
    <link rel=""edit"" title=""AnnotationTests_Product"" href=""Products(2)"" />
    <title />
    <updated>2012-11-15T19:29:45Z</updated>
    <author>
      <name />
    </author>
    <content type=""application/xml"">
      <m:properties>
        <d:ID m:type=""Edm.Int32"">1</d:ID>
      </m:properties>
    </content>
  </entry>"))
            };

            odataResponseMessage.SetHeader("DataServiceId", "http://service/foo/Products(2)");
            odataResponseMessage.SetHeader("Location", "http://service/foo/Products(2)");
            odataResponseMessage.SetHeader("Content-Type", "application/atom+xml");

            DataServiceContextWithCustomTransportLayer context = new DataServiceContextWithCustomTransportLayer(DataServiceProtocolVersion.V3, () => odataRequestMessage, () => odataResponseMessage);

            WritingNavigationLinkArgs      startingNavigLinkArgs = null;
            WritingNavigationLinkArgs      endingNavigLinkArgs   = null;
            WritingEntityReferenceLinkArgs args = null;

            context.Configurations.RequestPipeline.OnNavigationLinkStarting((r => startingNavigLinkArgs = r));
            context.Configurations.RequestPipeline.OnNavigationLinkEnding((r => endingNavigLinkArgs = r));
            context.Configurations.RequestPipeline.OnEntityReferenceLink((r => args = r));

            SimpleNorthwind.Product product = new SimpleNorthwind.Product()
            {
                ID = 1
            };
            SimpleNorthwind.Supplier supplier = new SimpleNorthwind.Supplier()
            {
                ID = 2
            };

            context.AddObject("Products", product);
            context.AttachTo("Suppliers", supplier);

            context.SetLink(product, "Supplier", supplier);
            context.SaveChanges();

            VerifyWritingNavigationLinkArgs(startingNavigLinkArgs, null, product, supplier, "Supplier");
            VerifyWritingNavigationLinkArgs(endingNavigLinkArgs, null, product, supplier, "Supplier");

            args.Should().NotBeNull();
            args.EntityReferenceLink.Url.AbsoluteUri.Should().Be("http://somedummyuri/myService.svc/Suppliers(2)");
        }
        public void AttachEntityAddEntityAndBindSaveChanges()
        {
            var odataRequestMessage = new ODataTestMessage();
            var odataResponseMessage = new ODataTestMessage()
            {
                StatusCode = 202,
                MemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(@"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
<entry xml:base=""http://services.odata.org/OData/OData.svc/"" xmlns:d=""http://schemas.microsoft.com/ado/2007/08/dataservices"" xmlns:m=""http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"" xmlns=""http://www.w3.org/2005/Atom"">
    <id>http://services.odata.org/OData/OData.svc/Suppliers(2)</id>
    <category term=""ODataDemo.Product"" scheme=""http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"" />
    <link rel=""edit"" title=""AnnotationTests_Product"" href=""Products(2)"" />
    <title />
    <updated>2012-11-15T19:29:45Z</updated>
    <author>
      <name />
    </author>
    <content type=""application/xml"">
      <m:properties>
        <d:ID m:type=""Edm.Int32"">1</d:ID>
      </m:properties>
    </content>
  </entry>")) };
            odataResponseMessage.SetHeader("DataServiceId", "http://service/foo/Products(2)");
            odataResponseMessage.SetHeader("Location", "http://service/foo/Products(2)");
            odataResponseMessage.SetHeader("Content-Type", "application/atom+xml");

            DataServiceContextWithCustomTransportLayer context = new DataServiceContextWithCustomTransportLayer(DataServiceProtocolVersion.V3, () => odataRequestMessage, () => odataResponseMessage);

            WritingNavigationLinkArgs startingNavigLinkArgs = null;
            WritingNavigationLinkArgs endingNavigLinkArgs = null;
            WritingEntityReferenceLinkArgs args = null;
            context.Configurations.RequestPipeline.OnNavigationLinkStarting((r => startingNavigLinkArgs = r));
            context.Configurations.RequestPipeline.OnNavigationLinkEnding((r => endingNavigLinkArgs = r));
            context.Configurations.RequestPipeline.OnEntityReferenceLink((r => args = r));

            SimpleNorthwind.Product product = new SimpleNorthwind.Product() { ID = 1 };
            SimpleNorthwind.Supplier supplier = new SimpleNorthwind.Supplier() { ID = 2 };

            context.AddObject("Products", product);
            context.AttachTo("Suppliers", supplier);

            context.SetLink(product, "Supplier", supplier);
            context.SaveChanges();

            VerifyWritingNavigationLinkArgs(startingNavigLinkArgs, null, product, supplier, "Supplier");
            VerifyWritingNavigationLinkArgs(endingNavigLinkArgs, null, product, supplier, "Supplier");

            args.Should().NotBeNull();
            args.EntityReferenceLink.Url.AbsoluteUri.Should().Be("http://somedummyuri/myService.svc/Suppliers(2)");
        }
        public void LoadCategoryPropertyTest()
        {
            const string payload = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
<entry xml:base=""http://services.odata.org/OData/OData.svc/"" xmlns:d=""http://docs.oasis-open.org/odata/ns/data"" xmlns:m=""http://docs.oasis-open.org/odata/ns/metadata"" xmlns=""http://www.w3.org/2005/Atom"">
  <id>http://services.odata.org/OData/OData.svc/Categories(0)</id>
  <title type=""text"">Food</title>
  <updated>2012-11-15T16:42:58Z</updated>
  <author>
    <name />
  </author>
  <link rel=""edit"" title=""Category"" href=""Categories(0)"" />
  <link rel=""http://docs.oasis-open.org/odata/ns/related/Products"" type=""application/atom+xml;type=feed"" title=""Products"" href=""Categories(0)/Products"" />
  <category term=""#ODataDemo.Category"" scheme=""http://docs.oasis-open.org/odata/ns/scheme"" />
  <content type=""application/xml"">
    <m:properties>
      <d:ID m:type=""Edm.Int32"">0</d:ID>
      <d:Name>Food</d:Name>
    </m:properties>
  </content>
</entry>";

            var context = CreateTransportLayerContext(payload);
            var p       = new SimpleNorthwind.Product()
            {
                ID = 0
            };

            context.AttachTo("Products", p);
            var operationResponse = context.LoadProperty(p, "Category");
            var enumerator        = operationResponse.GetEnumerator();

            Assert.IsTrue(enumerator.MoveNext());
            var category = (SimpleNorthwind.Category)enumerator.Current;

            Assert.IsNotNull(category);
            category.ID.Should().Be(0);
            Assert.IsFalse(enumerator.MoveNext());
        }
        public void BindTwoEntitiesSaveChanges()
        {
            var odataRequestMessage = new ODataTestMessage();
            var odataResponseMessage = new ODataTestMessage(){StatusCode = 204};
            DataServiceContextWithCustomTransportLayer context = new DataServiceContextWithCustomTransportLayer(DataServiceProtocolVersion.V3, () => odataRequestMessage, () => odataResponseMessage);

            WritingEntityReferenceLinkArgs args = null;
            context.Configurations.RequestPipeline.OnEntityReferenceLink((r => args = r));

            SimpleNorthwind.Product product = new SimpleNorthwind.Product() {ID = 1};
            SimpleNorthwind.Supplier supplier = new SimpleNorthwind.Supplier() {ID = 2};

            context.AttachTo("Products", product);
            context.AttachTo("Suppliers", supplier);

            context.SetLink(product, "Supplier", supplier);
            context.SaveChanges();

            // Note writing entity links is not triggered here as its not in the payload
            args.Should().BeNull();
            odataRequestMessage.Method.Should().Be("PUT");
            odataRequestMessage.Url.AbsoluteUri.Should().Be("http://somedummyuri/myService.svc/Products(1)/$links/Supplier");
        }
        public void LoadCategoryPropertyTest()
        {
            const string payload = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
<entry xml:base=""http://services.odata.org/OData/OData.svc/"" xmlns:d=""http://docs.oasis-open.org/odata/ns/data"" xmlns:m=""http://docs.oasis-open.org/odata/ns/metadata"" xmlns=""http://www.w3.org/2005/Atom"">
  <id>http://services.odata.org/OData/OData.svc/Categories(0)</id>
  <title type=""text"">Food</title>
  <updated>2012-11-15T16:42:58Z</updated>
  <author>
    <name />
  </author>
  <link rel=""edit"" title=""Category"" href=""Categories(0)"" />
  <link rel=""http://docs.oasis-open.org/odata/ns/related/Products"" type=""application/atom+xml;type=feed"" title=""Products"" href=""Categories(0)/Products"" />
  <category term=""#ODataDemo.Category"" scheme=""http://docs.oasis-open.org/odata/ns/scheme"" />
  <content type=""application/xml"">
    <m:properties>
      <d:ID m:type=""Edm.Int32"">0</d:ID>
      <d:Name>Food</d:Name>
    </m:properties>
  </content>
</entry>";

            var context = CreateTransportLayerContext(payload);
            var p = new SimpleNorthwind.Product() { ID = 0 };
            context.AttachTo("Products", p);
            var operationResponse = context.LoadProperty(p, "Category");
            var enumerator = operationResponse.GetEnumerator();
            Assert.IsTrue(enumerator.MoveNext());
            var category = (SimpleNorthwind.Category)enumerator.Current;
            Assert.IsNotNull(category);
            category.ID.Should().Be(0);
            Assert.IsFalse(enumerator.MoveNext());
        }