Skip to content

Commit f7b08ce

Browse files
committed
Some cleanup
1 parent 551b974 commit f7b08ce

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/Foundatio/Messaging/ITypeNameSerializer.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ public interface ITypeNameSerializer {
1212
}
1313

1414
public class DefaultTypeNameSerializer : ITypeNameSerializer {
15-
private readonly Dictionary<string, Type> _typeNameOverrides;
1615
private readonly ILogger _logger;
16+
private readonly Dictionary<string, Type> _typeNameOverrides;
17+
private readonly ConcurrentDictionary<Type, string> _typeNameCache = new ConcurrentDictionary<Type, string>();
18+
private readonly ConcurrentDictionary<string, Type> _typeCache = new ConcurrentDictionary<string, Type>();
1719

1820
public DefaultTypeNameSerializer(ILogger logger = null, IDictionary<string, Type> typeNameOverrides = null) {
1921
_logger = logger ?? NullLogger.Instance;
20-
_typeNameOverrides = typeNameOverrides != null ? new Dictionary<string, Type>(typeNameOverrides) : new Dictionary<string, Type>();
22+
if (typeNameOverrides != null)
23+
_typeNameOverrides = new Dictionary<string, Type>(typeNameOverrides);
2124
}
2225

23-
private readonly ConcurrentDictionary<string, Type> _knownMessageTypesCache = new ConcurrentDictionary<string, Type>();
2426
public Type Deserialize(string typeName) {
25-
return _knownMessageTypesCache.GetOrAdd(typeName, newTypeName => {
27+
return _typeCache.GetOrAdd(typeName, newTypeName => {
2628
if (_typeNameOverrides != null && _typeNameOverrides.ContainsKey(newTypeName))
2729
return _typeNameOverrides[newTypeName];
2830

@@ -37,12 +39,13 @@ public Type Deserialize(string typeName) {
3739
});
3840
}
3941

40-
private readonly ConcurrentDictionary<Type, string> _mappedMessageTypesCache = new ConcurrentDictionary<Type, string>();
4142
public string Serialize(Type type) {
42-
return _mappedMessageTypesCache.GetOrAdd(type, newType => {
43-
var reversedMap = _typeNameOverrides.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
44-
if (reversedMap.ContainsKey(newType))
45-
return reversedMap[newType];
43+
return _typeNameCache.GetOrAdd(type, newType => {
44+
if (_typeNameOverrides != null) {
45+
var reversedMap = _typeNameOverrides.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
46+
if (reversedMap.ContainsKey(newType))
47+
return reversedMap[newType];
48+
}
4649

4750
return String.Concat(type.FullName, ", ", type.Assembly.GetName().Name);
4851
});

0 commit comments

Comments
 (0)