15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

880 ❘ ChaPTer 31 AdO.net entity frAmewOrk<br />

object Tracking<br />

To allow data read from the store to be modified <strong>and</strong> saved, the entities must be tracked after they are<br />

loaded. This also requires that the object context be aware if an entity has already been loaded from<br />

the store. If multiple queries are accessing the same records, the object context needs to return already<br />

loaded entities.<br />

The ObjectStateManager is used by the object context to keep track of entities that are loaded<br />

into the context.<br />

The following example demonstrates that indeed if two different queries are done that return the same<br />

record from the database, the state manager is aware of that <strong>and</strong> does not create a new entity. Instead, the<br />

same entity is returned. The ObjectStateManager instance that is associated with the object context can be<br />

accessed with the ObjectStateManager property. The ObjectStateManager class defines an event named<br />

ObjectStateManagerChanged that is invoked every time a new object is added or removed from the object<br />

context. Here, the method ObjectStateManager_ObjectStateManagerChanged is assigned to the event to<br />

get information about changes.<br />

Two different queries are used to return an entity object. The first query gets the first racer from the country<br />

Austria with the last name Lauda. The second query asks for the racers from Austria, sorts the racers by<br />

the number of races won, <strong>and</strong> gets the first result. As a matter of fact, that’s the same racer. To verify that the<br />

same entity object is returned, the method Object.ReferenceEquals() is used to verify whether the two<br />

object references indeed reference the same instance:<br />

private static void TrackingDemo()<br />

{<br />

using (var data = new Formula1Entities())<br />

{<br />

data.ObjectStateManager.ObjectStateManagerChanged +=<br />

ObjectStateManager_ObjectStateManagerChanged;<br />

Racer niki1 = data.Racers.Where("it.Country='Austria' && it.Lastname='Lauda'").<br />

First();<br />

Racer niki2 = data.Racers.Where("it.Country='Austria'").<br />

OrderBy("it.Wins DESC").First();<br />

if (Object.ReferenceEquals(niki1, niki2))<br />

{<br />

Console.WriteLine("the same object");<br />

}<br />

}<br />

}<br />

static void ObjectStateManager_ObjectStateManagerChanged(object sender,<br />

CollectionChangeEventArgs e)<br />

{<br />

Console.WriteLine("Object State change — action: {0}", e.Action);<br />

Racer r = e.Element as Racer;<br />

if (r != null)<br />

Console.WriteLine("Racer {0}", r.Lastname);<br />

}<br />

code snippet Formula1Demo/Program.cs<br />

Running the application, you can see that the event of the ObjectStateManagerChanged of the<br />

ObjectStateManager occurs only once, <strong>and</strong> the references niki1 <strong>and</strong> niki2 are indeed the same:<br />

Object State change — action: Add<br />

Racer Lauda<br />

The same object<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!