DBProxyComponentSystem.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Threading;
  5. using ETModel;
  6. using MongoDB.Bson;
  7. using MongoDB.Bson.Serialization;
  8. using MongoDB.Driver;
  9. namespace ETHotfix
  10. {
  11. [ObjectSystem]
  12. public class DbProxyComponentSystem : AwakeSystem<DBProxyComponent>
  13. {
  14. public override void Awake(DBProxyComponent self)
  15. {
  16. self.Awake();
  17. }
  18. }
  19. /// <summary>
  20. /// 用来与数据库操作代理
  21. /// </summary>
  22. public static class DBProxyComponentEx
  23. {
  24. public static void Awake(this DBProxyComponent self)
  25. {
  26. StartConfig dbStartConfig = StartConfigComponent.Instance.DBConfig;
  27. self.dbAddress = dbStartConfig.GetComponent<InnerConfig>().IPEndPoint;
  28. }
  29. public static async ETTask Save(this DBProxyComponent self, ComponentWithId component)
  30. {
  31. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  32. await session.Call(new DBSaveRequest { Component = component });
  33. }
  34. public static async ETTask SaveBatch(this DBProxyComponent self, List<ComponentWithId> components)
  35. {
  36. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  37. await session.Call(new DBSaveBatchRequest { Components = components });
  38. }
  39. public static async ETTask Save(this DBProxyComponent self, ComponentWithId component, CancellationToken cancellationToken)
  40. {
  41. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  42. await session.Call(new DBSaveRequest { Component = component }, cancellationToken);
  43. }
  44. public static async ETVoid SaveLog(this DBProxyComponent self, ComponentWithId component)
  45. {
  46. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  47. await session.Call(new DBSaveRequest { Component = component, CollectionName = "Log" });
  48. }
  49. public static async ETTask<T> Query<T>(this DBProxyComponent self, long id) where T: ComponentWithId
  50. {
  51. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  52. DBQueryResponse dbQueryResponse = (DBQueryResponse)await session.Call(new DBQueryRequest { CollectionName = typeof(T).Name, Id = id });
  53. return (T)dbQueryResponse.Component;
  54. }
  55. /// <summary>
  56. /// 根据查询表达式查询
  57. /// </summary>
  58. /// <param name="self"></param>
  59. /// <param name="exp"></param>
  60. /// <typeparam name="T"></typeparam>
  61. /// <returns></returns>
  62. public static async ETTask<List<ComponentWithId>> Query<T>(this DBProxyComponent self, Expression<Func<T ,bool>> exp) where T: ComponentWithId
  63. {
  64. ExpressionFilterDefinition<T> filter = new ExpressionFilterDefinition<T>(exp);
  65. IBsonSerializerRegistry serializerRegistry = BsonSerializer.SerializerRegistry;
  66. IBsonSerializer<T> documentSerializer = serializerRegistry.GetSerializer<T>();
  67. string json = filter.Render(documentSerializer, serializerRegistry).ToJson();
  68. return await self.Query<T>(json);
  69. }
  70. public static async ETTask<List<ComponentWithId>> Query<T>(this DBProxyComponent self, List<long> ids) where T : ComponentWithId
  71. {
  72. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  73. DBQueryBatchResponse dbQueryBatchResponse = (DBQueryBatchResponse)await session.Call(new DBQueryBatchRequest { CollectionName = typeof(T).Name, IdList = ids });
  74. return dbQueryBatchResponse.Components;
  75. }
  76. /// <summary>
  77. /// 根据json查询条件查询
  78. /// </summary>
  79. /// <param name="self"></param>
  80. /// <param name="json"></param>
  81. /// <typeparam name="T"></typeparam>
  82. /// <returns></returns>
  83. public static async ETTask<List<ComponentWithId>> Query<T>(this DBProxyComponent self, string json) where T : ComponentWithId
  84. {
  85. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  86. DBQueryJsonResponse dbQueryJsonResponse = (DBQueryJsonResponse)await session.Call(new DBQueryJsonRequest { CollectionName = typeof(T).Name, Json = json });
  87. return dbQueryJsonResponse.Components;
  88. }
  89. }
  90. }