Skip to content

Connection Providers

Mike Hanson edited this page Mar 5, 2018 · 1 revision

SqlRepo depends on Connection Providers for connecting to a database. Whether you are using Dependency Injection or the static RepoFactory you must configure an impelementation of IConnectionProvider during application start-up before the first call to create a Repository. If you do not an exception will be thrown to remind you.

If you are using IoC then something the equivalent of the following Autofac example will need to included during startup:

using System;
using Autofac;
using SqlRepo.Abstractions;
using SqlRepo.SqlServer.Autofac;
using SqlRepo.SqlServer.ConnectionProviders;

namespace GettingStartedIoC
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var containerBuilder = new ContainerBuilder();

            containerBuilder.RegisterModule<SqlRepoSqlServerAutofacModule>();

            var connectionProvider = new AppConfigFirstConnectionProvider();
            containerBuilder.RegisterInstance(connectionProvider)
                            .As<IConnectionProvider>();

            // ... other registrations

            var container = containerBuilder.Build();
        }
    }
}

If you are using the static RepoFactory then the following example will need to be included during startup:

using System;
using SqlRepo.SqlServer.ConnectionProviders;
using SqlRepo.SqlServer.Static;

namespace GettingStartedStatic
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var connectionProvider = new AppConfigFirstConnectionProvider();
            RepoFactory.UseConnectionProvider(connectionProvider);

            // ...
        }
    }
}

The SqlRepo.SqlServer package includes four connection providers that should cover most use cases. You can also create your own implementation of IConnectionProvider if you use case is not covered.

AppConfigFirstConnectionProvider

This connection provider relies on an [App|Web].config file used in .NET Framework applications. It returns connections based on the first connection string in the connectionStrings section of the file. It is important that you remember to clear any existing connection strings at the start of the section to ensure the first one that is defined within the file is used. Note how the following example uses the clear element.

<?xml version="1.0" encoding="utf-8" ?>

<configuration>
  <connectionStrings>
    <clear />
    <add name="GettingStartedDbCon"
         connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\_src\SqlRepo\Demos\GettingStarted\GettingStarted.mdf;Integrated Security=True;Connect Timeout=30" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0"
                      sku=".NETFramework,Version=v4.7" />
  </startup>
</configuration>

The C# examples above show how this connection provider should be used.

AppConfigNamedConnectionProvider

This connection provider relies on an [App|Web].config file used in .NET Framework applications. It returns connections based on the connection string with a specified name in the connectionStrings section of the file.

Given the following configuration file:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>
  <connectionStrings>
    <clear />
    <add name="GettingStartedDbCon"
         connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\_src\SqlRepo\Demos\GettingStarted\GettingStarted.mdf;Integrated Security=True;Connect Timeout=30" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0"
                      sku=".NETFramework,Version=v4.7" />
  </startup>
</configuration>

This connection provider must be instantiated like this:

var connectionProvider = new AppConfigNamedConnectionProvider("GettingStartedDbCon");

This can be registered as an instance in IoC or passed to the UseConnectionProvider(...) method of the static RepoFactory.

AppSettingsConnectionProvider

This connection provider relies on the IConfiguration interface introduced with .NET Core and settings specified in an appsettings.json file or similar file loaded during startup of the application.

Given the following appsettings.json file:

{
    "ConnectionStrings": {
        "GettingStartedDbCon": "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\_src\\SqlRepo\\SqlRepo.SqlServer.IntegrationTests\\Testing.mdf;Integrated Security=True;Connect Timeout=30"
    }
}

That is loaded a startup something like this:

var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
var configuration = builder.Build();

This connection provider must be instantiated like this:

var conectionProvider = new AppSettingsConfigurationProvider(configuration, "GettingStartedDbCon");

This can be registered as an instance in IoC or passed to the UseConnectionProvider(...) method of the static RepoFactory.

ConnectionStringConnectionProvider

This connection provider does not rely on any configuration file, instead it requires you to instantiate it with a full connection string. Where you get that connection string from is at your discretion. It must be instantiated something like this:

var connectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\_src\\SqlRepo\\SqlRepo.SqlServer.IntegrationTests\\Testing.mdf;Integrated Security=True;Connect Timeout=30";

var connectionProvider = new ConnectionStringConnectionProvider(connectionString);

This can be registered as an instance in IoC or passed to the UseConnectionProvider(...) method of the static RepoFactory.