Skip to content

Commit

Permalink
Adding readme's for integrations
Browse files Browse the repository at this point in the history
Also decided to rename the method to AddSqliteConnection to be clearer as to what you're adding
  • Loading branch information
aaronpowell committed Jan 9, 2025
1 parent cf64c24 commit b5d214c
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
builder.AddServiceDefaults();
builder.Services.AddProblemDetails();

builder.AddSqliteClient("sqlite");
builder.AddSqliteConnection("sqlite");
builder.AddSqliteDbContext<BloggingContext>("sqlite-ef");

var app = builder.Build();
Expand Down
34 changes: 34 additions & 0 deletions src/CommunityToolkit.Aspire.Hosting.Sqlite/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# CommunityToolkit.Aspire.Hosting.Sqlite library

Provides extension methods and resource definitions for the .NET Aspire AppHost to support creating and running SQLite databases.

The integration also provides support for running [SQLite Web](https://github.com/coleifer/sqlite-web) to interact with the SQLite database.

By default, the Sqlite resource will create a new SQLite database in a temporary location. You can also specify a path to an existing SQLite database file.

## Getting Started

### Install the package

In your AppHost project, install the package using the following command:

```dotnetcli
dotnet add package CommunityToolkit.Aspire.Hosting.Sqlite
```

### Example usage

Then, in the _Program.cs_ file of `AppHost`, define an Sqlite resource, then call `AddSqlite`:

```csharp
var ollama = builder.AddSqlite("sqlite")
.WithSqliteWeb();
```

## Additional Information

https://learn.microsoft.com/dotnet/aspire/community-toolkit/sqlite

## Feedback & contributing

https://github.com/CommunityToolkit/Aspire
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,40 @@ public static class AspireSqliteExtensions
/// </summary>
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param>
/// <param name="name">The connection name to use to find a connection string.</param>
/// <param name="configureSettings">An optional method that can be used for customizing the <see cref="SqliteClientSettings"/>. It's invoked after the settings are read from the configuration.</param>
/// <param name="configureSettings">An optional method that can be used for customizing the <see cref="SqliteConnectionSettings"/>. It's invoked after the settings are read from the configuration.</param>
/// <remarks>Reads the configuration from "Aspire:Sqlite:Client" section.</remarks>
/// <exception cref="InvalidOperationException">If required ConnectionString is not provided in configuration section.</exception>
public static void AddSqliteClient(
public static void AddSqliteConnection(
this IHostApplicationBuilder builder,
string name,
Action<SqliteClientSettings>? configureSettings = null) =>
Action<SqliteConnectionSettings>? configureSettings = null) =>
AddSqliteClient(builder, DefaultConfigSectionName, configureSettings, name, serviceKey: null);

/// <summary>
/// Registers <see cref="SqliteConnection" /> as a keyed singleton for the given <paramref name="name" /> in the services provided by the <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param>
/// <param name="name">The connection name to use to find a connection string.</param>
/// <param name="configureSettings">An optional method that can be used for customizing the <see cref="SqliteClientSettings"/>. It's invoked after the settings are read from the configuration.</param>
/// <param name="configureSettings">An optional method that can be used for customizing the <see cref="SqliteConnectionSettings"/>. It's invoked after the settings are read from the configuration.</param>
/// <remarks>Reads the configuration from "Aspire:Sqlite:Client" section.</remarks>
/// <exception cref="InvalidOperationException">If required ConnectionString is not provided in configuration section.</exception>
public static void AddKeyedSqliteClient(
public static void AddKeyedSqliteConnection(
this IHostApplicationBuilder builder,
string name,
Action<SqliteClientSettings>? configureSettings = null) =>
Action<SqliteConnectionSettings>? configureSettings = null) =>
AddSqliteClient(builder, $"{DefaultConfigSectionName}:{name}", configureSettings, connectionName: name, serviceKey: name);

private static void AddSqliteClient(
this IHostApplicationBuilder builder,
string configurationSectionName,
Action<SqliteClientSettings>? configureSettings,
Action<SqliteConnectionSettings>? configureSettings,
string connectionName,
object? serviceKey)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(connectionName);

SqliteClientSettings settings = new();
SqliteConnectionSettings settings = new();
var configSection = builder.Configuration.GetSection(configurationSectionName);
configSection.Bind(settings);

Expand Down Expand Up @@ -85,7 +85,7 @@ private static void AddSqliteClient(

private static void RegisterSqliteServices(
this IHostApplicationBuilder builder,
SqliteClientSettings settings,
SqliteConnectionSettings settings,
string connectionName,
object? serviceKey)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#nullable enable
Microsoft.Extensions.Hosting.AspireSqliteExtensions
Microsoft.Extensions.Hosting.SqliteClientSettings
Microsoft.Extensions.Hosting.SqliteClientSettings.ConnectionString.get -> string?
Microsoft.Extensions.Hosting.SqliteClientSettings.ConnectionString.set -> void
Microsoft.Extensions.Hosting.SqliteClientSettings.DisableHealthChecks.get -> bool
Microsoft.Extensions.Hosting.SqliteClientSettings.DisableHealthChecks.set -> void
Microsoft.Extensions.Hosting.SqliteClientSettings.SqliteClientSettings() -> void
static Microsoft.Extensions.Hosting.AspireSqliteExtensions.AddKeyedSqliteClient(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! name, System.Action<Microsoft.Extensions.Hosting.SqliteClientSettings!>? configureSettings = null) -> void
static Microsoft.Extensions.Hosting.AspireSqliteExtensions.AddSqliteClient(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! name, System.Action<Microsoft.Extensions.Hosting.SqliteClientSettings!>? configureSettings = null) -> void
Microsoft.Extensions.Hosting.SqliteConnectionSettings
Microsoft.Extensions.Hosting.SqliteConnectionSettings.ConnectionString.get -> string?
Microsoft.Extensions.Hosting.SqliteConnectionSettings.ConnectionString.set -> void
Microsoft.Extensions.Hosting.SqliteConnectionSettings.DisableHealthChecks.get -> bool
Microsoft.Extensions.Hosting.SqliteConnectionSettings.DisableHealthChecks.set -> void
Microsoft.Extensions.Hosting.SqliteConnectionSettings.SqliteConnectionSettings() -> void
static Microsoft.Extensions.Hosting.AspireSqliteExtensions.AddKeyedSqliteConnection(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! name, System.Action<Microsoft.Extensions.Hosting.SqliteConnectionSettings!>? configureSettings = null) -> void
static Microsoft.Extensions.Hosting.AspireSqliteExtensions.AddSqliteConnection(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! name, System.Action<Microsoft.Extensions.Hosting.SqliteConnectionSettings!>? configureSettings = null) -> void
43 changes: 43 additions & 0 deletions src/CommunityToolkit.Aspire.Microsoft.Data.Sqlite/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# CommunityToolkit.Aspire.Microsoft.Data.Sqlite library

Register a `SqliteConnection` in the DI container to interact with a SQLite database using ADO.NET.

## Getting Started

### Prerequisites

- A SQLite database

### Install the package

Install the .NET Aspire EF Core Sqlite library using the following command:

```dotnetcli
dotnet add package CommunityToolkit.Aspire.Microsoft.Data.Sqlite
```

### Example usage

In the _Program.cs_ file of your project, call the `AddSqliteConnection` extension method to register the `SqliteConnection` implementation in the DI container. This method takes the connection name as a parameter:

```csharp
builder.AddSqliteConnection("sqlite");
```

Then, in your service, inject `SqliteConnection` and use it to interact with the database:

```csharp
public class MyService(SqliteConnection context)
{
// ...
}
```

## Additional documentation

- https://learn.microsoft.com/dotnet/aspire/community-toolkit/sqlite
- https://learn.microsoft.com/dotnet/standard/data/sqlite

## Feedback & contributing

https://github.com/CommunityToolkit/Aspire
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Microsoft.Extensions.Hosting;
/// <summary>
/// Represents the settings for the Sqlite client.
/// </summary>
public sealed class SqliteClientSettings
public sealed class SqliteConnectionSettings
{
/// <summary>
/// The connection string of the PostgreSQL database to connect to.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite library

Register a `DbContext` in the DI container to interact with a SQLite database using Entity Framework Core.

## Getting Started

### Prerequisites

- A `DbContext`
- A SQLite database

### Install the package

Install the .NET Aspire EF Core Sqlite library using the following command:

```dotnetcli
dotnet add package CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite
```

### Example usage

In the _Program.cs_ file of your project, call the `AddSqliteDbContext<TDbContext>` extension method to register the `TDbContext` implementation in the DI container. This method takes the connection name as a parameter:

```csharp
builder.AddSqliteDbContext<BloggingContext>("sqlite");
```

Then, in your service, inject `TDbContext` and use it to interact with the database:

```csharp
public class MyService(BloggingContext context)
{
// ...
}
```

## Additional documentation

- https://learn.microsoft.com/dotnet/aspire/community-toolkit/sqlite-entity-framework-integration
- https://learn.microsoft.com/ef/core/providers/sqlite/

## Feedback & contributing

https://github.com/CommunityToolkit/Aspire
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ public class ConfigurationTests
{
[Fact]
public void ConnectionStringIsNullByDefault() =>
Assert.Null(new SqliteClientSettings().ConnectionString);
Assert.Null(new SqliteConnectionSettings().ConnectionString);

[Fact]
public void HealthChecksEnabledByDefault() =>
Assert.False(new SqliteClientSettings().DisableHealthChecks);
Assert.False(new SqliteConnectionSettings().DisableHealthChecks);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace CommunityToolkit.Aspire.Microsoft.Data.Sqlite.Tests;

public class ConformanceTests : ConformanceTests<SqliteConnection, SqliteClientSettings>
public class ConformanceTests : ConformanceTests<SqliteConnection, SqliteConnectionSettings>
{
protected override ServiceLifetime ServiceLifetime => ServiceLifetime.Scoped;

Expand All @@ -24,29 +24,29 @@ protected override void PopulateConfiguration(ConfigurationManager configuration
]);
}

protected override void RegisterComponent(HostApplicationBuilder builder, Action<SqliteClientSettings>? configure = null, string? key = null)
protected override void RegisterComponent(HostApplicationBuilder builder, Action<SqliteConnectionSettings>? configure = null, string? key = null)
{
if (key is null)
{
builder.AddSqliteClient("sqlite", configure);
builder.AddSqliteConnection("sqlite", configure);
}
else
{
builder.AddKeyedSqliteClient(key, configure);
builder.AddKeyedSqliteConnection(key, configure);
}
}

protected override void SetHealthCheck(SqliteClientSettings options, bool enabled)
protected override void SetHealthCheck(SqliteConnectionSettings options, bool enabled)
{
throw new NotImplementedException();
}

protected override void SetMetrics(SqliteClientSettings options, bool enabled)
protected override void SetMetrics(SqliteConnectionSettings options, bool enabled)
{
throw new NotImplementedException();
}

protected override void SetTracing(SqliteClientSettings options, bool enabled)
protected override void SetTracing(SqliteConnectionSettings options, bool enabled)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public void ReadsFromConnectionStringCorrectly(bool useKeyed)

if (useKeyed)
{
builder.AddKeyedSqliteClient("sqlite");
builder.AddKeyedSqliteConnection("sqlite");
}
else
{
builder.AddSqliteClient("sqlite");
builder.AddSqliteConnection("sqlite");
}

using var host = builder.Build();
Expand All @@ -47,11 +47,11 @@ public void CanSetConnectionStringInCode(bool useKeyed)

if (useKeyed)
{
builder.AddKeyedSqliteClient("sqlite", settings => settings.ConnectionString = "Data Source=:memory:");
builder.AddKeyedSqliteConnection("sqlite", settings => settings.ConnectionString = "Data Source=:memory:");
}
else
{
builder.AddSqliteClient("sqlite", settings => settings.ConnectionString = "Data Source=:memory:");
builder.AddSqliteConnection("sqlite", settings => settings.ConnectionString = "Data Source=:memory:");
}

using var host = builder.Build();
Expand All @@ -76,11 +76,11 @@ public void CanSetConnectionStringInCodeWithKey(bool useKeyed)

if (useKeyed)
{
builder.AddKeyedSqliteClient("sqlite");
builder.AddKeyedSqliteConnection("sqlite");
}
else
{
builder.AddSqliteClient("sqlite");
builder.AddSqliteConnection("sqlite");
}

using var host = builder.Build();
Expand All @@ -93,16 +93,16 @@ public void CanSetConnectionStringInCodeWithKey(bool useKeyed)
}

[Fact]
public void CanSetMultipleKeyedClients()
public void CanSetMultipleKeyedConnections()
{
var builder = Host.CreateEmptyApplicationBuilder(null);
builder.Configuration.AddInMemoryCollection([
new KeyValuePair<string, string?>("ConnectionStrings:sqlite1", "Data Source=/tmp/sqlite1.db"),
new KeyValuePair<string, string?>("ConnectionStrings:sqlite2", "Data Source=/tmp/sqlite2.db")
]);

builder.AddKeyedSqliteClient("sqlite1");
builder.AddKeyedSqliteClient("sqlite2");
builder.AddKeyedSqliteConnection("sqlite1");
builder.AddKeyedSqliteConnection("sqlite2");

using var host = builder.Build();

Expand Down

0 comments on commit b5d214c

Please sign in to comment.