IT tutorials
 
Technology
 

Windows Phone 8 : Databases and Storage (part 4) - Local Databases - Optimizing the Context Class

8/15/2013 11:28:17 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

3.2 Optimizing the Context Class

Although the context class will track your objects, you can help the context class by ensuring that your table classes support the INotifyPropertyChanging and INotifyPropertyChanged interfaces. Implementing these interfaces has the additional benefit of assisting with data binding in XAML. Therefore, it is recommended that all your table classes support this interface, like so:

[Table]
public class Game : INotifyPropertyChanging, INotifyPropertyChanged
{
  // ...

  public event PropertyChangingEventHandler PropertyChanging;

  public event PropertyChangedEventHandler PropertyChanged;

  void RaisePropertyChanged(string propName)
  {
    if (PropertyChanged != null)
    {
      PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
  }

  void RaisePropertyChanging(string propName)
  {
    if (PropertyChanging != null)
    {
      PropertyChanging(this,
                       new PropertyChangingEventArgs(propName));
    }
  }
}

Implementing both interfaces will add the PropertyChanging and PropertyChanged events to your class. As seen here, creating a simple helper method to raise these events is a common practice. Now that the interfaces are implemented, you have to use them. This involves calling the helper methods in each property setter. The original Game class used automatic properties to expose the columns, but because you need to call the helper method, you need standard properties:

[Table]
public class Game : INotifyPropertyChanged
{
  int _id;

  [Column(IsPrimaryKey = true, IsDbGenerated = true)]
  public int Id
  {
    get { return _id; }
    set
    {
      RaisePropertyChanging("Id");
      _id= value;
      RaisePropertyChanged("Id");
    }
  }

  string _name;

  [Column]
  public string Name
  {
    get { return _name; }
    set
    {
      RaisePropertyChanging("Name");
      _name = value;
      RaisePropertyChanged("Name");
    }
  }

  DateTime? _releaseDate;

  [Column]
  public DateTime? ReleaseDate
  {
    get { return _releaseDate; }
    set
    {
      RaisePropertyChanging("ReleaseDate");
      _releaseDate = value;
      RaisePropertyChanged("ReleaseDate");
    }
  }

  double? _price;

  [Column]
  public double? Price
  {
    get { return _price; }
    set
    {
      RaisePropertyChanging("Price");
      _price = value;
      RaisePropertyChanged("Price");
    }
  }

  // ...
}

You should notice that each property now has a backing field member (for example, _id for the Id property) and calls the RaisePropertyChanging and RaisePropertyChanged methods with the name of the property when the setter is called. By using these interfaces, the memory footprint of the context object is much smaller because it uses these interfaces to monitor changes.

In addition to these interfaces, you can improve the size of your update and delete queries by including a version member of your class:

[Table]
public class Game : INotifyPropertyChanging, INotifyPropertyChanged
{
  // ...

  [Column(IsVersion = true)]
  private Binary _version;
}

The version column (IsVersion = true) is optional but will improve the performance of change tracking when using database data. The version must be of type Binary from the System.Data.Linq namespace. It can be a private field (so it’s not visible to users) but does need to be marked as IsVersion = true for LINQ to SQL to consider it the version column.


Performance Recommendation

Your table classes should support a primary key column and a version column and should implement the INotifyPropertyChanging and INotifyPropertyChanged interfaces to be as efficient as possible in your database access code.


Finally, if your database is only performing queries, you can tell the context class that you do not want to monitor any change management. You would accomplish this by setting the context class’s ObjectTrackingEnabled property to false, like so:

using (var ctx = new AppContext())
{
  ctx.ObjectTrackingEnabled = false;

  var qry = from g in ctx.Games
            where g.Price < 19.99
            orderby g.ReleaseDate descending
            select g;

  var results = qry.ToList();
}

By disabling change management, the context object will be much more lightweight. Also, because the context is not necessary for tracking the change, you can create it locally and dispose of it when the query is complete. You usually would keep the context around for the lifetime of the page or application so that it can monitor and batch those changes back to the database, but because you are only reading from the database, the lifetime can be shortened if needed.

 
Others
 
- Windows Phone 8 : Databases and Storage (part 3) - Local Databases - Getting Started
- Windows Phone 8 : Databases and Storage (part 2) - Storage - Serialization
- Windows Phone 8 : Databases and Storage (part 1) - Storage
- Active Directory 2008 : Creating Computers and Joining the Domain (part 3) - Offline Domain Join
- Active Directory 2008 : Creating Computers and Joining the Domain (part 2) - Joining a Computer to the Domain, Secure Computer Creation and Joins
- Active Directory 2008 : Creating Computers and Joining the Domain (part 1) - The Computers Container and OUs
- Exchange Server 2010 : Using the Exchange Management Shell
- Exchange Server 2010 : Using the Exchange Management Shell - Working with Cmdlets
- Exchange Server 2010 : Using the Exchange Management Shell - Using Windows PowerShell
- Administration of Microsoft Lync Server 2010 : Topology Model
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us