我应该如何注册使用MongoClient,Singleton或作用域的mongodb服务? [英] How should I register my mongodb service that uses the MongoClient, Singleton or scoped?

查看:147
本文介绍了我应该如何注册使用MongoClient,Singleton或作用域的mongodb服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mongodb构建带有ASP.NET核心的API,并且我拥有不同的服务,用户服务家庭服务等.我想知道我应该将每个服务注册为asp.net核心文档中提到的单例服务还是视范围而定.链接到存储库 https://github.com/krisk0su/apartments

I am building an API with ASP.NET core using Mongodb and i have different services user service home service and etc. I would like to know should i register every service as singleton as it is mentioned in asp.net core documention or as scoped. Link to repository https://github.com/krisk0su/apartments

UserService.cs

UserService.cs

public class UserService
{
    private readonly IMongoCollection<User> _books;
    private readonly IPasswordHasher _passwordService;

    public UserService(IBookstoreDatabaseSettings settings,  IPasswordHasher passwordService)
    {
        var client = new MongoClient(settings.ConnectionString);
        var database = client.GetDatabase(settings.DatabaseName);

        _books = database.GetCollection<User>(settings.UsersCollectionName);
        _passwordService = passwordService;
    }

    public List<User> Get() =>
        _books
        .Find(book => true)
        .ToList();

    public User Get(string id) =>
        _books.Find(user => user.Id == id).FirstOrDefault();

    public User Create(User user)
    {
        var password = this._passwordService.HashPassword(user.Password);
        user.Password = password;
        _books.InsertOne(user);
        return user;
    }
    public void Update(string id, User bookIn) =>
        _books.ReplaceOne(book => book.Id == id, bookIn);

    public void Remove(User bookIn) =>
        _books.DeleteOne(book => book.Id == bookIn.Id);

    public void Remove(string id) =>
        _books.DeleteOne(book => book.Id == id);
}

Startup.cs

Startup.cs

services.AddSingleton<UserService>();
            services.AddSingleton<BookService>();
            services.AddSingleton<AuthenticationService>();
            services.AddScoped<IPasswordHasher, PasswordHasher>();

推荐答案

MongoDB .NET驱动程序版本2.10的参考文档在参考>上进行了解释Mongo Client中的驱动程序>连接"页面重用部分:

The MongoDB .NET Driver reference documentation for version 2.10 explains on the Reference > Driver > Connecting page in the Mongo Client Re-use section that:

建议存储 MongoClient 实例(作为静态变量)或在具有单例生存期的IoC容器中作为全局实例.

It is recommended to store a MongoClient instance in a global place, either as a static variable or in an IoC container with a singleton lifetime.

关于Mongo数据库重用它没有提到单例生命期,但是它确实说"是线程安全的,并且可以安全地全局存储",因此我将其解释为意味着它可以如果这是实现所需的内容,则可以将其安全地存储为单例,但是如果您希望有另一个生存期,则不必将其安全存储.

With regards to Mongo Database Re-use it doesn't mention a singleton lifetime but it does say it "is thread-safe and is safe to be stored globally", so I would interpret that to mean it can be stored safely as a singleton if that's what your implementation desired, but it doesn't need to be if you prefer another lifetime.

IMongoDatabase 的实现> MongoClient 是线程安全的,可以安全地全局存储或存储在IoC容器中.

The implementation of IMongoDatabase provided by a MongoClient is thread-safe and is safe to be stored globally or in an IoC container.

关于Mongo Collection

It's the same with regards to Mongo Collection Re-use:

IMongoCollection< TDocument> 最终由 MongoClient 是线程安全的,可以安全地全局存储或存储在IoC容器中.

The implementation of IMongoCollection<TDocument> ultimately provided by a MongoClient is thread-safe and is safe to be stored globally or in an IoC container.

所以我再次解释说,寿命的选择取决于您的特定要求.

So again I'd interpret that to mean the choice of lifetime is up to your specific requirements.

似乎只有 MongoClient 带有建议才能使用单例生命.

It seems it's only the MongoClient that carries a recommendation to use a singleton lifetime.

这篇关于我应该如何注册使用MongoClient,Singleton或作用域的mongodb服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆