MongoDBTools.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using Business.Core.Attributes;
  2. using Microsoft.Extensions.Options;
  3. using MongoDB.Driver;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace Business.Core.MongoDBHelper
  10. {
  11. /// <summary>
  12. /// MongoDB实现
  13. /// </summary>
  14. public class MongoDBTools<T> : IMongoDB<T> where T : class
  15. {
  16. public readonly IMongoCollection<T> mongoCollection;
  17. public IOptionsSnapshot<Config> _config;
  18. /// <summary>
  19. /// MongoDB帮助类
  20. /// </summary>
  21. /// <param name="config"></param>
  22. /// <exception cref="NotImplementedException"></exception>
  23. public MongoDBTools(IOptionsSnapshot<Config> config)
  24. {
  25. _config = config;
  26. //数据库链接
  27. MongoClient client = new MongoClient(config.Value.connectstring);
  28. CollectionNameAttribute collectonName = typeof(T).GetCustomAttributes(typeof(CollectionNameAttribute), true).FirstOrDefault() as CollectionNameAttribute;
  29. if (collectonName == null)
  30. {
  31. throw new NotImplementedException("请配置Attribute属性!");
  32. }
  33. //数据库
  34. var database = client.GetDatabase(collectonName.DatabaseName);
  35. //表名
  36. mongoCollection = database.GetCollection<T>(collectonName.CollectionName);
  37. }
  38. public Task<long> BulkInsert(List<T> documents)
  39. {
  40. throw new NotImplementedException();
  41. }
  42. public Task<List<T>> GetAll()
  43. {
  44. throw new NotImplementedException();
  45. }
  46. public Task<T> GetOneByID(long id)
  47. {
  48. throw new NotImplementedException();
  49. }
  50. public Task Inc(long id)
  51. {
  52. throw new NotImplementedException();
  53. }
  54. public Task InsertMany(List<T> documents)
  55. {
  56. throw new NotImplementedException();
  57. }
  58. public Task InsertOne(T document)
  59. {
  60. return mongoCollection.InsertOneAsync(document);
  61. }
  62. public Task<UpdateResult> UpdateMultiFields(T document, long id)
  63. {
  64. throw new NotImplementedException();
  65. }
  66. public Task<UpdateResult> UpdateOne(string name, long id)
  67. {
  68. throw new NotImplementedException();
  69. }
  70. }
  71. }