Twitter DataContext

,

The TwitterDataContext class is the proxy to the Twitter local database and includes two public Table<TSource> properties: TwitterUsers and TimelineItems (see Listing 29.3).

The assignment of the DataContext.Log property allows you to monitor the activities of the DataContext at runtime, and in particular, to observe the generated SQL as it is being sent to the database. This customized logging is examined later in the chapter.

The GetTable property of the DataContext class retrieves a Table<TEntity> object representing a table in the underlying database. Table objects can be queried using LINQ to SQL.

LISTING 29.3. TwitterDataContext Class


public class TwitterDataContext : DataContext
{
    public TwitterDataContext(string connection) : base(connection)
    {
        Log = new DebugStreamWriter();
    }

    public Table<TwitterUser> TwitterUsers
    {
        get
        {
            return GetTable<TwitterUser>();
        }
    }

    public Table<TimelineItem> TimelineItems
    {
        get
        {
            return GetTable<TimelineItem>();
        }
    }

}


The Table<TEntity> class allows you to perform CRUD (create, read, update, delete) operations on a table. For example, you can insert a row (representing an entity) into the underlying table using the Table.InsertOnSubmit(TEntity entity) method.

The Table<TEntity> class also allows you to attach a disconnected (or detached) entity to a new DataContext.

As an aside, the DataContext base class automatically initializes its public fields of type Table<TEntity>. By placing a public field in a DataContext derived class, such as the following, the field is automatically assigned when the DataContext is instantiated:

public Table<TwitterUser>;

Although this offers a way of exposing Table objects from your DataContext using less code, the use of public fields is not recommended because it breaks the convention that fields should have private visibility; nonprivate fields violate the encapsulation of the class and make the class less amenable to change.

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

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