<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Cheyzan</title>
    <description>The latest articles on DEV Community by Cheyzan (@cheyzan_18dc21bbc946902fd).</description>
    <link>https://dev.to/cheyzan_18dc21bbc946902fd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3477379%2F406d31eb-7e99-4261-aa26-c8cee99f48ea.png</url>
      <title>DEV Community: Cheyzan</title>
      <link>https://dev.to/cheyzan_18dc21bbc946902fd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cheyzan_18dc21bbc946902fd"/>
    <language>en</language>
    <item>
      <title>Embracing Cloud Independence &amp; Multi-Database in .NET Core Web API</title>
      <dc:creator>Cheyzan</dc:creator>
      <pubDate>Wed, 03 Sep 2025 15:02:16 +0000</pubDate>
      <link>https://dev.to/cheyzan_18dc21bbc946902fd/embracing-cloud-independence-multi-database-in-net-core-web-api-4ki4</link>
      <guid>https://dev.to/cheyzan_18dc21bbc946902fd/embracing-cloud-independence-multi-database-in-net-core-web-api-4ki4</guid>
      <description>&lt;p&gt;In today's cloud-native world, vendor lock-in poses a significant risk, particularly due to high costs. A modern application should be adaptable, capable of running on AWS, Azure, or GCP, and be able to switch between databases like PostgreSQL and SQL Server with minimal friction. I recently implemented this functionality into a .NET Core Web API project, and here's a breakdown of the approach.&lt;/p&gt;

&lt;p&gt;The Goal: Configuration-Driven Infrastructure&lt;/p&gt;

&lt;p&gt;The idea is to make the cloud provider and database technology a configuration choice, not a hard-coded decision. The application loads itself based on predefined settings in the &lt;code&gt;appsettings.json&lt;/code&gt; or environment variables.&lt;/p&gt;

&lt;p&gt;How It Works: A Look at the Code&lt;/p&gt;

&lt;p&gt;The magic happens in &lt;code&gt;Program.cs&lt;/code&gt;. The application starts by building a configuration that can be overridden by an environment variable (&lt;code&gt;APP_CONFIG_FILE&lt;/code&gt;), crucial for differing environments (dev, staging, prod).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;csharp
Load configuration file (supports APP_CONFIG_FILE env override)
var configFile = Environment.GetEnvironmentVariable("APP_CONFIG_FILE")
                 ?? $"appsettings.{builder.Environment.EnvironmentName}.json"
                 ?? "appsettings.json";
builder.Configuration.AddConfiguration(configuration);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, it binds strongly-typed option classes for service and cloud settings. This is where we decide our fate for this deployment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;csharp
region Bind required options
var serviceOption = configuration.GetSection("ServiceOptions").Get&amp;lt;ServiceOptions&amp;gt;()
    ?? throw new ApplicationException(...);

var cloudConfig = configuration.GetSection("CloudServiceOptions").Get&amp;lt;CloudServiceOptions&amp;gt;()
    ?? throw new ApplicationException(...);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The database provider is using a simple &lt;code&gt;switch&lt;/code&gt; statement, registering the appropriate validator and services. This pattern is easily extendable to new databases (e.g., MySQL, SQLite).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;csharp
// Register DB Validators based on provider
switch (serviceOption.DbProvider)
{
    case DatabaseProvider.PostgreSQL:
        builder.Services.AddSingleton&amp;lt;IDatabaseConnectionValidator, PostgresConnectionValidator&amp;gt;();
        break;
    case DatabaseProvider.SqlServer:
        builder.Services.AddSingleton&amp;lt;IDatabaseConnectionValidator, SqlServerConnectionValidator&amp;gt;();
        break;
    default:
        throw new NotSupportedException($"Unsupported DB provider: {serviceOption.DbProvider}");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The most powerful part is the Strategy Pattern for cloud providers. A factory, based on the config, creates the specific configurator (e.g., &lt;code&gt;AwsConfigurator&lt;/code&gt;, &lt;code&gt;AzureConfigurator&lt;/code&gt;). This object then handles all provider-specific setup: secrets management, blob storage, etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;csharp
#region Cloud Provider Strategy
var cloudConfigurator = CloudConfiguratorFactory.Create(cloudConfig.Provider);
await cloudConfigurator.ConfigureAsync(builder, configuration);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, comprehensive health checks (/health/live, /health/ready) ensure all components—whether an AWS RDS PostgreSQL instance or an Azure SQL Database—are responsive before the application accepts traffic.&lt;/p&gt;

&lt;p&gt;Scenarios Where This Shines&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Multi-Cloud Deployments: Run the exact same codebase in different clouds for redundancy, compliance, or to leverage best-of-breed services.&lt;/li&gt;
&lt;li&gt;Mitigating Vendor Lock-in: It gives you the negotiating power and freedom to migrate if a provider's costs increase or services decline.&lt;/li&gt;
&lt;li&gt;Development &amp;amp; Testing: Developers can run the API locally with SQLite, while QA tests against a production-like PostgreSQL container, and staging uses the real Azure infrastructure.&lt;/li&gt;
&lt;li&gt;Disaster Recovery (DR): Your DR plan can involve spinning up the application in a secondary cloud provider with a simple configuration change.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Pros &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Flexibility &amp;amp; Portability: The biggest win. Your infrastructure is no longer rigid.&lt;/li&gt;
&lt;li&gt;Easier Testing: Isolate and mock dependencies more cleanly based on configuration.&lt;/li&gt;
&lt;li&gt;Future-Proofing: Onboarding a new cloud provider or database means adding a new implementation, not refactoring the entire codebase.&lt;/li&gt;
&lt;li&gt;Clear Separation of Concerns: Application logic is decoupled from infrastructure logic.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Cons &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initial Complexity: The setup is more complex than a simple, single-provider app. You need to abstract away provider-specific nuances.&lt;/li&gt;
&lt;li&gt;Maintenance Overhead: You must maintain multiple client SDKs and implementations. A change in one cloud provider's SDK needs testing.&lt;/li&gt;
&lt;li&gt;Potential for "Lowest Common Denominator": To remain portable, you might avoid using powerful, unique features of a specific cloud provider (e.g., Azure Cosmos DB's serverless capacity, AWS Aurora's specific integrations).&lt;/li&gt;
&lt;li&gt;Testing Matrix Expansion: You need to test all supported configurations, which can multiply the number of test scenarios.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Conclusion&lt;br&gt;
Building a .NET Core Web API with support for multiple clouds and databases is an investment in architectural flexibility. While it introduces upfront complexity, the long-term benefits of portability, resilience, and avoiding vendor lock-in are immense for any serious application destined for a cloud environment. The key is to use the .NET configuration system and patterns like Strategy and Dependency Injection to your advantage, just as the code above demonstrates.&lt;/p&gt;

&lt;p&gt;Note: The code provided here is not meant to serve as a tutorial, but rather as a basic overview and conceptual abstraction.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>net</category>
      <category>csharp</category>
      <category>cloud</category>
    </item>
  </channel>
</rss>
