1- using GameFrameX . Utility . Log ;
2-
3- namespace GameFrameX . Apps ;
4-
5- public class DictionaryRepresentationConvention : ConventionBase , IMemberMapConvention
6- {
7- private readonly DictionaryRepresentation _dictionaryRepresentation ;
8-
9- public DictionaryRepresentationConvention ( DictionaryRepresentation dictionaryRepresentation = DictionaryRepresentation . ArrayOfDocuments )
10- {
11- _dictionaryRepresentation = dictionaryRepresentation ;
12- }
13-
14- public void Apply ( BsonMemberMap memberMap )
15- {
16- var serializer = memberMap . GetSerializer ( ) ;
17- if ( serializer is IDictionaryRepresentationConfigurable dictionaryRepresentationConfigurable )
18- {
19- var reconfiguredSerializer = dictionaryRepresentationConfigurable . WithDictionaryRepresentation ( _dictionaryRepresentation ) ;
20- memberMap . SetSerializer ( reconfiguredSerializer ) ;
21- }
22- }
23- }
24-
25- public class EmptyContainerSerializeMethodConvention : ConventionBase , IMemberMapConvention
26- {
27- public void Apply ( BsonMemberMap memberMap )
28- {
29- if ( memberMap . MemberType . IsGenericType )
30- {
31- var genType = memberMap . MemberType . GetGenericTypeDefinition ( ) ;
32- if ( genType == typeof ( List < > ) )
33- {
34- memberMap . SetShouldSerializeMethod ( o =>
35- {
36- var value = memberMap . Getter ( o ) ;
37- if ( value is IList list )
38- {
39- return list != null && list . Count > 0 ;
40- }
41-
42- return true ;
43- } ) ;
44- }
45-
46- else if ( genType == typeof ( ConcurrentDictionary < , > ) || genType == typeof ( Dictionary < , > ) )
47- {
48- memberMap . SetShouldSerializeMethod ( o =>
49- {
50- if ( o != null )
51- {
52- var value = memberMap . Getter ( o ) ;
53- if ( value != null )
54- {
55- var countProperty = value . GetType ( ) . GetProperty ( "Count" ) ;
56- if ( countProperty != null )
57- {
58- var count = ( int ) countProperty . GetValue ( value , null ) ;
59- return count > 0 ;
60- }
61- }
62- }
63-
64- return true ;
65- } ) ;
66- }
67- }
68- }
69- }
70-
71- public static class BsonClassMapHelper
72- {
73- public static void SetConvention ( )
74- {
75- ConventionRegistry . Register ( nameof ( DictionaryRepresentationConvention ) ,
76- new ConventionPack { new DictionaryRepresentationConvention ( ) , } , _ => true ) ;
77-
78- ConventionRegistry . Register ( nameof ( EmptyContainerSerializeMethodConvention ) ,
79- new ConventionPack { new EmptyContainerSerializeMethodConvention ( ) , } , _ => true ) ;
80- }
81-
82- /// <summary>
83- /// 提前注册,简化多态类型处理
84- /// </summary>
85- /// <param name="assembly"></param>
86- public static void RegisterAllClass ( Assembly assembly )
87- {
88- var types = assembly . GetTypes ( ) ;
89- foreach ( var t in types )
90- {
91- try
92- {
93- if ( ! BsonClassMap . IsClassMapRegistered ( t ) )
94- {
95- RegisterClass ( t ) ;
96- }
97- }
98- catch ( Exception e )
99- {
100- LogHelper . Error ( e ) ;
101- }
102- }
103- }
104-
105- public static void RegisterClass ( Type t )
106- {
107- var bsonClassMap = new BsonClassMap ( t ) ;
108- bsonClassMap . AutoMap ( ) ;
109- bsonClassMap . SetIgnoreExtraElements ( true ) ;
110- bsonClassMap . SetIgnoreExtraElementsIsInherited ( true ) ;
111- BsonClassMap . RegisterClassMap ( bsonClassMap ) ;
112- }
1+ using GameFrameX . Foundation . Logger ;
2+
3+ namespace GameFrameX . Apps ;
4+
5+ public class DictionaryRepresentationConvention : ConventionBase , IMemberMapConvention
6+ {
7+ private readonly DictionaryRepresentation _dictionaryRepresentation ;
8+
9+ public DictionaryRepresentationConvention ( DictionaryRepresentation dictionaryRepresentation = DictionaryRepresentation . ArrayOfDocuments )
10+ {
11+ _dictionaryRepresentation = dictionaryRepresentation ;
12+ }
13+
14+ public void Apply ( BsonMemberMap memberMap )
15+ {
16+ var serializer = memberMap . GetSerializer ( ) ;
17+ if ( serializer is IDictionaryRepresentationConfigurable dictionaryRepresentationConfigurable )
18+ {
19+ var reconfiguredSerializer = dictionaryRepresentationConfigurable . WithDictionaryRepresentation ( _dictionaryRepresentation ) ;
20+ memberMap . SetSerializer ( reconfiguredSerializer ) ;
21+ }
22+ }
23+ }
24+
25+ public class EmptyContainerSerializeMethodConvention : ConventionBase , IMemberMapConvention
26+ {
27+ public void Apply ( BsonMemberMap memberMap )
28+ {
29+ if ( memberMap . MemberType . IsGenericType )
30+ {
31+ var genType = memberMap . MemberType . GetGenericTypeDefinition ( ) ;
32+ if ( genType == typeof ( List < > ) )
33+ {
34+ memberMap . SetShouldSerializeMethod ( o =>
35+ {
36+ var value = memberMap . Getter ( o ) ;
37+ if ( value is IList list )
38+ {
39+ return list != null && list . Count > 0 ;
40+ }
41+
42+ return true ;
43+ } ) ;
44+ }
45+
46+ else if ( genType == typeof ( ConcurrentDictionary < , > ) || genType == typeof ( Dictionary < , > ) )
47+ {
48+ memberMap . SetShouldSerializeMethod ( o =>
49+ {
50+ if ( o != null )
51+ {
52+ var value = memberMap . Getter ( o ) ;
53+ if ( value != null )
54+ {
55+ var countProperty = value . GetType ( ) . GetProperty ( "Count" ) ;
56+ if ( countProperty != null )
57+ {
58+ var count = ( int ) countProperty . GetValue ( value , null ) ;
59+ return count > 0 ;
60+ }
61+ }
62+ }
63+
64+ return true ;
65+ } ) ;
66+ }
67+ }
68+ }
69+ }
70+
71+ public static class BsonClassMapHelper
72+ {
73+ public static void SetConvention ( )
74+ {
75+ ConventionRegistry . Register ( nameof ( DictionaryRepresentationConvention ) ,
76+ new ConventionPack { new DictionaryRepresentationConvention ( ) , } , _ => true ) ;
77+
78+ ConventionRegistry . Register ( nameof ( EmptyContainerSerializeMethodConvention ) ,
79+ new ConventionPack { new EmptyContainerSerializeMethodConvention ( ) , } , _ => true ) ;
80+ }
81+
82+ /// <summary>
83+ /// 提前注册,简化多态类型处理
84+ /// </summary>
85+ /// <param name="assembly"></param>
86+ public static void RegisterAllClass ( Assembly assembly )
87+ {
88+ var types = assembly . GetTypes ( ) ;
89+ foreach ( var t in types )
90+ {
91+ try
92+ {
93+ if ( ! BsonClassMap . IsClassMapRegistered ( t ) )
94+ {
95+ RegisterClass ( t ) ;
96+ }
97+ }
98+ catch ( Exception e )
99+ {
100+ LogHelper . Error ( e ) ;
101+ }
102+ }
103+ }
104+
105+ public static void RegisterClass ( Type t )
106+ {
107+ var bsonClassMap = new BsonClassMap ( t ) ;
108+ bsonClassMap . AutoMap ( ) ;
109+ bsonClassMap . SetIgnoreExtraElements ( true ) ;
110+ bsonClassMap . SetIgnoreExtraElementsIsInherited ( true ) ;
111+ BsonClassMap . RegisterClassMap ( bsonClassMap ) ;
112+ }
113113}
0 commit comments