Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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

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.


advertisement
Inserting Data
Now that you have looked at the code required to perform read and update operations through the object services database, here's an example showing how to insert data that uses the ProductCategory table in the AdventureWorks database.

private void btnAddCategory_Click(object sender, EventArgs e) { string connectionString = Properties.Settings.Default.AdventureWorksConnectionString; ObjectContext context = new ObjectContext(connectionString, "AdventureWorksModel.AdventureWorks"); ProductCategory category = new ProductCategory(); category.Name = txtCategoryName.Text ; category.ModifiedDate = DateTime.Now; category.rowguid = System.Guid.NewGuid(); context.AddObject(category); context.SaveChanges(); MessageBox.Show("Product Category successfully added"); }

The preceding code creates a new ProductCategory object instance, setting its properties to appropriate values. Then it invokes the ObjectContext.AddObject() method to add the ProductCategory object to the current object context.

context.AddObject(category);

To persist the inserted values to the database, you then invoke the ObjectContext.SaveChanges() method.



context.SaveChanges();

Figure 5 shows the output generated by the Windows form in the downloadable sample application.

 
Figure 5. Inserting a New Product Category: To insert a new product category, you simply create a ProductCategory object, populate its properties, invoke the ObjectContext.AddObject() method and finally persist the changes through the ObjectContext.SaveChanges() method.
A Missing Operation
Astute readers will have noticed that I haven't covered the DELETE operation. There is a DeleteObject method available (seen through Intellisense) but it requires a bunch of arguments (such as a MetadataWorkspace object and so on). I tried passing various combinations of values to get that to work, but it never did work. Unfortunately, there is no documentation on that method to see exactly what needs to be passed and in what format. Perhaps DELETE operations will become clearer in a later CTP; if so, I will cover that in a future article.

Even without that, however, you've seen how the object services layer simplifies working with data, letting you make modifications to .NET objects rather than performing detailed operations against tabular data. To recap, the main advantages are:

  • By using the object services layer, you can delegate the management of persistence, and to work at state management to the ADO.NET entity framework, and focus mainly on the core business logic of your application.
  • By representing entities as a set of objects, you work with objects that directly represent the domain model, and not with data structures in the same format as the relational database (like DataReaders).
  • The object services layer provides a high quality, flexible data access layer that reduces testing and debugging efforts.
As examples, you saw how to perform READ, INSERT, and UPDATE operations through the object services layer. The next installment in this series will focus on the intersection between LINQ (Language INtegrated Query) and ADO.NET vNext Framework.



Thiru Thangarathinam works at Intel Corporation in Chandler, Arizona. He's a Microsoft MVP who specializes in architecting, designing, and developing distributed enterprise-class applications using .NET-related technologies. He is the author of the books "Professional ASP.NET 2.0 XML" and "Professional ASP.NET 2.0 Databases" from Wrox press and has coauthored a number of books on .NET-related technologies. He is a frequent contributor to leading technology-related online publications.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap