jeudi 16 février 2023

Dapper ContextClass DI scope and repository class scopes best practices

What is the best practices to register DapperContext.cs and repository classes. What scope should be used for Context class and what scope should used repository classes ?

This is my current code. DbContext Class

    private readonly ICoreLogger<AntiguaCacheContext> _logger;
    private readonly DatabaseOptions _options;
    private readonly string _connectionString;

    public IDbConnection CreateConnection() => new SqlConnection(_connectionString);

    public DbCacheContext(IOptions<DatabaseOptions> options, ICoreLogger<DbContext> logger)
    {
        _logger = logger;
        _options = options.Value;
        _connectionString = GetConnectionString(ConnectionStrings.DbContext_Connection_String_Key);
    }

    private string GetConnectionString(string key)
    {
       //get string
    }

And i adding depdendencies like this

        public static void ConfigureDataAccessLayer(this IServiceCollection services)
        {
            services.AddSingleton<DbContext>();

            services.AddTransient<IRepository, Repository>();
        }

Hos should be relation betwwen context and repo injections?Context always sjhould be singleton ? Reposr transient or Scoped ? and so onn...what is the best practices.

Aucun commentaire:

Enregistrer un commentaire