Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed May 8, 2019
1 parent 551b974 commit f7b08ce
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/Foundatio/Messaging/ITypeNameSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ public interface ITypeNameSerializer {
}

public class DefaultTypeNameSerializer : ITypeNameSerializer {
private readonly Dictionary<string, Type> _typeNameOverrides;
private readonly ILogger _logger;
private readonly Dictionary<string, Type> _typeNameOverrides;
private readonly ConcurrentDictionary<Type, string> _typeNameCache = new ConcurrentDictionary<Type, string>();
private readonly ConcurrentDictionary<string, Type> _typeCache = new ConcurrentDictionary<string, Type>();

public DefaultTypeNameSerializer(ILogger logger = null, IDictionary<string, Type> typeNameOverrides = null) {
_logger = logger ?? NullLogger.Instance;
_typeNameOverrides = typeNameOverrides != null ? new Dictionary<string, Type>(typeNameOverrides) : new Dictionary<string, Type>();
if (typeNameOverrides != null)
_typeNameOverrides = new Dictionary<string, Type>(typeNameOverrides);
}

private readonly ConcurrentDictionary<string, Type> _knownMessageTypesCache = new ConcurrentDictionary<string, Type>();
public Type Deserialize(string typeName) {
return _knownMessageTypesCache.GetOrAdd(typeName, newTypeName => {
return _typeCache.GetOrAdd(typeName, newTypeName => {
if (_typeNameOverrides != null && _typeNameOverrides.ContainsKey(newTypeName))
return _typeNameOverrides[newTypeName];

Expand All @@ -37,12 +39,13 @@ public Type Deserialize(string typeName) {
});
}

private readonly ConcurrentDictionary<Type, string> _mappedMessageTypesCache = new ConcurrentDictionary<Type, string>();
public string Serialize(Type type) {
return _mappedMessageTypesCache.GetOrAdd(type, newType => {
var reversedMap = _typeNameOverrides.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
if (reversedMap.ContainsKey(newType))
return reversedMap[newType];
return _typeNameCache.GetOrAdd(type, newType => {
if (_typeNameOverrides != null) {
var reversedMap = _typeNameOverrides.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
if (reversedMap.ContainsKey(newType))
return reversedMap[newType];
}

return String.Concat(type.FullName, ", ", type.Assembly.GetName().Name);
});
Expand Down

0 comments on commit f7b08ce

Please sign in to comment.