,

LINQ to SQL on the Phone

Retrieval and storage of data in a local database is done entirely using LINQ to SQL, which provides object-relational mapping (ORM) capabilities, allowing you to use LINQ to communicate with a relational database. LINQ to SQL maps the object model, which you define by decorating entity classes with attributes, to a relational database. When your app runs, LINQ to SQL translates language-integrated queries into Transact-SQL and then sends the queries to the database for execution. When the database returns the results, LINQ to SQL translates the results back to objects.

When first learning of Microsoft’s choice to go with LINQ to SQL in Windows Phone, I was somewhat dismayed that it was chosen over the Entity Framework. Yet, while the Entity Framework offers a more powerful API and set of tools, it is not optimized for mobile devices. I have since come to see LINQ to SQL as a good fit for the phone, because it offers a simple API for rapid data layer construction.

LINQ to SQL makes heavy use of the System.Data.Linq.DataContext class, which serves as a proxy to your database, allowing you to query the database via Table objects representing tables in the database (see Figure 29.2). Each Table member of a DataContext allows the storage and retrieval of entity objects; an instance of an entity represents a table row. Persisting changes to the local database is done using the DataContext.SubmitChanges method.

Image

FIGURE 29.2 DataContext Table members are associated with database tables.

By default, DataContext also tracks changes to your entity objects so that you can easily push changes back to your database.

An entity is a POCO (Plain Old CLR Object), representing a domain object, and is decorated with a System.Data.Linq.Mapping.Table attribute. Other data related attributes, placed on and within each entity class, define the mapping between the object model and the schema of the database, and determine the structure of the database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.123.227