Skip to content

Repository Options Configuration

Johelvis Guzman edited this page Mar 22, 2019 · 19 revisions

The repositories can be configure using the RepositoryOptions object, which can use ORM frameworks like Entity Framework. The RepositoryOptions object is built using the RepositoryOptionsBuilder.

Currently the following are supported:

Activity within a repository can be intercepted by registering an interceptor (This would be helpful if we need to update an object timestamp before adding it to the database or something)

var options = new RepositoryOptionsBuilder()
    .UseInterceptor(new AuditRepositoryInterceptor())
    .Options;
namespace ExampleApplicationDemo
{
    using DotNetToolkit.Repository.Configuration.Interceptors;
    using System;

    public class AuditRepositoryInterceptor : RepositoryInterceptorBase
    {
        private readonly string _user;

        public AuditRepositoryInterceptor(string loggedInUser)
        {
            _user = loggedInUser;
        }

        public override void AddExecuting<TEntity>(TEntity entity)
        {
            if (entity is IHaveTimeStamp haveStamp)
            {
                var currentTime = DateTime.UtcNow;

                haveStamp.CreateTime = currentTime;
                haveStamp.CreateUser = _user;
                haveStamp.ModTime = currentTime;
                haveStamp.ModUser = _user;
            }
        }

        public override void UpdateExecuting<TEntity>(TEntity entity)
        {
            if (entity is IHaveTimeStamp haveStamp)
            {
                var currentTime = DateTime.UtcNow;

                haveStamp.ModTime = currentTime;
                haveStamp.ModUser = _user;
            }
        }
    }
}

We can even register a logger provider which will output any logging being done in the repositories (this will include logging any raw SQL query string being executed)

var options = new RepositoryOptionsBuilder()
    .UseLoggerProvider(new ConsoleLoggerProvider())
    .Options;

We cab register a caching provider which allows for caching all query results within the repositories.

var options = new RepositoryOptionsBuilder()
    .UseCachingProvider(new InMemoryCacheProvider())
    .Options;

We can register a mapping provider which takes in a data reader object that is returned by an executed SQL query, and maps it into a new entity form. For more, please check out the Execute Raw Sql Queries guide.

var options = new RepositoryOptionsBuilder()
    .UseMapperProvider(new MapperProvider())
    .Options;

We can also use configuration files (which can be defined with interceptors). For more configuration details, please check out the Config File Setup guide.

var options = new RepositoryOptionsBuilder()
    .UseConfiguration() // for net451, it will use the App.config file
    .UseConfiguration(Microsoft.Extensions.Configuration.IConfiguration) // for .netstandard2_0
    .Options;
Clone this wiki locally