DBQueryTask.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using MongoDB.Driver;
  3. namespace ETModel
  4. {
  5. [ObjectSystem]
  6. public class DBQueryTaskSystem : AwakeSystem<DBQueryTask, string, ETTaskCompletionSource<ComponentWithId>>
  7. {
  8. public override void Awake(DBQueryTask self, string collectionName, ETTaskCompletionSource<ComponentWithId> tcs)
  9. {
  10. self.CollectionName = collectionName;
  11. self.Tcs = tcs;
  12. }
  13. }
  14. public sealed class DBQueryTask : DBTask
  15. {
  16. public string CollectionName { get; set; }
  17. public ETTaskCompletionSource<ComponentWithId> Tcs { get; set; }
  18. public override async ETTask Run()
  19. {
  20. DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
  21. try
  22. {
  23. // 执行查询数据库任务
  24. IAsyncCursor<ComponentWithId> cursor = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == this.Id);
  25. ComponentWithId component = await cursor.FirstOrDefaultAsync();
  26. this.Tcs.SetResult(component);
  27. }
  28. catch (Exception e)
  29. {
  30. this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {Id}", e));
  31. }
  32. }
  33. }
  34. }