diff --git a/README.md b/README.md index 828381ac..446a93aa 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,42 @@ IDataConfiguration dataConfiguration = new DataConfiguration( ); ``` +Register with dependency injection + +```c# +services.AddFluentCommand(builder => builder + .UseConnectionString(ConnectionString) + .UseSqlServer() +); +``` + +Register using a connection name from the appsettings.json + +```c# +services.AddFluentCommand(builder => builder + .UseConnectionName("Tracker") + .UseSqlServer() +); +``` + +```json +{ + "ConnectionStrings": { + "Tracker": "Data Source=(local);Initial Catalog=TrackerTest;Integrated Security=True;TrustServerCertificate=True;" + } +} +``` + +Register for PostgreSQL + +```c# +services.AddFluentCommand(builder => builder + .UseConnectionName("Tracker") + .AddProviderFactory(NpgsqlFactory.Instance) + .AddPostgreSqlGenerator() +); +``` + ### Example Query all users with email domain. Entity is automatically created from DataReader. diff --git a/docs/guide/configuration.md b/docs/guide/configuration.md index 617a1a43..b04d502e 100644 --- a/docs/guide/configuration.md +++ b/docs/guide/configuration.md @@ -19,3 +19,39 @@ var dataConfiguration = new DataConfiguration( queryLogger: dataLogger ); ``` + +Register with dependency injection + +```csharp +services.AddFluentCommand(builder => builder + .UseConnectionString(ConnectionString) + .UseSqlServer() +); +``` + +Register using a connection name from the appsettings.json + +```csharp +services.AddFluentCommand(builder => builder + .UseConnectionName("Tracker") + .UseSqlServer() +); +``` + +```json +{ + "ConnectionStrings": { + "Tracker": "Data Source=(local);Initial Catalog=TrackerTest;Integrated Security=True;TrustServerCertificate=True;" + } +} +``` + +Register for PostgreSQL + +```csharp +services.AddFluentCommand(builder => builder + .UseConnectionName("Tracker") + .AddProviderFactory(NpgsqlFactory.Instance) + .AddPostgreSqlGenerator() +); +``` diff --git a/src/FluentCommand.SqlServer/DataConfigurationBuilderExtensions.cs b/src/FluentCommand.SqlServer/DataConfigurationBuilderExtensions.cs new file mode 100644 index 00000000..22f9c49b --- /dev/null +++ b/src/FluentCommand.SqlServer/DataConfigurationBuilderExtensions.cs @@ -0,0 +1,15 @@ +using Microsoft.Data.SqlClient; + +namespace FluentCommand; + +public static class DataConfigurationBuilderExtensions +{ + public static DataConfigurationBuilder UseSqlServer(this DataConfigurationBuilder builder) + { + builder + .AddProviderFactory(SqlClientFactory.Instance) + .AddSqlServerGenerator(); + + return builder; + } +} diff --git a/test/FluentCommand.SqlServer.Tests/DatabaseFixture.cs b/test/FluentCommand.SqlServer.Tests/DatabaseFixture.cs index 55d37fd6..2f273b7f 100644 --- a/test/FluentCommand.SqlServer.Tests/DatabaseFixture.cs +++ b/test/FluentCommand.SqlServer.Tests/DatabaseFixture.cs @@ -33,8 +33,7 @@ protected override void ConfigureServices(HostBuilderContext context, IServiceCo services.AddFluentCommand(builder => builder .UseConnectionString(trackerConnection) - .AddProviderFactory(SqlClientFactory.Instance) - .AddSqlServerGenerator() + .UseSqlServer() .AddDistributedDataCache() ); @@ -42,8 +41,7 @@ protected override void ConfigureServices(HostBuilderContext context, IServiceCo services.AddFluentCommand(builder => builder .UseConnectionString(readOnlyConnection) - .AddProviderFactory(SqlClientFactory.Instance) - .AddSqlServerGenerator() + .UseSqlServer() .AddDistributedDataCache() ); }