MongoDBTools.cs 2.4 KB

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