The TwitterUser
class contains various properties, such as the screen name of the Twitter user. It also includes a collection of TimelineItem
objects that are maintained using an EntitySet
. An EntitySet
provides for deferred loading and manages the relationship of the collection side of one-to-many relationships between entities.
The TwitterUser
constructor initializes the EntitySet
, specifying two handlers: one that is called when a TimelineItem
is added to its collection; the other when a TimelineItem
is removed (see Listing 29.2).
The TwitterUser
class also uses the Index
attribute, which tells the DataContext
to create a database index for the secondary column ScreenName.
Indexes improve the speed of data retrieval operations by allowing the database engine to quickly locate a record. Without an index, potentially all rows in a table need to be scanned, an expensive O(n) operation.1
1. O(n) describes algorithm complexity. For more information on Big O notation see http://en.wikipedia.org/wiki/Big_O_notation.
[Table]
[Index(Columns = "ScreenName")]
public class TwitterUser : NotifyPropertyChangeBase
{
public TwitterUser()
{
timelineItems = new EntitySet<TimelineItem>(
AttachTimelineItem, DetachTimelineItems);
}
string id;
[Column(IsPrimaryKey = true)]
public string Id
{
get
{
return id;
}
set
{
Assign(ref id, value);
}
}
string screenName;
[Column]
public string ScreenName
{
get
{
return screenName;
}
set
{
Assign(ref screenName, value);
}
}
... (ImageUrl and Description properties omitted)
readonly EntitySet<TimelineItem> timelineItems;
[Association(
Storage = "timelineItems",
OtherKey = "TwitterUserId")]
public EntitySet<TimelineItem> TimelineItems
{
get
{
return timelineItems;
}
set
{
timelineItems.Assign(value);
}
}
void AttachTimelineItem(TimelineItem entity)
{
OnPropertyChanging("TimelineItems", timelineItems, timelineItems);
entity.TwitterUser = this;
OnPropertyChanged("TimelineItems");
}
void DetachTimelineItems(TimelineItem entity)
{
OnPropertyChanging("TimelineItems", timelineItems, null);
entity.TwitterUser = null;
OnPropertyChanged("TimelineItems");
}
}
The PropertyChanging
event is raised manually before the assignment of the TimelineItem.TwitterUser
property, and the PropertyChanged
event is raised after it has been assigned. These calls indicate to the change tracking infrastructure that the TwitterUser
object is dirty and differs from the stored value in the database.
3.129.148.4