Skip to content

Repository Options Configuration

Johelvis Guzman edited this page Jan 10, 2022 · 19 revisions

The repositories can be configured 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>(RepositoryInterceptionContext<TEntity> interceptionContext)
        {
            if (interceptionContext.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>(RepositoryInterceptionContext<TEntity> interceptionContext)
        {
            if (interceptionContext.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 if supported by the context provider)

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

We can register a caching provider which allows for caching all query results within the repositories. For more, please check out the Caching section to see all caching providers that are included.

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

We can also configure the repositories from a configuration file. For more configuration details, please check out the Config File Setup guide.

var options = new RepositoryOptionsBuilder()
    .UseConfiguration()
    .Options;
Clone this wiki locally