Core Classes

Most commonly used classes.

  • Encapsulates an OData change set. A change set is used to group a set of entity or link changes into a single unit of work, like an atomic database transaction.

    Example using proxy classes

    open func applyChangesExample() throws -> Void {
        let service = self.service
        let suppliers = try service.fetchSuppliers(matching: DataQuery().top(2))
        let products = try service.fetchProducts(matching: DataQuery().top(3))
        let product1 = products[0].copy()
        let product2 = products[1].copy()
        let product3 = products[2].copy()
        product1.productName = "Blueberry Muffins"
        product2.productName = "Strawberry Yoghurt"
        product3.productName = "Raspberry Pie"
        let entityCreates = ChangeSet()
        entityCreates.createEntity(product1)
        entityCreates.createEntity(product2)
        entityCreates.createEntity(product3)
        try service.applyChanges(entityCreates)
        let entityChanges = ChangeSet()
        product2.productName = "Blackberry Yoghurt"
        entityChanges.updateEntity(product2)
        entityChanges.deleteEntity(product3)
        try service.applyChanges(entityChanges)
        let linkChanges = ChangeSet()
        let supplier1 = suppliers[0]
        let supplier2 = suppliers[1]
        linkChanges.createLink(from: product1, property: Product.supplier,
            to: supplier1)
        linkChanges.updateLink(from: product1, property: Product.supplier,
            to: supplier2)
        linkChanges.deleteLink(from: product1, property: Product.supplier)
        try service.applyChanges(linkChanges)
    }
    

    Example using dynamic API

    open func applyChangesExample() throws -> Void {
        let service = self.service
        let suppliersEntitySet = service.entitySet(withName: "Suppliers")
        let productsEntitySet = service.entitySet(withName: "Products")
        let productEntityType = productsEntitySet.entityType
        let productNameProperty = productEntityType.property(withName: "ProductName")
        let supplierProperty = productEntityType.property(withName: "Supplier")
        let suppliers = try service.executeQuery(DataQuery().from(suppliersEntitySet)
            .top(2))
            .entityList()
        let products = try service.executeQuery(DataQuery().from(productsEntitySet)
            .top(3))
            .entityList()
        let product1 = products.item(at: 0).copyEntity()
        let product2 = products.item(at: 1).copyEntity()
        let product3 = products.item(at: 1).copyEntity()
        productNameProperty.setStringValue(in: product1, to: "Blueberry Yoghurt")
        productNameProperty.setStringValue(in: product2, to: "Strawberry Yoghurt")
        productNameProperty.setStringValue(in: product3, to: "Raspberry Pie")
        let entityCreates = ChangeSet()
        entityCreates.createEntity(product1)
        entityCreates.createEntity(product2)
        entityCreates.createEntity(product3)
        try service.applyChanges(entityCreates)
        let entityChanges = ChangeSet()
        productNameProperty.setStringValue(in: product2, to: "Blackberry Yoghurt")
        entityChanges.updateEntity(product2)
        entityChanges.deleteEntity(product3)
        try service.applyChanges(entityChanges)
        let linkChanges = ChangeSet()
        let supplier1 = suppliers.item(at: 0)
        let supplier2 = suppliers.item(at: 1)
        linkChanges.createLink(from: product1, property: supplierProperty,
            to: supplier1)
        linkChanges.updateLink(from: product1, property: supplierProperty,
            to: supplier2)
        linkChanges.deleteLink(from: product1, property: supplierProperty)
        try service.applyChanges(linkChanges)
    }
    
    See more

    Declaration

    Swift

    open class ChangeSet: ObjectBase
  • Encapsulates an OData data service. The generic type parameter indicates the type of data service provider.

    See also

    DataQuery.

    Example using proxy classes

    open func dataServiceExample() throws -> Void {
        let provider = OnlineODataProvider(serviceName: "NorthwindService",
            serviceRoot: "http://services.odata.org/V4/Northwind/Northwind.svc/")
        let service = NorthwindService(provider: provider)
        // Alternate using generic DataService - allows provider-specific functions
        // and properties (e.g. traceRequests) to be accessed via `service` object.
        // let service: NorthwindService<OnlineODataProvider> = new NorthwindService(provider: provider)
        let query = DataQuery()
            .select(Customer.customerID, Customer.companyName, Customer.contactName)
            .orderBy(Customer.companyName)
        let customers = try service.fetchCustomers(matching: query)
        self.showCustomers(customers)
        let customer = customers.first!.copy()
        customer.companyName = "Created Inc."
        try service.createEntity(customer)
        customer.companyName = "Updated Inc."
        try service.updateEntity(customer)
        try service.deleteEntity(customer)
    }
    

    Example using dynamic API

    open func dataServiceExample() throws -> Void {
        let provider = OnlineODataProvider(serviceName: "NorthwindService",
            serviceRoot: "http://services.odata.org/V4/Northwind/Northwind.svc/")
        let service = AnyDataService(provider: provider)
        // Alternate using generic DataService - allows provider-specific functions
        // and properties (e.g. traceRequests) to be accessed via `service` object.
        // let service: DataService<OnlineODataProvider> = new DataService(provider: provider)
        let customersEntitySet = service.entitySet(withName: "Customers")
        let customerEntityType = customersEntitySet.entityType
        let customerIDProperty = customerEntityType.property(withName: "CustomerID")
        let companyNameProperty = customerEntityType.property(withName: "CompanyName")
        let contactNameProperty = customerEntityType.property(withName: "ContactName")
        let query = DataQuery()
            .select(customerIDProperty, companyNameProperty, contactNameProperty)
            .from(customersEntitySet).orderBy(companyNameProperty)
        let customers = try service.executeQuery(query).entityList()
        self.showCustomers(customers)
        let customer = customers.first().copyEntity()
        companyNameProperty.setStringValue(in: customer, to: "Created Inc.")
        try service.createEntity(customer)
        companyNameProperty.setStringValue(in: customer, to: "Updated Inc.")
        try service.updateEntity(customer)
        try service.deleteEntity(customer)
    }
    
    See more

    Declaration

    Swift

    open class DataService<Provider: DataServiceProvider>
  • A data service provider for online OData.

    See more

    Declaration

    Swift

    open class OnlineODataProvider: ObjectBase, DataServiceProvider
  • Encapsulates an OData batch request.

    See also

    ChangeSet, DataQuery, DataService.processBatch.

    Example using dynamic API

    open func processBatchExample() throws -> Void {
        defer {
            DebugConsole.traceOut("example.NorthwindClient.processBatchExample")
        }
        do {
            DebugConsole.traceIn("example.NorthwindClient.processBatchExample")
            do {
                let service = self.service
                let suppliersEntitySet = service.entitySet(withName: "Suppliers")
                let productsEntitySet = service.entitySet(withName: "Products")
                let supplierEntityType = suppliersEntitySet.entityType
                let companyNameProperty = supplierEntityType.property(withName: "CompanyName")
                let productEntityType = productsEntitySet.entityType
                let productNameProperty = productEntityType.property(withName: "ProductName")
                let supplierProperty = productEntityType.property(withName: "Supplier")
                let supplier1 = try service.executeQuery(DataQuery()
                    .from(suppliersEntitySet).top(1))
                    .requiredEntity()
                let supplier2 = supplier1.copyEntity()
                let supplier3 = supplier1.copyEntity()
                let supplier4 = supplier1.copyEntity()
                companyNameProperty.setStringValue(in: supplier2, to: "Alpha Inc.")
                companyNameProperty.setStringValue(in: supplier3, to: "Beta Inc.")
                try service.createEntity(supplier2)
                try service.createEntity(supplier3)
                companyNameProperty.setStringValue(in: supplier3, to: "Gamma Inc.")
                let product1 = try service.executeQuery(DataQuery()
                    .from(productsEntitySet).top(1))
                    .requiredEntity()
                let product2 = product1.copyEntity()
                productNameProperty.setStringValue(in: product2, to: "Delta Cake")
                let batch = RequestBatch()
                let changes = ChangeSet()
                changes.createEntity(supplier4)
                changes.updateEntity(supplier3)
                changes.deleteEntity(supplier2)
                changes.createEntity(product2)
                changes.createLink(from: product2, property: supplierProperty,
                    to: supplier4)
                let query = DataQuery().from(suppliersEntitySet)
                batch.addChanges(changes)
                batch.addQuery(query)
                try service.processBatch(batch)
                let suppliers = try batch.queryResult(for: query).entityList()
                try Example.show("There are now ",
                    Example.formatInt(suppliers.length), " suppliers.")
            }
        }
    }
    

    Example using proxy classes

    open func processBatchExample() throws -> Void {
        defer {
            DebugConsole.traceOut("example.NorthwindProxyClient.processBatchExample")
        }
        do {
            DebugConsole.traceIn("example.NorthwindProxyClient.processBatchExample")
            do {
                let service = self.service
                let supplier1 = try service.fetchSupplier(matching: DataQuery()
                    .top(1))
                let supplier2 = supplier1.copy()
                let supplier3 = supplier1.copy()
                let supplier4 = supplier1.copy()
                supplier2.companyName = "Alpha Inc."
                supplier3.companyName = "Beta Inc."
                try service.createEntity(supplier2)
                try service.createEntity(supplier3)
                supplier3.companyName = "Gamma Inc."
                let product1 = try service.fetchProduct(matching: DataQuery().top(1))
                let product2 = product1.copy()
                product2.productName = "Delta Cake"
                let batch = RequestBatch()
                let changes = ChangeSet()
                changes.createEntity(supplier4)
                changes.updateEntity(supplier3)
                changes.deleteEntity(supplier2)
                changes.createEntity(product2)
                changes.createLink(from: product2, property: Product.supplier,
                    to: supplier4)
                let query = DataQuery()
                    .from(NorthwindServiceMetadata.EntitySets.suppliers)
                batch.addChanges(changes)
                batch.addQuery(query)
                try service.processBatch(batch)
                let suppliers = try Supplier.array(from: batch.queryResult(for: query)
                    .entityList())
                try Example.show("There are now ", Example.formatInt(suppliers.count),
                    " suppliers.")
            }
        }
    }
    
    See more

    Declaration

    Swift

    open class RequestBatch: ObjectBase
  • Encapsulates an OData query request. A query can be defined by setting query properties, or by calling query functions which provide a fluent interface.

    Example using proxy classes

    open func dataQueryExample() throws -> Void {
        defer {
            DebugConsole.traceOut("example.NorthwindProxyClient.dataQueryExample")
        }
        do {
            DebugConsole.traceIn("example.NorthwindProxyClient.dataQueryExample")
            do {
                let service = self.service
                let query = DataQuery()
                    .select(Customer.customerID, Customer.companyName,
                        Customer.contactName)
                    .filter(Customer.address.toLower().contains("king"))
                    .orderBy(Customer.companyName)
                let customers = try service.fetchCustomers(matching: query)
                self.showCustomers(customers)
            }
        }
    }
    

    Example using dynamic API

    open func dataQueryExample() throws -> Void {
        defer {
            DebugConsole.traceOut("example.NorthwindClient.dataQueryExample")
        }
        do {
            DebugConsole.traceIn("example.NorthwindClient.dataQueryExample")
            do {
                let service = self.service
                let customersEntitySet = service.entitySet(withName: "Customers")
                let customerEntityType = customersEntitySet.entityType
                let customerIDProperty = customerEntityType.property(withName: "CustomerID")
                let companyNameProperty = customerEntityType.property(withName: "CompanyName")
                let contactNameProperty = customerEntityType.property(withName: "ContactName")
                let addressProperty = customerEntityType.property(withName: "Address")
                let query = DataQuery()
                    .select(customerIDProperty, companyNameProperty,
                        contactNameProperty)
                    .from(customersEntitySet).filter(addressProperty.toLower()
                        .contains("king"))
                    .orderBy(companyNameProperty)
                let customers = try service.executeQuery(query).entityList()
                self.showCustomers(customers)
            }
        }
    }
    
    See more

    Declaration

    Swift

    open class DataQuery: ObjectBase