Object Services Layer in Entity Framework

By FoxLearn 1/2/2025 8:22:35 AM   35
To reduce the impedance mismatch between the object model and data model in an application, you can leverage the Object Services Layer in ADO.NET Entity Framework.

An ORM (Object-Relational Mapping) tool abstracts the data access logic of an application, bridging the gap between the object model used in the application and the relational data model in the database.

What is an ORM and Why is it Important?

An application with a data-centric focus has two perspectives: the data model, which defines how data is stored in a database, and the object model, which represents the application's object-oriented structure. The data store, such as SQL Server or Oracle, represents the database, while the object model reflects the application's object-oriented programming structure.

An ORM allows you to convert data between incompatible type systems, enabling you to store domain objects in a database without needing to manage the internal details of how the data is stored.

In traditional ADO.NET, retrieving data from the database involves manually writing SQL queries and handling connections and data readers. For instance, the following code fetches the product name and price from a "Products" table for a product with a specific ID:

string queryString = "SELECT ProductName, Price FROM Products WHERE ProductID = 100";

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand sqlCommand = new SqlCommand(queryString, connection);
    connection.Open();
    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
    
    try
    {
        while (sqlDataReader.Read())
        {
            Console.WriteLine($"Product: {sqlDataReader[0]}, Price: {sqlDataReader[1]}");
        }
    }
    finally
    {
        sqlDataReader.Close();
    }
}

On the other hand, using an ORM like Entity Framework makes data retrieval simpler. You can fetch the same data with fewer lines of code:

Repository repository = new Repository();
// Some code
Product product = repository.GetProduct(100);
string productName = product.ProductName;
decimal price = product.Price;

With ORM, the complexity of database interactions is abstracted, making it easier to work with data in an object-oriented way.

The ADO.NET Entity Framework

Microsoft's Entity Framework is an extended ORM that separates the object model of an application from the underlying data model, allowing you to interact with data using domain-specific objects. It enables CRUD operations on data through the object model instead of directly dealing with the database structure.

The Entity Framework consists of three layers:

  • Conceptual Layer (C-Space): Represented by CSDL (Conceptual Data Language).
  • C-S Mapping Layer: Represented by MSL (Mapping Schema Language).
  • Storage Layer (S-Space): Represented by SSDL (Store-specific Data Language).

You can query data exposed by the Entity Data Model using:

  • EntityClient Provider with Entity SQL
  • Object Services Layer with ObjectQuery
  • LINQ to Entities

The Object Services Layer

The Object Services layer in Entity Framework sits between the EntityClient Provider and the query layer, providing the core ORM functionality. It resides in the System.Data.Objects namespace and is part of the System.Data.Entity.dll assembly. This layer simplifies data access by using an ObjectQuery instance to handle queries and process data internally.

The ObjectContext is the central component of the Object Services layer. By including the necessary namespaces (System.Data.Objects and System.Data.Objects.DataClasses), developers can leverage Object Services for various benefits, including:

  • State management, optimistic concurrency, and identity resolution.
  • Lazy loading, inheritance, and navigation relationships.
  • Query transformation, materialization, and change tracking.

The Object Services layer supports CRUD operations and querying through Entity SQL and LINQ.

using (var context = new MyAppEntities())
{
    ObjectQuery<Customer> customers = context.CreateQuery<Customer>("SELECT VALUE c FROM Customers AS c");
    foreach (var customer in customers)
    {
        Console.WriteLine($"Customer: {customer.CustomerName}");
    }
}

Here’s how you can update a product’s price in the "Products" table

using (var context = new ProductContext())
{
    var product = context.Products.First(p => p.ProductID == 101);
    product.Price = 19.99;
    context.SaveChanges();
}

In these examples, the Object Services layer streamlines querying and updating operations, making it easier to interact with the data in a more object-oriented way while minimizing the amount of code needed.