---
title: Overview
---

## Overview
This page assumes you have read the [AdminUI as a NuGet package](../Installation/Installation_NuGet.md) documentation area.

The AdminUI NuGet package uses a factory abstraction for creating database connections. When using the AdminUI NuGet package in your web applications, you can use the default implementation. This uses connection strings in configuration to create the connection. The connection strings it uses are in appSettings.json and cover 5 different areas of functionality:

```language-json
  "IdentityConnectionString": "...",
  "IdentityServerConnectionString": "...",
  "OperationalConnectionString": "...",
  "AuditRecordsConnectionString": "...",
  "DataProtectionConnectionString": "...",
```

You can get away with just using the first 2 as the last 3 will default to the second one. See [here](../Configuration_and_Integration/Configuring_AdminUI.md) for more details.

Alternatively, you can write your own connection factory implementation to create connections using a different strategy. This opens up AdminUI to users who may have not been able to take advantage of it in the past - perhaps users who deploy AdminUI in Azure and authenticates with the database via an Azure AD token. 

### Changes
As mentioned above, AdminUI’s connection creation has historically been based purely on using connection strings.

AdminUI 6.7 adds a pluggable abstraction that allows for the decoupling from this old strategy.

The abstraction is `IDatabaseConnectionFactory` and it is initialized in AdminUI's service layer with the default implementation. The interface is defined thus:

```language-csharp
public interface IDatabaseConnectionFactory
{
     // Create connection for the Identity database (Users, Claim Types, Roles etc.)
    public DbConnection CreateIdentityConnection(bool migration = false);

    // Create connection for the IdentityServer database (Clients, Resources, Persited Grants etc.)
    public DbConnection CreateIdentityServerConnection(bool migration = false);

    // Create connection for the Auditing DbContext
    public DbConnection CreateAuditRecordsConnection(bool migration = false);

    // Create connection for the Persisted Grants DbContext
    public DbConnection CreateOperationalConnection(bool migration = false);
    
    // Creates connection for the DataProtectionKey DbContext
    public DbConnection CreateDataProtectionConnection(bool migration = false);
}
```

The `migration` parameter in each method is used to distinguish whether the method is called by AdminUI as part of its normal operation or when running migrations (in case you need to distinguish).

To change the default implementation, simply implement this interface and tell AdminUI to use it instead of the default.

##Starting point

To get started, take the result of the [NuGet Installation](../Installation/Installation_NuGet.md) and update the service registration to the following:

```language-csharp
builder.Services
    .AddAdminUI(
        options =>
        {
            options.DatabaseConnectionFactoryType = DatabaseConnectionFactoryType.Custom;
        })
    .WithConnectionFactory<MyCustomConnectionFactory>();
```

The `WithConnectionFactory<MyCustomConnectionFactory>()` method replaces the default connection factory implementation with that defined by `MyCustomConnectionFactory` class that you write.

That's it!

## Additional Considerations

If the custom connection factory has additional interfaces injected into its constructor that are unknown to AdminUI then these will have to be registered with the DI container in the usual way using `builder.Services`, for example, if your custom connection factory implementation looks like this:

```language-csharp
public class MyCustomConnectionFactory : IDatabaseConnectionFactory
{
    public MyCustomConnectionFactory(IConfiguration configuration, IMyOtherInterface myOtherInterface)
    {
        ...
    }

    ...
}
```

then the service registration might look like this:

```language-csharp
builder.Services.AddSingleton<IMyOtherInterface, MyOtherInterfaceImplementation>();
builder.Services
    .AddAdminUI(
        options =>
        {
            options.DatabaseConnectionFactoryType = DatabaseConnectionFactoryType.Custom;
        })
    .WithConnectionFactory<MyCustomConnectionFactory>();
```

## Sample code

A sample of how to replace the connection factory can be found on our [GitHub](https://github.com/RockSolidKnowledge/Samples.CustomIdentityStore/tree/main/Duende) in the `CustomDatabaseConnection` project. Remember to fill in the `CustomConnectionFactoryConnectionString` connection string and the license key in `appSettings.json`.
