200 lines
7.3 KiB
C#
200 lines
7.3 KiB
C#
// <auto-generated>
|
|
// This file is part of EonaCat.Sync examples
|
|
// </auto-generated>
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using EonaCat.Sync;
|
|
|
|
namespace EonaCat.Sync.Examples.ConsoleApp
|
|
{
|
|
/// <summary>
|
|
/// Console application example demonstrating basic file synchronization.
|
|
/// </summary>
|
|
class Program
|
|
{
|
|
static async Task Main(string[] args)
|
|
{
|
|
Console.WriteLine("=== EonaCat.Sync Console Example ===\n");
|
|
|
|
try
|
|
{
|
|
// Example 1: Basic file sync
|
|
await RunBasicFileSync();
|
|
|
|
// Example 2: File sync with options
|
|
await RunFileSyncWithOptions();
|
|
|
|
// Example 3: Database sync
|
|
await RunBasicDatabaseSync();
|
|
|
|
// Example 4: Bidirectional sync
|
|
await RunBidirectionalSync();
|
|
|
|
Console.WriteLine("\n✓ All examples completed successfully!");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.Error.WriteLine($"✗ Error: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Example 1: Basic file synchronization
|
|
/// </summary>
|
|
static async Task RunBasicFileSync()
|
|
{
|
|
Console.WriteLine("Example 1: Basic File Sync");
|
|
Console.WriteLine("---------------------------");
|
|
|
|
var source = Path.Combine(Path.GetTempPath(), "sync_source");
|
|
var target = Path.Combine(Path.GetTempPath(), "sync_target");
|
|
|
|
try
|
|
{
|
|
// Create test files
|
|
Directory.CreateDirectory(source);
|
|
File.WriteAllText(Path.Combine(source, "test1.txt"), "Hello World");
|
|
File.WriteAllText(Path.Combine(source, "test2.txt"), "Test Content");
|
|
|
|
// Create sync service
|
|
var syncService = SyncServiceFactory.CreateSyncService();
|
|
|
|
// Synchronize files
|
|
Console.WriteLine($"Syncing from {source}");
|
|
Console.WriteLine($"Syncing to {target}");
|
|
var result = await syncService.SyncFilesAsync(source, target);
|
|
|
|
if (result.Success)
|
|
{
|
|
Console.WriteLine($"✓ Sync successful! ({result.FilesChanged} files changed)\n");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"✗ Sync failed: {result.ErrorMessage}\n");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"✗ Example failed: {ex.Message}\n");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Example 2: File sync with custom options
|
|
/// </summary>
|
|
static async Task RunFileSyncWithOptions()
|
|
{
|
|
Console.WriteLine("Example 2: File Sync with Options");
|
|
Console.WriteLine("----------------------------------");
|
|
|
|
var source = Path.Combine(Path.GetTempPath(), "sync_source2");
|
|
var target = Path.Combine(Path.GetTempPath(), "sync_target2");
|
|
|
|
try
|
|
{
|
|
Directory.CreateDirectory(source);
|
|
File.WriteAllText(Path.Combine(source, "important.txt"), "Critical Data");
|
|
File.WriteAllText(Path.Combine(source, "readme.md"), "Documentation");
|
|
|
|
var options = new FileSyncOptions
|
|
{
|
|
ConflictResolutionStrategy = ConflictResolutionStrategy.LastWriteWins,
|
|
DeleteMissingInTarget = false,
|
|
VerifyFileIntegrity = true,
|
|
ExcludePatterns = new[] { "*.tmp", "*.log" },
|
|
BufferSize = (int)(1024 * 1024) // 1 MB
|
|
};
|
|
|
|
var syncService = SyncServiceFactory.CreateFileSyncService();
|
|
Console.WriteLine($"Syncing with custom options...");
|
|
var result = await syncService.SyncFilesAsync(source, target, options);
|
|
|
|
if (result.Success)
|
|
{
|
|
Console.WriteLine($"✓ Sync complete! Files: {result.FilesChanged}\n");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"✗ Example failed: {ex.Message}\n");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Example 3: Database synchronization
|
|
/// </summary>
|
|
static async Task RunBasicDatabaseSync()
|
|
{
|
|
Console.WriteLine("Example 3: Database Sync (Conceptual)");
|
|
Console.WriteLine("--------------------------------------");
|
|
|
|
var sourceConnection = "Server=.;Database=SourceDB;Integrated Security=true;";
|
|
var targetConnection = "Server=.;Database=TargetDB;Integrated Security=true;";
|
|
var entities = new[] { "Users", "Products", "Orders" };
|
|
|
|
try
|
|
{
|
|
var syncService = SyncServiceFactory.CreateDatabaseSyncService();
|
|
|
|
var options = new DatabaseSyncOptions
|
|
{
|
|
BatchSize = 1000,
|
|
VerifyAfterSync = true,
|
|
EnableTransactions = true
|
|
};
|
|
|
|
Console.WriteLine($"Source: {sourceConnection}");
|
|
Console.WriteLine($"Target: {targetConnection}");
|
|
Console.WriteLine($"Entities: {string.Join(", ", entities)}");
|
|
|
|
// Note: This example requires actual database setup
|
|
Console.WriteLine("(Requires actual database configuration)\n");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Note: {ex.Message}\n");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Example 4: Bidirectional synchronization
|
|
/// </summary>
|
|
static async Task RunBidirectionalSync()
|
|
{
|
|
Console.WriteLine("Example 4: Bidirectional Sync");
|
|
Console.WriteLine("------------------------------");
|
|
|
|
var computer1 = Path.Combine(Path.GetTempPath(), "computer1");
|
|
var computer2 = Path.Combine(Path.GetTempPath(), "computer2");
|
|
|
|
try
|
|
{
|
|
// Setup directories
|
|
Directory.CreateDirectory(computer1);
|
|
Directory.CreateDirectory(computer2);
|
|
File.WriteAllText(Path.Combine(computer1, "file1.txt"), "From Computer 1");
|
|
File.WriteAllText(Path.Combine(computer2, "file2.txt"), "From Computer 2");
|
|
|
|
var syncService = SyncServiceFactory.CreateSyncService();
|
|
|
|
Console.WriteLine("Syncing Computer 1 → Computer 2");
|
|
await syncService.SyncFilesAsync(computer1, computer2);
|
|
|
|
Console.WriteLine("Syncing Computer 2 → Computer 1");
|
|
var result = await syncService.SyncFilesAsync(computer2, computer1);
|
|
|
|
if (result.Success)
|
|
{
|
|
Console.WriteLine($"✓ Bidirectional sync complete!\n");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"✗ Example failed: {ex.Message}\n");
|
|
}
|
|
}
|
|
}
|
|
}
|