microsoft / microsoft/aspire

Add interfaces to PostgreSQL, Redis, and SqlServer Hosting Resources

Open
#6,055 4 comments 0 reactions 0 assignees View on GitHub
area-integrations postgres redis
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

## Background and Motivation

See https://github.com/dotnet/aspire/pull/5930#issuecomment-2381917879 for some of the context.

With

* [Add Azure PostgreSQL managed identity (dotnet/aspire#5930)](https://github.com/dotnet/aspire/pull/5930)
* [Support Managed Identity in Azure Cache for Redis (dotnet/aspire#5966)](https://github.com/dotnet/aspire/pull/5966)
* [Update Azure Sql to use new API pattern (dotnet/aspire#5998)](https://github.com/dotnet/aspire/issues/5998)

We re-designed how resources that can be hosted in either a container or a managed Azure resource (ex. PostgreSQL, Redis, SqlServer) are added to the model.

### Before

```C#
var redis = builder.AddRedis("redis")
.PublishAsAzureRedis();
```

### After

```C#
var redis = builder.AddAzureRedis("redis")
.RunAsContainer();
```

The downside of this approach is composability of resources. Let's say I wanted to have a resource that depended on a Postgres database instance. With this change the implementor of that would need to have extensions for each cloud provider (unless they just allow any IResourceWithConnectionString).

## Proposed API

```C#
namespace Aspire.Hosting.Redis;

public interface IRedisResource : IResourceWithConnectionString
{
}

namespace Aspire.Hosting.ApplicationModel;
public class RedisResource : IRedisResource {...}

namespace Aspire.Hosting.Azure;
public class AzureRedisCacheResource : IRedisResource {...}

namespace Aspire.Hosting.Postgres;

public interface IPostgresServerResource : IResourceWithConnectionString
{
}

namespace Aspire.Hosting.ApplicationModel;
public class PostgresServerResource : IPostgresServerResource {...}

namespace Aspire.Hosting.Azure;
public class AzurePostgresFlexibleServerResource : IPostgresServerResource {...}

namespace Aspire.Hosting.SqlServer;

public interface ISqlServerServerResource : IResourceWithConnectionString
{
}

namespace Aspire.Hosting.ApplicationModel;
public class SqlServerServerResource : ISqlServerServerResource {...}

namespace Aspire.Hosting.Azure;
public class AzureSqlServerServerResource : ISqlServerServerResource {...}
```

## Usage Examples

```csharp

var redis1 = builder.AddRedis("cache1");
UseRedis(redis1);

var redis2 = builder.AddAzureRedis("cache2");
UseRedis(redis2);

static void UseRedis(IResourceBuilder redis)
{
... get the connection string, or use the resource some other way
}
```

## Alternative Designs

## Risks

cc @davidfowl @mitchdenny

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.