Disposer.cs 633 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using MongoDB.Bson.Serialization.Attributes;
  3. namespace Model
  4. {
  5. [BsonKnownTypes(typeof(Entity))]
  6. [BsonKnownTypes(typeof(Component))]
  7. public abstract class Disposer : Object, IDisposable
  8. {
  9. [BsonIgnoreIfDefault]
  10. [BsonDefaultValue(1L)]
  11. [BsonElement]
  12. [BsonId]
  13. public long Id { get; set; }
  14. [BsonIgnore]
  15. public bool IsFromPool { get; set; }
  16. protected Disposer()
  17. {
  18. this.Id = IdGenerater.GenerateId();
  19. }
  20. protected Disposer(long id)
  21. {
  22. this.Id = id;
  23. }
  24. public virtual void Dispose()
  25. {
  26. this.Id = 0;
  27. if (this.IsFromPool)
  28. {
  29. ObjectPool.Instance.Recycle(this);
  30. }
  31. }
  32. }
  33. }