Created
August 2, 2012 12:04
-
-
Save garchibald/3236552 to your computer and use it in GitHub Desktop.
Sample Caliburn.Micro SQ Lite IResult Implementation for WinRT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************** Module Header ******************************\ | |
Module Name: SQLiteResult | |
Copyright (c) Grant Archibald. | |
This source is subject to the Microsoft Public License. | |
See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. | |
All other rights reserved. | |
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, | |
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. | |
\***************************************************************************/ | |
using System; | |
using System.IO; | |
using System.Threading.Tasks; | |
using SQLite; | |
using Windows.UI.Xaml.Controls; | |
namespace Caliburn.Micro.WinRT.Sample.Results | |
{ | |
/// <summary> | |
/// Sample implementation of Sq Lite result to interact with the database | |
/// </summary> | |
/// <remarks>The internal implementation make use of <see cref="Task"/> to subsequent <see cref="IResult"/> actions make need to us ethe <see cref="Control.Dispatcher"/> to update the UI thread</remarks> | |
/// <remarks>See Tim Heuer's blog post http://timheuer.com/blog/archive/2012/05/20/using-sqlite-in-metro-style-app.aspx on how to include x32/x64 version of sqllite in your project </remarks> | |
public class SQLiteResult : IResult | |
{ | |
private readonly Action<SQLiteConnection> _action; | |
public string DatabaseName { get; set; } | |
public SQLiteResult(Action<SQLiteConnection> action) | |
{ | |
_action = action; | |
DatabaseName = "db.sqlite"; | |
UseTransaction = false; | |
} | |
public void Execute(ActionExecutionContext context) | |
{ | |
Task.Run(() => DoAction()); | |
} | |
/// <summary> | |
/// Internal task that will execute the command and call <see cref="Completed"/> once done | |
/// </summary> | |
private void DoAction() | |
{ | |
try | |
{ | |
string dbRootPath = Windows.Storage.ApplicationData.Current.LocalFolder.Path; | |
using (var db = new SQLiteConnection(Path.Combine(dbRootPath, DatabaseName))) | |
{ | |
if (UseTransaction) | |
{ | |
db.RunInTransaction(() => _action(db)); | |
} | |
else | |
{ | |
_action(db); | |
} | |
} | |
} | |
finally | |
{ | |
if (Completed != null) | |
{ | |
Completed(this, new ResultCompletionEventArgs()); | |
} | |
} | |
} | |
/// <summary> | |
/// <c>True</c> indicates that the action should be included in a SqLite transaction | |
/// </summary> | |
public bool UseTransaction { get; set; } | |
public event EventHandler<ResultCompletionEventArgs> Completed; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment