Adapter Pattern — Provider Abstraction in .NET
Definition
Section titled “Definition”The Adapter pattern converts the interface of an existing class into the interface expected by the client, allowing incompatible components to collaborate. The adapter wraps the existing class and translates calls.
Diagram
Section titled “Diagram”classDiagram
class IBlobStorageClient {
+DeleteObjectAsync()
+HeadObjectAsync()
}
class AmazonS3Client {
+DeleteObjectAsync()
+GetObjectMetadataAsync()
}
class S3BlobClient {
-s3Client : AmazonS3Client
}
IBlobStorageClient <|.. S3BlobClient
S3BlobClient --> AmazonS3Client : adapts
class ISmtpTransport {
+SendAsync()
}
class SmtpClient {
+ConnectAsync()
+SendAsync()
}
class MailKitSmtpTransport {
-smtpClient : SmtpClient
}
ISmtpTransport <|.. MailKitSmtpTransport
MailKitSmtpTransport --> SmtpClient : adapts
Implementation in Granit
Section titled “Implementation in Granit”| Adapter | File | Target interface | Adapted class |
|---|---|---|---|
S3BlobClient | src/Granit.BlobStorage.S3/Internal/S3BlobClient.cs | IBlobStorageClient | AmazonS3Client (AWS SDK) |
MailKitSmtpTransport | src/Granit.Notifications.Email.Smtp/MailKitSmtpTransport.cs | ISmtpTransport | SmtpClient (MailKit, sealed) |
Rationale
Section titled “Rationale”S3BlobClient isolates the framework from the AWS SDK, allowing provider
changes (European hosting, MinIO) without touching the core.
MailKitSmtpTransport wraps the sealed MailKit SmtpClient behind a
testable ISmtpTransport interface.
Usage example
Section titled “Usage example”// Replacing S3 with MinIO -- only the adapter changesservices.AddSingleton<IBlobStorageClient, MinioBlobClient>();
// The rest of the application code remains unchangedIBlobStorage blobStorage = serviceProvider.GetRequiredService<IBlobStorage>();PresignedUploadTicket ticket = await blobStorage.InitiateUploadAsync( "medical-documents", new BlobUploadRequest("mri-report.pdf", "application/pdf", MaxAllowedBytes: 50_000_000), cancellationToken);