1
- using GameFrameX . DataBase . Abstractions ;
2
- using GameFrameX . DataBase . Storage ;
3
- using GameFrameX . Foundation . Json ;
4
- using MongoDB . Entities ;
5
-
6
- namespace GameFrameX . DataBase ;
7
-
8
- /// <summary>
9
- /// 缓存数据对象
10
- /// </summary>
11
- public abstract class BaseCacheState : ICacheState , IEntity
12
- {
13
- /// <summary>
14
- /// 唯一ID
15
- /// </summary>
16
- public virtual long Id { get ; set ; }
17
-
18
- /// <summary>
19
- /// 获取数据对象是否修改
20
- /// </summary>
21
- public virtual bool IsModify ( )
22
- {
23
- return IsChanged ( ) . isChanged ;
24
- }
25
-
26
- /// <summary>
27
- /// 是否删除
28
- /// </summary>
29
- public virtual bool IsDeleted { get ; set ; }
30
-
31
- /// <summary>
32
- /// 删除时间
33
- /// </summary>
34
- public virtual long DeleteTime { get ; set ; }
35
-
36
- /// <summary>
37
- /// 创建人
38
- /// </summary>
39
- public virtual long CreateId { get ; set ; }
40
-
41
- /// <summary>
42
- /// 创建时间
43
- /// </summary>
44
- public virtual long CreateTime { get ; set ; }
45
-
46
- /// <summary>
47
- /// 更新次数
48
- /// </summary>
49
- public virtual int UpdateCount { get ; set ; }
50
-
51
- /// <summary>
52
- /// 更新时间
53
- /// </summary>
54
- public virtual long UpdateTime { get ; set ; }
55
-
56
- /// <summary>
57
- /// </summary>
58
- /// <returns></returns>
59
- public override string ToString ( )
60
- {
61
- return JsonHelper . Serialize ( this ) ;
62
- }
63
-
64
- #region hash
65
-
66
- private StateHash _stateHash ;
67
-
68
-
69
- /// <summary>
70
- /// 用于在对象从数据库加载后进行一些特定的处理,如初始化数据或设置状态。
71
- /// </summary>
72
- /// <param name="isNew"></param>
73
- public virtual void LoadFromDbPostHandler ( bool isNew )
74
- {
75
- _stateHash = new StateHash ( this , isNew ) ;
76
- }
77
-
78
- /// <summary>
79
- /// 是否修改
80
- /// </summary>
81
- /// <returns></returns>
82
- public virtual ( bool isChanged , byte [ ] data ) IsChanged ( )
83
- {
84
- return _stateHash . IsChanged ( ) ;
85
- }
86
-
87
- /// <summary>
88
- /// 是否由ID引起的变化
89
- /// </summary>
90
- /// <returns></returns>
91
- public virtual ( bool isChanged , long stateId , byte [ ] data ) IsChangedWithId ( )
92
- {
93
- var res = _stateHash . IsChanged ( ) ;
94
- return ( res . Item1 , Id , res . Item2 ) ;
95
- }
96
-
97
- /// <summary>
98
- /// 仅DBModel.Mongodb时调用
99
- /// </summary>
100
- public virtual void BeforeSaveToDb ( )
101
- {
102
- // var db = GameDb.As<RocksDBConnection>().CurDataBase;
103
- // var table = db.GetTable<SaveTimestamp>();
104
- // var saveState = new SaveTimestamp
105
- // {
106
- // //此处使用UTC时间
107
- // Timestamp = TimeUtils.CurrentTimeMillisUTC(),
108
- // StateName = GetType().FullName,
109
- // StateId = Id.ToString(),
110
- // };
111
- // table.Set(saveState.Key, saveState);
112
- }
113
-
114
-
115
- /// <summary>
116
- /// 在对象保存到数据库后调用的方法,可以进行一些后续处理。
117
- /// </summary>
118
- public void SaveToDbPostHandler ( )
119
- {
120
- _stateHash . SaveToDbPostHandler ( ) ;
121
- }
122
-
123
- /// <summary>
124
- /// 将对象序列化转换为字节数组
125
- /// </summary>
126
- /// <returns></returns>
127
- public abstract byte [ ] ToBytes ( ) ;
128
-
129
- #endregion
130
-
131
- /// <summary>
132
- /// Generate and return a new ID from this method. It will be used when saving new entities that don't have their ID set.
133
- /// I.e. if an entity has a default ID value (determined by calling <see cref="M:MongoDB.Entities.IEntity.HasDefaultID" /> method),
134
- /// this method will be called for obtaining a new ID value. If you're not doing custom ID generation, simply do
135
- /// <c>return ObjectId.GenerateNewId().ToString()</c>
136
- /// </summary>
137
- public object GenerateNewID ( )
138
- {
139
- throw new NotImplementedException ( ) ;
140
- }
141
-
142
- /// <summary>
143
- /// When saving entities, this method will be called in order to determine if <see cref="M:MongoDB.Entities.IEntity.GenerateNewID" /> needs to be called.
144
- /// If this method returns <c>'true'</c>, <see cref="M:MongoDB.Entities.IEntity.GenerateNewID" /> method is called and the ID (primary key) of the entity is populated.
145
- /// If <c>'false'</c> is returned, it is assumed that ID generation is not required and the entity already has a non-default ID value.
146
- /// </summary>
147
- public bool HasDefaultID ( )
148
- {
149
- return Id == 0 ;
150
- }
1
+ using GameFrameX . DataBase . Abstractions ;
2
+ using GameFrameX . DataBase . Storage ;
3
+ using GameFrameX . Foundation . Json ;
4
+ using GameFrameX . Utility . Extensions ;
5
+ using MongoDB . Entities ;
6
+
7
+ namespace GameFrameX . DataBase ;
8
+
9
+ /// <summary>
10
+ /// 缓存数据对象
11
+ /// </summary>
12
+ public abstract class BaseCacheState : ICacheState , IEntity
13
+ {
14
+ /// <summary>
15
+ /// 唯一ID
16
+ /// </summary>
17
+ public virtual long Id { get ; set ; }
18
+
19
+ /// <summary>
20
+ /// 获取数据对象是否修改
21
+ /// </summary>
22
+ public virtual bool IsModify ( )
23
+ {
24
+ return IsChanged ( ) . isChanged ;
25
+ }
26
+
27
+ /// <summary>
28
+ /// 是否删除
29
+ /// </summary>
30
+ public virtual bool IsDeleted { get ; set ; }
31
+
32
+ /// <summary>
33
+ /// 删除时间
34
+ /// </summary>
35
+ public virtual long DeleteTime { get ; set ; }
36
+
37
+ /// <summary>
38
+ /// 创建人
39
+ /// </summary>
40
+ public virtual long CreateId { get ; set ; }
41
+
42
+ /// <summary>
43
+ /// 创建时间
44
+ /// </summary>
45
+ public virtual long CreateTime { get ; set ; }
46
+
47
+ /// <summary>
48
+ /// 更新次数
49
+ /// </summary>
50
+ public virtual int UpdateCount { get ; set ; }
51
+
52
+ /// <summary>
53
+ /// 更新时间
54
+ /// </summary>
55
+ public virtual long UpdateTime { get ; set ; }
56
+
57
+ /// <summary>
58
+ /// </summary>
59
+ /// <returns></returns>
60
+ public override string ToString ( )
61
+ {
62
+ return JsonHelper . Serialize ( this ) ;
63
+ }
64
+
65
+ #region hash
66
+
67
+ private StateHash _stateHash ;
68
+
69
+
70
+ /// <summary>
71
+ /// 用于在对象从数据库加载后进行一些特定的处理,如初始化数据或设置状态。
72
+ /// </summary>
73
+ /// <param name="isNew">是否是新创建的实例,true表示是新创建的实例,false表示不是</param>
74
+ public virtual void LoadFromDbPostHandler ( bool isNew = false )
75
+ {
76
+ _stateHash = new StateHash ( this , isNew ) ;
77
+ }
78
+
79
+ /// <summary>
80
+ /// 是否修改
81
+ /// </summary>
82
+ /// <returns></returns>
83
+ public virtual ( bool isChanged , byte [ ] data ) IsChanged ( )
84
+ {
85
+ CheckStateHash ( ) ;
86
+ return _stateHash . IsChanged ( ) ;
87
+ }
88
+
89
+ /// <summary>
90
+ /// 是否由ID引起的变化
91
+ /// </summary>
92
+ /// <returns></returns>
93
+ public virtual ( bool isChanged , long stateId , byte [ ] data ) IsChangedWithId ( )
94
+ {
95
+ CheckStateHash ( ) ;
96
+ var res = _stateHash . IsChanged ( ) ;
97
+ return ( res . Item1 , Id , res . Item2 ) ;
98
+ }
99
+
100
+ /// <summary>
101
+ /// 仅DBModel.Mongodb时调用
102
+ /// </summary>
103
+ public virtual void BeforeSaveToDb ( )
104
+ {
105
+ // var db = GameDb.As<RocksDBConnection>().CurDataBase;
106
+ // var table = db.GetTable<SaveTimestamp>();
107
+ // var saveState = new SaveTimestamp
108
+ // {
109
+ // //此处使用UTC时间
110
+ // Timestamp = TimeUtils.CurrentTimeMillisUTC(),
111
+ // StateName = GetType().FullName,
112
+ // StateId = Id.ToString(),
113
+ // };
114
+ // table.Set(saveState.Key, saveState);
115
+ }
116
+
117
+
118
+ /// <summary>
119
+ /// 在对象保存到数据库后调用的方法,可以进行一些后续处理。
120
+ /// </summary>
121
+ public void SaveToDbPostHandler ( )
122
+ {
123
+ CheckStateHash ( ) ;
124
+ _stateHash . SaveToDbPostHandler ( ) ;
125
+ }
126
+
127
+ /// <summary>
128
+ /// 检查StateHash对象是否存在
129
+ /// </summary>
130
+ private void CheckStateHash ( )
131
+ {
132
+ if ( _stateHash . IsNotNull ( ) )
133
+ {
134
+ return ;
135
+ }
136
+
137
+ LoadFromDbPostHandler ( false ) ;
138
+ }
139
+
140
+ /// <summary>
141
+ /// 将对象序列化转换为字节数组
142
+ /// </summary>
143
+ /// <returns></returns>
144
+ public abstract byte [ ] ToBytes ( ) ;
145
+
146
+ #endregion
147
+
148
+ /// <summary>
149
+ /// Generate and return a new ID from this method. It will be used when saving new entities that don't have their ID set.
150
+ /// I.e. if an entity has a default ID value (determined by calling <see cref="M:MongoDB.Entities.IEntity.HasDefaultID" /> method),
151
+ /// this method will be called for obtaining a new ID value. If you're not doing custom ID generation, simply do
152
+ /// <c>return ObjectId.GenerateNewId().ToString()</c>
153
+ /// </summary>
154
+ public object GenerateNewID ( )
155
+ {
156
+ throw new NotImplementedException ( ) ;
157
+ }
158
+
159
+ /// <summary>
160
+ /// When saving entities, this method will be called in order to determine if <see cref="M:MongoDB.Entities.IEntity.GenerateNewID" /> needs to be called.
161
+ /// If this method returns <c>'true'</c>, <see cref="M:MongoDB.Entities.IEntity.GenerateNewID" /> method is called and the ID (primary key) of the entity is populated.
162
+ /// If <c>'false'</c> is returned, it is assumed that ID generation is not required and the entity already has a non-default ID value.
163
+ /// </summary>
164
+ public bool HasDefaultID ( )
165
+ {
166
+ return Id == 0 ;
167
+ }
151
168
}
0 commit comments