Skip to content

A Comparison of Proxy Classes and Dynamic API

The following code samples are based on the OData Sample Service.

Getting an Entity List

void entityListExample() {
    DataQuery query = new DataQuery()
            .select(Customer.customerID, Customer.city, Customer.country)
            .orderBy(Customer.country);
    List<Customer> customers = espmContainer.getCustomers(query);
    for (Customer customer : customers) {
        this.showCustomer(customer);
    }
}
fun entityListExample() {
    val query = DataQuery()
            .select(Customer.customerID, Customer.city, Customer.country)
            .orderBy(Customer.country)
    val customers = espmContainer.getCustomers(query)
    for (customer in customers) {
        this.showCustomer(customer)
    }
}

Dynamic API for Query

void entityListExample() {
    EntitySet s_customers = espmContainer.getEntitySet("Customers");
    EntityType t_customer = s_customers.getEntityType();
    Property p_customerID = t_customer.getProperty("CustomerID");
    Property p_city = t_customer.getProperty("City");
    Property p_country = t_customer.getProperty("Country");
    DataQuery query = new DataQuery()
            .select(p_customerID, p_city, p_country)
            .from(s_customers)
            .orderBy(p_country);
    EntityValueList customers = espmContainer.executeQuery(query).getEntityList();
    for (EntityValue customer : customers) {
        this.showCustomer(customer);
    }
}
fun entityListExample() {
    val s_customers = espmContainer.getEntitySet("Customers")
    val t_customer = s_customers.entityType
    val p_customerID = t_customer.getProperty("CustomerID")
    val p_city = t_customer.getProperty("City")
    val p_country = t_customer.getProperty("Country")
    val query = DataQuery()
            .select(p_customerID, p_city, p_country)
            .from(s_customers)
            .orderBy(p_country)
    val customers = espmContainer.executeQuery(query).entityList
    for (customer in customers) {
        this.showCustomer(customer)
    }
}

Creating an Entity

Proxy Classes for Creation

void createEntityExample() {
    Customer customer = new Customer();
    customer.setCustomerID(GuidValue.random().toString());
    customer.setCity("London");
    customer.setCountry("GB");
    espmContainer.createEntity(customer);
}
fun createEntityExample() {
    val customer = Customer()
    customer.customerID = GuidValue.random().toString()
    customer.city = "London"
    customer.country = "GB"
    espmContainer.createEntity(customer)
}

Dynamic API for Creation

void createEntityExample() {
    EntitySet s_customers = espmContainer.getEntitySet("Customers");
    EntityType t_customer = s_customers.getEntityType();
    Property p_customerID = t_customer.getProperty("CustomerID");
    Property p_city = t_customer.getProperty("City");
    Property p_country = t_customer.getProperty("Country");
    EntityValue customer = EntityValue.ofType(t_customer);
    p_customerID.setString(customer, GuidValue.random().toString());
    p_city.setString(customer, "London");
    p_country.setString(customer, "GB");
    espmContainer.createEntity(customer);
}
fun createEntityExample() {
    val s_customers = espmContainer.getEntitySet("Customers")
    val t_customer = s_customers.entityType
    val p_customerID = t_customer.getProperty("CustomerID")
    val p_city = t_customer.getProperty("City")
    val p_country = t_customer.getProperty("Country")
    val customer = EntityValue.ofType(t_customer)
    p_customerID.setString(customer, GuidValue.random().toString())
    p_city.setString(customer, "London")
    p_country.setString(customer, "GB")
    espmContainer.createEntity(customer)
}

Updating an Entity

void updateEntityExample() {
    DataQuery query = new DataQuery()
        .top(1)
        .filter(Customer.city.equal("London"));
    List<Customer> customers = espmContainer.getCustomers(query);
    Customer customer = customers.get(0);
    customer.setCity("Liverpool");
    espmContainer.updateEntity(customer);
}
fun updateEntityExample() {
    val query = DataQuery()
            .top(1)
            .filter(Customer.city.equal("London"))
    val customers = espmContainer.getCustomers(query)
    val customer = customers[0]
    customer.city = "Liverpool"
    espmContainer.updateEntity(customer)
}

Dynamic API for Update

void updateEntityExample() {
    EntitySet s_customers = espmContainer.getEntitySet("Customers");
    EntityType t_customer = s_customers.getEntityType();
    Property p_city = t_customer.getProperty("City");
    DataQuery query = new DataQuery()
        .top(1)
        .from(s_customers)
        .filter(p_city.equal("London"));
    EntityValue customer = espmContainer.executeQuery(query)
        .getRequiredEntity();
    p_city.setString(customer, "Liverpool");
    espmContainer.updateEntity(customer);
}
fun updateEntityExample() {
    val s_customers = espmContainer.getEntitySet("Customers")
    val t_customer = s_customers.entityType
    val p_city = t_customer.getProperty("City")
    val query = DataQuery()
            .top(1)
            .from(s_customers)
            .filter(p_city.equal("London"))
    val customer = espmContainer.executeQuery(query).requiredEntity
    p_city.setString(customer, "Liverpool")
    espmContainer.updateEntity(customer)
}

Deleting an Entity

void deleteEntityExample() {
    DataQuery query = new DataQuery()
        .top(1)
        .selectKey()
        .filter(Customer.city.equal("Liverpool"));
    List<Customer> customers = espmContainer.getCustomers(query);
    Customer customer = customers.get(0);
    espmContainer.deleteEntity(customer);
}
fun deleteEntityExample() {
    val query = DataQuery()
            .top(1)
            .selectKey()
            .filter(Customer.city.equal("Liverpool"))
    val customers = espmContainer.getCustomers(query)
    val customer = customers[0]
    espmContainer.deleteEntity(customer)
}

Dynamic API for Deletion

void deleteEntityExample() {
    EntitySet s_customers = espmContainer.getEntitySet("Customers");
    EntityType t_customer = s_customers.getEntityType();
    Property p_city = t_customer.getProperty("City");
    DataQuery query = new DataQuery()
        .top(1)
        .selectKey()
        .from(s_customers)
        .filter(p_city.equal("Liverpool"));
    EntityValue customer = espmContainer.executeQuery(query).getRequiredEntity();
    espmContainer.deleteEntity(customer);
}
fun deleteEntityExample() {
    val s_customers = espmContainer.getEntitySet("Customers")
    val t_customer = s_customers.entityType
    val p_city = t_customer.getProperty("City")
    val query = DataQuery()
            .top(1)
            .selectKey()
            .from(s_customers)
            .filter(p_city.equal("Liverpool"))
    val customer = espmContainer.executeQuery(query).requiredEntity
    espmContainer.deleteEntity(customer)
}

Last update: October 20, 2020