Skip to content

Adds MongoRepository as a registered service with the .NET Core dependency resolver

Notifications You must be signed in to change notification settings

johnknoop/MongoRepository.DotNetCoreDi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

MongoRepository.DotNetCoreDi

Adds MongoRepository as a registered service with the .NET Core dependency resolver

Install

PM> Install-Package JohnKnoop.MongoRepository.DotNetCoreDi

Configuration

In the ConfigureServices method of your Startup class, simply call AddRepositories on the service collection, and pass in your IMongoClient instance.

public void ConfigureServices(IServiceCollection services)
    // Register IRepository<T>
    services.AddRepositories(mongoClient);

    // Register tenant key resolver
    services.AddScoped<ResolveTenantKey>(provider => () =>
    {
        // Pull the tenant key from wherever you keep it.
        // For example from user claim:

        var httpContextAccessor = provider.GetService<IHttpContextAccessor>();
        var user = httpContextAccessor.HttpContext?.User as ClaimsIdentity;
        return user?.Identity.Claims.SingleOrDefault(x => x.Type == "TenantKey")?.Value;
    });
}

Then you can accept IRepository<AnyMappedType> as constructor parameters.

About

Adds MongoRepository as a registered service with the .NET Core dependency resolver

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages