Browse DevX
Sign up for e-mail newsletters from DevX


ADO.NET vNext Part 2: Using the Object Services Layer : Page 2

The Entity Data Model and ADO.NET vNext let you deal with tabular data as objects, eliminating much of the effort endemic to older data-retrieval and modification code.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Key Capabilities of ObjectContext
As mentioned before, the ObjectContext is a key object in the object services layer that acts as the single point of contact for the client applications to interact with the data store. Basically, ObjectContext acts as a higher level container that manages and tracks instances. Specifically, it:

  • Keeps track of every object retrieved through a query.
  • Tracks the original versions of the values used to populate each object. This allows the system to perform optimistic concurrent checks against the database during updates.
  • Tracks objects that have been modified so that it knows which entities need to be updated in the store when you invoke the SaveChanges() method.
Passing Parameters to Entity SQL
In this section, I'll build on the previous example to demonstrate how to pass parameters to the Entity SQL through the object services layer. For example, suppose you want to query the Product table in the AdventureWorks, supplying the ProductID as an input parameter to retrieve a specific product.

private void btnGetProductDetails_Click (object sender, EventArgs e) { string connectionString = Properties.Settings.Default.AdventureWorksConnectionString; ObjectContext context = new ObjectContext(connectionString, "AdventureWorksModel.AdventureWorks"); Query<Product> products = context.GetQuery<Product> ("SELECT VALUE p FROM " + " AdventureWorksModel.AdventureWorks.Product " " AS p Where p.ProductID = @ProductID", new QueryParameter("ProductID", Convert.ToInt32(txtProductID.Text))); lstProductDetails.Items.Clear(); foreach (Product prod in products) { lstProductDetails.Items.Add(prod.ProductID); lstProductDetails.Items.Add(prod.Name); lstProductDetails.Items.Add(prod.ProductNumber); } }

Figure 3. Selecting Details of a Specific Product using Parameters: You can execute Entity SQL query with parameters by creating an instance of the QueryParameter object with appropriate arguments passed to its constructor.
The use of the QueryParameter object is a key point to note in the above code. You populate it with the name of the parameter and the parameter value through its constructor. After creating a QueryParameter object, you pass it to the ObjectContext.GetQuery() method as the second argument.

The user enters the value for the ProductID parameter in the TextBox named txtProductID. Once you execute the query, the code stores the results in a variable of type Query<Product>, which is a collection the last part of the code loops through to display the results in the list box (see Figure 3).

You can see that most of the plumbing code has been eliminated, and only the code that represents the application intent remains. At this point most of the impedance mismatch problem mentioned earlier has been eliminated. This is a key goal of the ADO.NET Entity Framework.

Updating Data
Usually when you want to make a change to the database, you issue INSERT, UPDATE and DELETE statements against the database. In most modern applications, a custom object layer that maps nicely to the entities in the database abstracts these statements. Now; however, the object services layer handles all the underlying complexities for you. The object services layer makes this possible by leveraging the entity metadata stored by the ADO.NET entity framework.

Figure 4. Updating the List Price of All Products: To perform batch updates, you loop through all the products, update each of them and finally commit changes through the ObjectContext.SaveChanges() method call.
As an example, consider a scenario where you want to increase the price of all the products in the Product table by a specific amount entered by the user (see Figure 4).

private void btnIncreaseThePrice_Click( object sender, EventArgs e) { string connectionString = Properties.Settings.Default. AdventureWorksConnectionString; ObjectContext context = new ObjectContext( connectionString, "AdventureWorksModel.AdventureWorks"); Query<Product> products = context.GetQuery<Product>( "SELECT VALUE p FROM " + "AdventureWorksModel.AdventureWorks." + "Product AS p"); foreach (Product prod in products) { prod.ListPrice += Convert.ToInt32(txtPrice.Text); } context.SaveChanges(); MessageBox.Show("Product Price successfully updated"); }

The first few lines of the preceding code are very similar to the previous example—it executes a simple Entity SQL to get a list of products from the AdventureWorks database. However, after retrieving the list, the code becomes more interesting when it updates the list price of all the products.

Query<Product> products = context.GetQuery<Product>( "SELECT VALUE p FROM AdventureWorksModel.AdventureWorks." + "Product AS p");

To update the list price of all the products, you loop through all the products contained in the Query<Product> object and increase the list price of each of the products by the value entered in the txtPrice text box.

foreach (Product prod in products) { prod.ListPrice += Convert.ToInt32(txtPrice.Text); }

Now you need to commit the latest changes in the database, which you can accomplish with one line of code by simply calling the ObjectContext.SaveChanges() method.


When you work with objects through the object services layer, it retrieves object instances from the cache and makes changes to them in-memory. When you invoke the SaveChanges() method, it pushes the in-memory changes to the database store.

Thanks for your registration, follow us on our social networks to keep up-to-date