DBComponent.cs 812 B

1234567891011121314151617181920212223242526272829303132333435
  1. using MongoDB.Driver;
  2. namespace ETModel
  3. {
  4. [ObjectSystem]
  5. public class DbComponentSystem : AwakeSystem<DBComponent>
  6. {
  7. public override void Awake(DBComponent self)
  8. {
  9. self.Awake();
  10. }
  11. }
  12. /// <summary>
  13. /// 连接mongodb
  14. /// </summary>
  15. public class DBComponent : Component
  16. {
  17. public MongoClient mongoClient;
  18. public IMongoDatabase database;
  19. public void Awake()
  20. {
  21. DBConfig config = Game.Scene.GetComponent<StartConfigComponent>().StartConfig.GetComponent<DBConfig>();
  22. string connectionString = config.ConnectionString;
  23. mongoClient = new MongoClient(connectionString);
  24. this.database = this.mongoClient.GetDatabase(config.DBName);
  25. }
  26. public IMongoCollection<ComponentWithId> GetCollection(string name)
  27. {
  28. return this.database.GetCollection<ComponentWithId>(name);
  29. }
  30. }
  31. }