示例#1
0
        public static async Task <bool> EnsureDataCreatedAsync([NotNull] MobileDbContext dbContext,
                                                               CancellationToken cancellationToken)
        => (await DbOperationsWrappers.PerformSafelyAsync(dbContext, async db =>
        {
            await db.Database.MigrateAsync(cancellationToken);

            Logger.LogDebug(LogScope, "Migration performed.");

            if (await db.Tasks.AnyAsync(cancellationToken))
            {
                Logger.LogDebug(LogScope, "Data already created.");
                return(new ErrorOr <Nothing>(new Nothing()));
            }

            var rnd = new Random();

            var tasks = new List <TaskEntity>();

            for (var i = 1; i <= 10; i++)
            {
                var task = new TaskEntity
                {
                    Title = $"Task {i}",
                    Description = $"Task {i} description. {Guid.NewGuid().ToString()}",
                    Start = new DateTime(2017, 10, rnd.Next(1, 10)),
                    End = new DateTime(2017, 10, rnd.Next(11, 30))
                };

                if (rnd.NextDouble() > 0.6)
                {
                    for (var j = 0; j < rnd.Next(1, 3); j++)
                    {
                        task.Notes.Add(new Note
                        {
                            Created = DateTime.Now,
                            Text = $"{task.Title} note {j} : {Guid.NewGuid()}"
                        });
                    }
                }

                tasks.Add(task);
            }

            db.Tasks.AddRange(tasks);

            await db.SaveChangesAsync(cancellationToken);

            Logger.LogDebug(LogScope, "Tasks saved.");

            return(new ErrorOr <Nothing>(new Nothing()));
        })).IsSuccess;
示例#2
0
 public static async Task <ErrorOr <TSuccess> > PerformSafelyAsync <TSuccess>(MobileDbContext mobileDbContext, Func <MobileDbContext, Task <ErrorOr <TSuccess> > > operation)
     where TSuccess : class
 {
     try
     {
         return(await operation.Invoke(mobileDbContext));
     }
     catch (OperationCanceledException operationCanceledException)
     {
         return(new ErrorOr <TSuccess>(new ErrorModel(operationCanceledException)));
     }
     catch (Exception exception)
     {
         return(new ErrorOr <TSuccess>(new ErrorModel(exception)));
     }
 }
 public static async Task <ErrorOr <TSuccess> > PerformSafelyAsync <TSuccess>(MobileDbContext mobileDbContext, Func <MobileDbContext, Task <ErrorOr <TSuccess> > > operation)
     where TSuccess : class
 {
     try
     {
         return(await operation.Invoke(mobileDbContext));
     }
     catch (OperationCanceledException operationCanceledException)
     {
         Logger.LogDebug(LogScope, "Database operation cancelled.");
         return(new ErrorOr <TSuccess>(new ErrorModel(operationCanceledException)));
     }
     catch (Exception exception)
     {
         Logger.LogEntry(LogScope, LogLevel.Error, "Database operation failed.");
         Logger.LogException(LogScope, LogLevel.Error, exception);
         return(new ErrorOr <TSuccess>(new ErrorModel(exception)));
     }
 }