zoukankan      html  css  js  c++  java
  • Asp.net Core Webapi for MongoDB

    Asp.net Core Webapi for MongoDB

    创建asp.net core webapi 3.1

    1、 配置连接字符串

      "BookstoreDatabaseSettings": {
        "BooksCollectionName": "Books",
        "ConnectionString": "mongodb://localhost:27017",
        "DatabaseName": "BookstoreDb"
      },
    

    2、添加实体类

    新建一个Models文件夹,添加一个Book实体类

    public class Book
    {
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; }
    
        [BsonElement("Name")]
        public string BookName { get; set; }
    
        public decimal Price { get; set; }
    
        public string Category { get; set; }
    
        public string Author { get; set; }
    }
    

    3、添加Book的操作类

     public class BookstoreDatabaseSettings : IBookstoreDatabaseSettings
     {
         public string BooksCollectionName { get; set; }
         public string ConnectionString { get; set; }
         public string DatabaseName { get; set; }
     }
    
     public interface IBookstoreDatabaseSettings
     {
         string BooksCollectionName { get; set; }
         string ConnectionString { get; set; }
         string DatabaseName { get; set; }
     }
    

    4、添加services类

     public class BookService
     {
         private readonly IMongoCollection<Book> _books;
    
         public BookService(IBookstoreDatabaseSettings settings)
         {
             var client = new MongoClient(settings.ConnectionString);
             var database = client.GetDatabase(settings.DatabaseName);
    
             _books = database.GetCollection<Book>(settings.BooksCollectionName);
         }
    
         public List<Book> Get() =>
             _books.Find(book => true).ToList();
    
         public Book Get(string id) =>
             _books.Find<Book>(book => book.Id == id).FirstOrDefault();
    
         public Book Create(Book book)
         {
             _books.InsertOne(book);
             return book;
         }
    
         public void Update(string id, Book bookIn) =>
             _books.ReplaceOne(book => book.Id == id, bookIn);
    
         public void Remove(Book bookIn) =>
             _books.DeleteOne(book => book.Id == bookIn.Id);
    
         public void Remove(string id) =>
             _books.DeleteOne(book => book.Id == id);
     }
    
    

    5、注册服务

    在setup.cs ConfigureServices 里面注册服务

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<BookstoreDatabaseSettings>(
           Configuration.GetSection(nameof(BookstoreDatabaseSettings)));
        services.AddSingleton<IBookstoreDatabaseSettings>(sp =>
             sp.GetRequiredService<IOptions<BookstoreDatabaseSettings>>().Value);
        services.AddSingleton<BookService>();
    
        services.AddControllers();
    }
    
  • 相关阅读:
    java中一个类中没有公共的构造方法,则说明不能让外界去new对象。
    PyQt IDE 环境搭建
    移动App开发基本技术面
    创业公司的技术管理
    软件工程师职业道德规范和实践要求
    比较好的刷题网站推荐
    对于程序员在boss直聘求职的建议
    cocospod 安装和使用
    TabBarController和其他view无法建立Relationship segue的原因
    面试与反面试的一些问题
  • 原文地址:https://www.cnblogs.com/qzdd/p/12311116.html
Copyright © 2011-2022 走看看