Skip to content

Config File Setup

Johelvis Guzman edited this page Oct 19, 2021 · 18 revisions

If you wish to use the configuration files to configure the repositories, please follow these steps:

Element Definitions

Element Description
defaultContextFactory A typed element that defines the default repository context factory to use
interceptors Accepts a collection of typed interceptor elements
interceptor A typed element that defines an interceptor that can intercept activity within the repository
loggingProvider A typed element that defines a logging provider for logging activity within the repository
cachingProvider A typed element that defines a caching provider for caching query results within the repository

Each element can accept a collection of parameters that can be used to construct the type.

Element Description
parameters Accepts a collection of parameter elements
parameter A name-value element

XML Configuration File Template Example

In order for the repositories to read data from the App.config the user will need to add a custom section handler and a configuration section:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="repository" type="DotNetToolkit.Repository.Internal.ConfigFile.ConfigurationSection, DotNetToolkit.Repository" />
  </configSections>
  <repository>
    <defaultContextFactory type="DotNetToolkit.Repository.InMemory.Internal.InMemoryRepositoryContextFactory, DotNetToolkit.Repository.InMemory">
      <param name="ignoreTransactionWarning" value="True" />
      <param name="databaseName" value="__InMemoryDatabaseName__" />
    </defaultContextFactory>
    <loggingProvider type="DotNetToolkit.Repository.Configuration.Logging.ConsoleLoggerProvider, DotNetToolkit.Repository">
      <param name="minLogLevel" value="Debug" />
    </loggingProvider>
    <cachingProvider type="DotNetToolkit.Repository.Test.Data.TestCacheProvider, DotNetToolkit.Repository.Test">
      <param name="expiry" value="00:00:30" />
    </cachingProvider>
    <interceptors>
      <interceptor type="DotNetToolkit.Repository.Test.Data.TestRepositoryInterceptor, DotNetToolkit.Repository.Test">
        <param name="p1" value="random param" />
        <param name="p2" value="True" />
      </interceptor>
    </interceptors>
  </repository>
</configuration>
var options = new RepositoryOptionsBuilder()
    .UseConfiguration() // it will use the App.config file
    .UseConfiguration(fileName) // it will use an arbitrary file path to load the xml file
    .Options;

JSON Configuration File Template Example

In order for the repositories to read data from the appsettings.json file the user will need to add a section to the file:

{
  "repository": {
    "defaultContextFactory": {
      "type": "DotNetToolkit.Repository.InMemory.Internal.InMemoryRepositoryContextFactory, DotNetToolkit.Repository.InMemory",
      "parameters": {
        "ignoreTransactionWarning": true,
        "databaseName": "__InMemoryDatabaseName__"
      }
    },
    "loggingProvider": {
      "type": "DotNetToolkit.Repository.Configuration.Logging.ConsoleLoggerProvider, DotNetToolkit.Repository",
      "parameters": {
        "minLogLevel": "Debug"
      }
    },
    "cachingProvider": {
      "type": "DotNetToolkit.Repository.Caching.InMemory.InMemoryCacheProvider, DotNetToolkit.Repository.Caching.InMemory",
      "parameters": {
        "expiry": "00:00:30"
      }
    },
    "interceptors": [
      {
        "type": "DotNetToolkit.Repository.Integration.Test.Data.TestRepositoryInterceptor, DotNetToolkit.Repository.Integration.Test",
        "parameters": {
          "randomParameter": "random param value",
        }
      }
    ]
  }
}
var options = new RepositoryOptionsBuilder()
    .UseConfiguration(Microsoft.Extensions.Configuration.IConfiguration) // it will use a configuration section
    .Options;

Additionally, any element type that is defined in the configuration file can be resolved by using the RepositoryDependencyResolver.