Your SlideShare is downloading. ×
  • Like
10 wp7   local database
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

10 wp7 local database

  • 3,475 views
Published

 

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
3,475
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
41
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Windows Phone 7
    Li Jingnan / Wang Tao
    2011-7-15
    1
  • 2. 2 days
    2
  • 3. ethos
    http://www.ethostechnologies.com/
    One of the 1st ISVs for cloud computing in Europe and China.
    Cloud computing development partner with Microsoft in Greater China Region.
    Invited to speak about Azure at several Microsoft events.
  • 4. about
    anytao| Ethos
    <ethos:Member
    id = “Wang Tao”
    msn = anytao@live.com
    weibo = http://weibo.com/anytao
    runat = “Senior System Architect”
    />
    Jason | Ethos
    <ethos:Member
    id = “Li Jingnan”
    msn = zengnami@hotmail.com
    weibo= http://weibo.com/jn1981
    runat = “SE”
    />
  • 5. about
    http://book.anytao.net
  • 6. 10 Local Database
    Wang Tao / 2011-07-15
  • 7. session outline
    LINQ to SQL
    LINQ to User Data
    overview
    architecture
    code-first development
    implementation details
    queries
    inserts, updates, deletes…
    database schema upgrades
    performance and best practices
    overview
    end-user consent
    supported account types
    implementation details
    querying contacts
    querying appointments
    performance and best practices
  • 8. LINQ to everything
    LINQ
    Objects
    XML
    SQL
    User Data
    OData
    7
    Mango
  • 9. complex schema
    numerous relationships and constraints
    example: shopping list
    7 tables
    100s of records
    5 foreign keys
  • 10. Reference Data
    Huge amounts of static reference data
    Example: dictionary app
    3 tables
    1 table with 500k rows
  • 11. web service cache
    Cloud Service
    fetch reference data from cloud
    cache it locally
    combine with user-specific data
    Windows Phone
    Service Cache
    User Data
  • 12. user data
    filter contacts
    birthdays in the next month
    query all appointments
    find an available time for a meeting
    Filter
  • 13. database support
  • 14. local data storage: overview
    apps store private data in Isolated Storage
    • settings and properties in the app dictionary
    • 15. unstructured data in Isolated Storage files
    • 16. structured data in database files
    App Root Folder
    Install
    Package Manager
    Creates root folder
    sandboxed to App
    DB
    Database
    File (r/o)
    App Data Folder
    Creates/Manages
    files and settings
    App
    WP7 Isolated Storage APIs
    DB
    Application
    Settings File
    Application
    Files
    Database file
  • 17. architecture
    Your App
    Custom Data Context
    App Objects
    var query = fromw indb.Wines
    wherew.Country== “USA"
    selectw.Name;
    System.Data.Linq
    Identity Management
    Change Tracking
    Update Processing
    Object Materialization
    .Call System.Linq.Queryable.Select( .Call System.Linq.Queryable.Where( .Constant(Table(Wines)), '(.Lambda #Lambda1)), '(.Lambda #Lambda2)) .Lambda #Lambda1(db.Wines $w) { $w.Country== “USA" } .Lambda #Lambda2(w.Country $w) { $w.Name}
    select Name
    from Wines
    where Country = “USA”
    Microsoft.Phone.Data.Internal
    Core ADO.NET (System.Data)
    SQL CE DB
    SQLCE ADO.NET Provider (System.Data.SqlServerCe)
  • 18. code first development
    Design time
    • Create object model: wines, varietals, vineyards, etc.
    • 19. Decorate objects with attributes for persistence
    Varietals
    Wines
    Vineyards
    WineMakers
    • Create DataContextreference to database
    • 20. Translate object model into a database file
    • 21. Submit API persists changes to DB
    Run time
    Database upgrade
    • Create new objects to enable new features
    • 22. Use upgrade APIs to change DB
  • database creation: example
    // Define the data context.
    publicpartialclassWineDataContext: DataContext
    {
    publicTable<Wine> Wines;
    publicTable<Vineyard> Vineyards;
    publicWineDataContext(stringconnection) : base(connection) { }
    }
    // Define the tables in the database
    [Table]
    publicclassWine
    {
    [Column(IsPrimaryKey=true]
    publicstringWineID{ get; set; }
    [Column]
    publicstringName { get; set; }
    ……
    }
    // Create the database form data context, using a connection string
    DataContextdb = newWineDataContext("isostore:/wineDB.sdf");
    if (!db.DatabaseExists()) db.CreateDatabase();
  • 23. queries: examples
    // Create the database form data context, using a connection string
    DataContextdb = newWineDataContext("isostore:/wineDB.sdf");
    // Find all wines currently at home, ordered by date acquired
    varq = from w indb.Wines
    wherew.Varietal.Name == “Shiraz” && w.IsAtHome == true
    orderbyw.DateAcquired
    select w;
  • 24. Inserts/Updates/Deletes
    Your App Code
    It’s all about the DataContext
    Changes made against the DataContext first
    Changes persisted by calling SubmitChanges()
    SubmitChanges
    LINQ to SQL determines change set and submits to DB
    DataContext
    DB
  • 25. inserts/updates/deletes
    update
    insert
    WinenewWine= newWine
    {
    WineID= “1768",
    Name = “Windows Phone Syrah",
    Description = “Bold and spicy"
    };
    db.Wines.InsertOnSubmit(newWine);
    db.SubmitChanges();
    Winewine=
    (fromw indb.Wines
    wherew.WineID== “1768"
    select w).First();
    wine.Description= “Hints of plum and melon";
    db.SubmitChanges();
  • 26. inserts/updates/deletes
    delete
    varvineyardsToDelete=
    fromVineyards v in db.Vineyards
    wherev.Country== “Australia”
    select v;
    db.Vineyards.DeleteAllOnSubmit
    (vineyardsToDelete);
    db.SubmitChanges();
    Foreign key constraint will cause exception here if Wines associated with the Vineyards are not deleted first
  • 27. inserts/updates/deletes
    varvineyardsToDelete= fromVineyards v indb.Vineyards
    wherev.Country== “Australia"
    select v;
    foreach (Vineyards v invineyardsToDelete)
    {
    db.Wines.DeleteAllOnSubmit(v.Wines);
    }
    db.Vineyards.DeleteAllOnSubmit(vineyardsToDelete);
    db.SubmitChanges();
  • 28. database schema upgrades
    DatabaseSchemaUpdater allows for simple upgrades on your existing DB
    It offers the ability to add
    Tables
    Columns
    Indices
    Associations/foreign keys
    All schema updates are transactional
    More complex schema upgrades require full DB migration
  • 29. Database Schema Upgrades
    Create a new DatabaseSchemaUpdater
    MyDerivedDataContext context = newMyDerivedDataContext("foo.sdf");DatabaseSchemaUpdaterdbUpdater = context.CreateDatabaseSchemaUpdater();
    Add a new table tied to the Product class
    dbUpdater.AddTable<Winemaker>();
    Add a Region column to the Customer table
    dbUpdater.AddColumn<Vineyard>(“YearEstablished");
    Execute upgrade
    dbUpdater.Execute();
  • 30. performance and best practices
    keep change sets small
    Submit early and often to avoid data loss on app termination
    use background threads
    Non-trivial operations will impact app responsiveness if done on UI thread
    optimize read-only queries
    Set ObjectTrackingEnabled to minimize memory usage
    use secondary indices for properties which you query often
  • 31. performance and best practices
    populate large reference data tables in advance
    Create a simple project to prepopulate data in emulator
    Pull out database file using Isolated Storage explorer
    when to use a database…
    expect some impact to startup time and memory usage from incorporating a DB
    stick to IsolatedStorageSettings or basic files for small data sets
  • 32. user data
  • 33. new and updated APIs in “Mango”
    Chooser Tasks related to user data
    EmailAddressChooserTask
    PhoneNumberChooserTask
    AddressChooserTask
    Microsoft.Phone.UserData for direct access
    Contacts
    Appointments
  • 34. AddressChooserTask
    privateAddressChooserTaskaddressChooserTask;
    // Constructor
    publicMainPage()
    {
    this.addressChooserTask= newAddressChooserTask();
    this.addressChooserTask.Completed += new
    EventHandler<AddressResult>(
    addressChooserTask_Completed);
    }
    privatevoidaddressChooserTask_Completed(objectsender, AddressResulte)
    {
    if(null == e.Error && TaskResult.OK == e.TaskResult)
    {
    ... = e.DisplayName;
    ... = e.Address;
    }
    }
  • 35. Microsoft.Phone.UserData
    Important points
    Contacts and Appointments APIs are read only
    Third party social network data cannot be shared
  • 36. Contacts/Appointments Data Shared
  • 37. contacts: hello, world!
    Contactscontacts = newContacts();
    contacts.SearchCompleted+= newEventHandler<ContactsSearchEventArgs>((sender, e) =>
    {
    ...= e.Results;
    });
    // E.g. search for all contacts
    contacts.SearchAsync(string.Empty, FilterKind.None, null);
    state
    // E.g. search for all contacts with display name matching "ja"
    contacts.SearchAsync("ja", FilterKind.DisplayName, null);
    filter expression
    (not a regex)
    Filter kind: name, email , phone or pinned to start)
  • 38. appointments: hello, world!
    Appointmentsappointments = newAppointments();
    appointments.SearchCompleted+= newEventHandler<AppointmentsSearchEventArgs>((sender, e) =>
    {
    ... = e.Results;
    });
    // E.g. get next appointment (up to 1 week away)
    appointments.SearchAsync(DateTime.Now,
    DateTime.Now+ TimeSpan.FromDays(7),
    1, null);
    start date and time
    end date and time
    Maximum items to return
    state
  • 39. performance and best practices
    be responsible
    your privacy policy should cover how you use the user’s contact information
    keep out of the way
    users have widely varying contact list sizes
    your UI should handle delays gracefully
    don’t let data get stale
    data returned is a snapshot
    refresh state when reasonable
  • 40. demo
    35
    / linq to sql
    / datacontext
    / CRUD
    / user data
    04 user manager
  • 41. practice
    36
    a todo list
  • 42. thank you
    37
    thank youwww.anytao.com
  • 43. 38