Jan 27 2025
public interface IPaymentProcessor
{
void ProcessPayment(decimal amount);
}
public class LegacyPaymentService
{
public void MakePayment(string amount)
{
Console.WriteLine($"Processing payment of {amount} via legacy system.");
}
}
public class PaymentAdapter(LegacyPaymentService legacyService) : IPaymentProcessor
{
public void ProcessPayment(decimal amount)
{
// Convert the amount to a string and delegate to the legacy service
string amountString = amount.ToString("F2");
legacyService.MakePayment(amountString);
}
}
internal class Program
{
static void Main(string[] args)
{
LegacyPaymentService legacyService = new();
IPaymentProcessor paymentProcessor = new PaymentAdapter(legacyService);
// Your application code uses the modern IPaymentProcessor interface
paymentProcessor.ProcessPayment(123.4567868m);
}
}
public class LegacyPrinter
{
public void Print(string text)
{
Console.WriteLine($"Legacy Printer: {text}");
}
}
public interface IPrinter
{
void PrintDocument(string content);
}
public class PrinterAdapter(LegacyPrinter legacyPrinter) : IPrinter
{
public void PrintDocument(string content)
{
// Delegate the call to the adaptee (LegacyPrinter)
legacyPrinter.Print(content);
}
}
internal class Program
{
static void Main(string[] args)
{
LegacyPrinter legacyPrinter = new();
IPrinter printerAdapter = new PrinterAdapter(legacyPrinter);
printerAdapter.PrintDocument("Hello, Object Adapter!");
}
}
public class PrinterAdapter : LegacyPrinter, IPrinter
{
public void PrintDocument(string content)
{
// Directly call the inherited method from LegacyPrinter
Print(content);
}
}
internal class Program
{
static void Main(string[] args)
{
IPrinter printerAdapter = new PrinterAdapter();
printerAdapter.PrintDocument("Hello, Class Adapter!");
}
}
public interface ICloudStorage
{
Task UploadFileAsync(string containerName, string fileName, Stream fileStream);
Task<Stream> DownloadFileAsync(string containerName, string fileName);
Task DeleteFileAsync(string containerName, string fileName);
}
public class S3StorageAdapter : ICloudStorage
{
private readonly IAmazonS3 _s3Client;
public S3StorageAdapter(IAmazonS3 s3Client)
{
_s3Client = s3Client;
}
public async Task UploadFileAsync(string containerName, string fileName, Stream fileStream)
{
var request = new PutObjectRequest
{
BucketName = containerName,
Key = fileName,
InputStream = fileStream
};
await _s3Client.PutObjectAsync(request);
}
public async Task<Stream> DownloadFileAsync(string containerName, string fileName)
{
var request = new GetObjectRequest
{
BucketName = containerName,
Key = fileName
};
var response = await _s3Client.GetObjectAsync(request);
return response.ResponseStream;
}
public async Task DeleteFileAsync(string containerName, string fileName)
{
var request = new DeleteObjectRequest
{
BucketName = containerName,
Key = fileName
};
await _s3Client.DeleteObjectAsync(request);
}
}
public class GoogleCloudStorageAdapter : ICloudStorage
{
private readonly StorageClient _storageClient;
public GoogleCloudStorageAdapter(StorageClient storageClient)
{
_storageClient = storageClient;
}
public async Task UploadFileAsync(string containerName, string fileName, Stream fileStream)
{
await _storageClient.UploadObjectAsync(containerName, fileName, null, fileStream);
}
public async Task<Stream> DownloadFileAsync(string containerName, string fileName)
{
MemoryStream memoryStream = new();
await _storageClient.DownloadObjectAsync(containerName, fileName, memoryStream);
memoryStream.Position = 0; // Reset the stream position
return memoryStream;
}
public async Task DeleteFileAsync(string containerName, string fileName)
{
await _storageClient.DeleteObjectAsync(containerName, fileName);
}
}
builder.Services.AddSingleton(new BlobServiceClient("YourAzureConnectionString")); // Azure Blob Storage
builder.Services.AddSingleton(StorageClient.Create()); // Google Cloud Storage
builder.Services.AddSingleton<IAmazonS3>(new AmazonS3Client()); // Amazon S3
// Register Adapters
builder.Services.AddTransient<AzureBlobStorageAdapter>();
builder.Services.AddTransient<GoogleCloudStorageAdapter>();
builder.Services.AddTransient<S3StorageAdapter>();
// Register Factory
builder.Services.AddTransient<Func<string, ICloudStorage>>(sp => provider =>
{
return provider switch
{
"Azure" => sp.GetRequiredService<AzureBlobStorageAdapter>(),
"Google" => sp.GetRequiredService<GoogleCloudStorageAdapter>(),
"AWS" => sp.GetRequiredService<S3StorageAdapter>(),
_ => throw new ArgumentException("Unsupported cloud provider")
};
});
// Register FileService
builder.Services.AddTransient<FileService>();
public class FileService
{
private readonly ICloudStorage _cloudStorage;
public FileService(ICloudStorage cloudStorage)
{
_cloudStorage = cloudStorage;
}
public async Task UploadFile(string containerName, string fileName, Stream fileStream)
{
await _cloudStorage.UploadFileAsync(containerName, fileName, fileStream);
}
public async Task<Stream> DownloadFile(string containerName, string fileName)
{
return await _cloudStorage.DownloadFileAsync(containerName, fileName);
}
public async Task DeleteFile(string containerName, string fileName)
{
await _cloudStorage.DeleteFileAsync(containerName, fileName);
}
}
//This is adapter that trying to adapt Azure Blob Storage to my Cloud Storage
public class AzureBlobStorageAdapter : ICloudStorage
{
private readonly BlobServiceClient _blobServiceClient;
public AzureBlobStorageAdapter(BlobServiceClient blobServiceClient)
{
_blobServiceClient = blobServiceClient;
}
public async Task UploadFileAsync(string containerName, string fileName, Stream fileStream)
{
var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
var blobClient = containerClient.GetBlobClient(fileName);
await blobClient.UploadAsync(fileStream, overwrite: true);
}
public async Task<Stream> DownloadFileAsync(string containerName, string fileName)
{
var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
var blobClient = containerClient.GetBlobClient(fileName);
var response = await blobClient.DownloadAsync();
return response.Value.Content;
}
public async Task DeleteFileAsync(string containerName, string fileName)
{
var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
var blobClient = containerClient.GetBlobClient(fileName);
await blobClient.DeleteIfExistsAsync();
}
}
1. Design Patterns that Deliver
This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 15,250+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 15,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.