-
Notifications
You must be signed in to change notification settings - Fork 4
Resources
CRUD operations are performed using the high level Get
, Post
, Put
and Delete
extension methods. These methods are easier to use than the lower level Execute
method because they automatically build the target path and extract the resource ETag if applicable.
The following example reads a single contact and outputs their full name.
var client = new SDataClient("http://example.com/sdata/slx/dynamic/-/")
{
UserName = "admin",
Password = "password"
};
var contact = await client.GetAsync<Contact>("CDEMOA000001");
Console.WriteLine(contact.FullName);
[SDataPath("contacts")]
public class Contact
{
[SDataProtocolProperty(SDataProtocolProperty.Key)]
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public string Status { get; set; }
public Address Address { get; set; }
public DateTime? ModifyDate { get; set; }
}
The SDataPath
attribute on the contact POCO specifies the path of the resource relative to the base URL of the client object. Without this attribute the resource path would need to be specified every time a query is created.
A fresh version of a resource can be requested by passing the existing resource in place of a key. If the resource contains an ETag then it'll be used to determine whether a new resource needs to be fetched.
contact = await client.GetAsync(contact);
Console.WriteLine(contact.ModifyDate);
More information on read operations can be found in section 7 of the SData specification.
The following example creates a new contact and outputs the server generate ID.
var contact = new Contact
{
FirstName = "John",
LastName = "Doe"
};
contact = await client.PostAsync(contact);
Console.WriteLine(contact.Id);
The Post
method doesn't change the input resource in any way. Instead it returns a new resource with the newly generated ID and ETag along with any changes that might have occurred during creation on the server side, such as calculated properties and audit properties.
It's also possible to create nested resources in the same request. The following example creates the same contact as above but also specifies a mailing address.
var contact = new Contact
{
FirstName = "John",
LastName = "Doe",
Address = new Address
{
Description = "Mailing",
City = "Scotsdale",
State = "AZ"
}
};
var options = new SDataPayloadOptions {Include = "Address"};
contact = await client.PostAsync(contact, null, options);
Console.WriteLine(contact.Id);
More information on create operations can be found in section 8 of the SData specification.
The following example updates a contact's status and outputs the modify date returned from the server. If the resource being updated has an ETag then it'll be used to ensure that it hasn't already been changed elsewhere.
contact.Status = "Inactive";
contact = await client.PutAsync(contact);
Console.WriteLine(contact.ModifyDate);
If the DifferentialUpdate
flag on the client object is true and the resource being updated implements IChangeTracking
then only the properties that have changes will be sent. Not only does this lighten the posted payload but it also lessens the amount of data lost if an update conflict occurs and the server doesn't support ETag concurrency. In addition to IChangeTracking
, the SDataResource
and SDataCollection
data containers also implement IRevertibleChangeTracking
which means they can undo pending changes. The following example rejects a pending contact status change and outputs the restored status value.
contact.Status = "Inactive";
contact.RejectChanges();
Console.WriteLine(contact.Status);
More information on update operations can be found in section 9 of the SData specification.
The following example deletes a contact. If the resource has an ETag then it'll be used to ensure that the resource hasn't been changed elsewhere before performing the delete.
await client.DeleteAsync(contact);
More information on delete operations can be found in section 10 of the SData specification.