コード例 #1
0
 public void Add(Uri uri, TorrentMovie movie)
 {
     using (var db = new LiteDatabase(PCinemaDbName))
     using (var trans = db.BeginTrans())
     {
         var c = db.GetCollection<TorrentMovie>(TorrentMovieCollectionName);
         if (!c.Update(movie))
         {
             c.Insert(movie);
         }
         trans.Commit();
     }
 }
コード例 #2
0
        public void Add(ImdbData movie)
        {
            movie.LastUpdated = DateTime.Now;

            using (var db = new LiteDatabase(PCinemaDbName))
            using (var trans = db.BeginTrans())
            {
                var c = db.GetCollection<ImdbData>(ImdbMovieCollectionName);
                if (!c.Update(movie))
                {
                    c.Insert(movie);
                }
                trans.Commit();
            }
        }
コード例 #3
0
        /// <summary>
        /// Wraps a database operation in a try/catch block and transaction
        /// </summary>
        /// <typeparam name="T">The Type which determines the database collection name</typeparam>
        /// <typeparam name="U">The type of result expected from the operation</typeparam>
        /// <param name="path">The database path and filename</param>
        /// <param name="f">The operation to invoke</param>
        /// <returns>A CallResult with the result of the oepration</returns>
#pragma warning disable CS1573
        public static CallResult <U> LiteDbAction <T, U>(string path, Func <LiteDatabase, LiteTransaction, LiteCollection <T>, U> f, [CallerMemberName] string callerMemberName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = 0) where T : new()
#pragma warning restore CS1573
        {
            //return LiteDbAction<T, U>(path, (database, collection) =>
            //{
            //    using (var transaction = database.BeginTrans())
            //    {
            //        return f.Invoke(database, transaction, collection);
            //    }
            //}, callerMemberName, callerFilePath, callerLineNumber);
            var result = new CallResult <U>();

            try
            {
                if (f == null)
                {
                    throw new ArgumentNullException("LiteDbAction: Parameter 'f' cannot be null");
                }
                using (var db = new LiteDB.LiteDatabase(path))
                {
                    var collection = GetCollection <T>(db);
                    using (var transaction = db.BeginTrans())
                    {
                        result.Result  = f.Invoke(db, transaction, collection);
                        result.Success = true;
                    }
                }
            }
            catch (LiteException e)
            {
                result.Exception = e;
                Log.e(new Exception(string.Format("LiteException: ErrorCode={0}, Message={1}", e.ErrorCode, e.Message), e));
            }
            catch (Exception e)
            {
                result.Exception = e;
                Log.e(new Exception(string.Format("Caller:{0}, Line:{1}, File:{2}", callerMemberName, callerLineNumber, callerFilePath), e));
            }
            return(result);
        }