Пример #1
0
        public async Task <byte[]> GetOfflineDBAsync(Guid projectId)
        {
            var temp = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            var context = new DbContextOptionsBuilder <DiaryContext>();

            context.UseSqlite($"Filename={temp}", p => p.AddChangeTracking());

            using (var ctx = new DiaryContext(context.Options))
            {
                ctx.Database.EnsureCreated();
            }

            var builder = new SyncBuilder();

            builder.Target(p => p.AddEntityFrameworkCore <DiaryContext>().UseSqlServer(_configuration.GetConnectionString("DiaryContext")))
            .Source(p => p.AddEntityFrameworkCore <DiaryContext>().UseSqlite($"Filename={temp}"));

            using (var sync = builder.Build(_provider))
            {
                var modelProvider = sync.TargetSyncProvider as ISyncConfigurationProvider;

                var sourceId = await sync.SourceSyncProvider.GetPeerIdAsync();

                await modelProvider.EnsureRemotePeerAsync(sourceId, p => p.Name = $"xamarin dev {sourceId}");

                await modelProvider.SetScenarioAsync(sourceId, Constants.ScenarioId);

                await modelProvider.SaveParameterConfigurationAsync(
                    sourceId,
                    new[] { new FilterParameterConfiguration
                            {
                                ParameterId = Constants.ProjectParameter.ParameterId,
                                Values      = { Codeworx.Synchronization.ChangeProperty.Create <Guid>(projectId) }
                            } });

                await sync.RunAsync();

                using (var fs = new FileStream(temp, FileMode.Open, FileAccess.Read))
                    using (var ms = new MemoryStream())
                    {
                        await fs.CopyToAsync(ms);

                        return(ms.ToArray());
                    }
            }
        }
Пример #2
0
        public static void SetBuilder(SyncBuilder builder)
        {
            if (_syncs == null)
            {
                throw new Exception("Failed to initialize");
            }

            for (int position = 0; position < builder.Data.Length;)
            {
                Id    id = (Id)ByteConverter.GetOfType(builder.Data, ref position, typeof(Id));
                ASync sync;
                if (!_syncs.TryGetValue(id, out sync))
                {
                    throw new KeyNotFoundException("ID not found in _syncs. ID: " + id);
                }
                sync.ReadFromSave(builder.Data, ref position);
            }
        }