Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 4feb51c

Browse files
committed
Code refactor
1 parent 2972e6a commit 4feb51c

File tree

2 files changed

+57
-46
lines changed

2 files changed

+57
-46
lines changed

src/Microsoft.AspNet.Mvc.Core/SessionStateTempDataProvider.cs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class SessionStateTempDataProvider : ITempDataProvider
3535

3636
private static readonly ConcurrentDictionary<Type, Func<JArray, object>> _arrayConverters =
3737
new ConcurrentDictionary<Type, Func<JArray, object>>();
38-
private static readonly ConcurrentDictionary<Type, Func<JObject, object>> _dictConverters =
38+
private static readonly ConcurrentDictionary<Type, Func<JObject, object>> _dictionaryConverters =
3939
new ConcurrentDictionary<Type, Func<JObject, object>>();
4040

4141
private static readonly Dictionary<JTokenType, Type> _tokenTypeLookup = new Dictionary<JTokenType, Type>
@@ -77,8 +77,9 @@ public virtual IDictionary<string, object> LoadTempData([NotNull] HttpContext co
7777
var jArrayValue = item.Value as JArray;
7878
if (jArrayValue != null && jArrayValue.Count > 0)
7979
{
80+
var arrayType = jArrayValue[0].Type;
8081
Type returnType;
81-
if (_tokenTypeLookup.TryGetValue(jArrayValue[0].Type, out returnType))
82+
if (_tokenTypeLookup.TryGetValue(arrayType, out returnType))
8283
{
8384
var arrayConverter = _arrayConverters.GetOrAdd(returnType, type =>
8485
{
@@ -90,7 +91,7 @@ public virtual IDictionary<string, object> LoadTempData([NotNull] HttpContext co
9091
}
9192
else
9293
{
93-
var message = Resources.FormatTempData_CannotDeserializeToken(jArrayValue[0].Type);
94+
var message = Resources.FormatTempData_CannotDeserializeToken(arrayType);
9495
throw new InvalidOperationException(message);
9596
}
9697
}
@@ -106,7 +107,7 @@ public virtual IDictionary<string, object> LoadTempData([NotNull] HttpContext co
106107
Type valueType;
107108
if (_tokenTypeLookup.TryGetValue(jTokenType, out valueType))
108109
{
109-
var dictConverter = _dictConverters.GetOrAdd(valueType, type =>
110+
var dictConverter = _dictionaryConverters.GetOrAdd(valueType, type =>
110111
{
111112
return (Func<JObject, object>)_convertDictMethodInfo.MakeGenericMethod(type).CreateDelegate(typeof(Func<JObject, object>));
112113
});
@@ -174,7 +175,7 @@ private static bool IsSessionEnabled(HttpContext context)
174175
internal void EnsureObjectCanBeSerialized(object item)
175176
{
176177
var itemType = item.GetType();
177-
Type[] actualTypes = null;
178+
Type actualType = null;
178179

179180
if (itemType.IsArray)
180181
{
@@ -185,29 +186,35 @@ internal void EnsureObjectCanBeSerialized(object item)
185186
if (itemType.ExtractGenericInterface(typeof(IList<>)) != null ||
186187
itemType.ExtractGenericInterface(typeof(IDictionary<,>)) != null)
187188
{
188-
actualTypes = itemType.GetGenericArguments();
189+
var genericTypeArguments = itemType.GetGenericArguments();
190+
if (genericTypeArguments.Length > 1)
191+
{
192+
// Throw if the key type of the dictionary is not string.
193+
if (genericTypeArguments[0] != typeof(string))
194+
{
195+
var message = Resources.FormatTempData_CannotSerializeDictionary(genericTypeArguments[0],
196+
typeof(SessionStateTempDataProvider).FullName);
197+
throw new InvalidOperationException(message);
198+
}
199+
else
200+
{
201+
actualType = genericTypeArguments[1];
202+
}
203+
}
204+
else
205+
{
206+
actualType = genericTypeArguments[0];
207+
}
189208
}
190209
}
191210

192-
actualTypes = actualTypes ?? new Type[] { itemType };
193-
194-
// Throw if the key type of the dictionary is not string.
195-
if (actualTypes.Length > 1 && actualTypes[0] != typeof(string))
196-
{
197-
var message = Resources.FormatTempData_CannotSerializeDictionary(actualTypes[0],
198-
typeof(SessionStateTempDataProvider).FullName);
199-
throw new InvalidOperationException(message);
200-
}
201-
202-
foreach (var actualType in actualTypes)
211+
actualType = actualType ?? itemType;
212+
if (!TypeHelper.IsSimpleType(actualType))
203213
{
204214
var underlyingType = Nullable.GetUnderlyingType(actualType) ?? actualType;
205-
if (!TypeHelper.IsSimpleType(actualType))
206-
{
207-
var message = Resources.FormatTempData_CannotSerializeToSession(underlyingType,
208-
typeof(SessionStateTempDataProvider).FullName);
209-
throw new InvalidOperationException(message);
210-
}
215+
var message = Resources.FormatTempData_CannotSerializeToSession(underlyingType,
216+
typeof(SessionStateTempDataProvider).FullName);
217+
throw new InvalidOperationException(message);
211218
}
212219
}
213220

@@ -216,12 +223,12 @@ private static IList<TVal> ConvertArray<TVal>(JArray array)
216223
return array.Values<TVal>().ToArray();
217224
}
218225

219-
private static IDictionary<string, T> ConvertDict<T>(JObject jObject)
226+
private static IDictionary<string, TVal> ConvertDict<TVal>(JObject jObject)
220227
{
221-
var convertedDict = new Dictionary<string, T>();
228+
var convertedDict = new Dictionary<string, TVal>(StringComparer.Ordinal);
222229
foreach (var item in jObject)
223230
{
224-
convertedDict.Add(item.Key, jObject.Value<T>(item.Key));
231+
convertedDict.Add(item.Key, jObject.Value<TVal>(item.Key));
225232
}
226233
return convertedDict;
227234
}

test/Microsoft.AspNet.Mvc.Core.Test/SessionStateTempDataProviderTest.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ public void EnsureObjectCanBeSerialized_ValidType_DoesNotThrow(object value)
170170
}
171171

172172
[Fact]
173-
public void SaveAndLoad_WorksAsExpected()
173+
public void SaveAndLoad_SimpleTypesCanBeStoredAndLoaded()
174174
{
175175
// Arrange
176176
var testProvider = new SessionStateTempDataProvider();
177177
var inputGuid = Guid.NewGuid();
178-
var inputDict = new Dictionary<string, string>
178+
var inputDictionary = new Dictionary<string, string>
179179
{
180180
{ "Hello", "World" },
181181
};
@@ -187,27 +187,31 @@ public void SaveAndLoad_WorksAsExpected()
187187
{ "DateTime", new DateTime() },
188188
{ "Guid", inputGuid },
189189
{ "List`string", new List<string> { "one", "two" } },
190-
{ "Dictionary", inputDict },
190+
{ "Dictionary", inputDictionary },
191191
};
192192
var context = GetHttpContext(new TestSessionCollection(), true);
193193

194194
// Act
195-
//System.Diagnostics.Debugger.Launch();
196195
testProvider.SaveTempData(context, input);
197196
var TempData = testProvider.LoadTempData(context);
198197

199198
// Assert
200-
Assert.Equal("value", TempData["string"]);
201-
Assert.Equal(10, Convert.ToInt32(TempData["int"]));
202-
Assert.Equal(false, (bool)TempData["bool"]);
203-
Assert.Equal(new DateTime().ToString(), ((DateTime)TempData["DateTime"]).ToString());
204-
Assert.Equal(inputGuid.ToString(), ((Guid)TempData["Guid"]).ToString());
199+
var stringVal = Assert.IsType<string>(TempData["string"]);
200+
Assert.Equal("value", stringVal);
201+
var intVal = Convert.ToInt32(TempData["int"]);
202+
Assert.Equal(10, intVal);
203+
var boolVal = Assert.IsType<bool>(TempData["bool"]);
204+
Assert.Equal(false, boolVal);
205+
var datetimeVal = Assert.IsType<DateTime>(TempData["DateTime"]);
206+
Assert.Equal(new DateTime().ToString(), datetimeVal.ToString());
207+
var guidVal = Assert.IsType<Guid>(TempData["Guid"]);
208+
Assert.Equal(inputGuid.ToString(), guidVal.ToString());
205209
var list = (IList<string>)TempData["List`string"];
206210
Assert.Equal(2, list.Count);
207211
Assert.Equal("one", list[0]);
208212
Assert.Equal("two", list[1]);
209-
var dict = (IDictionary<string, string>)TempData["Dictionary"];
210-
Assert.Equal("World", dict["Hello"]);
213+
var dictionary = Assert.IsType<Dictionary<string, string>>(TempData["Dictionary"]);
214+
Assert.Equal("World", dictionary["Hello"]);
211215
}
212216

213217
private class TestItem
@@ -239,49 +243,49 @@ private HttpContext GetHttpContext(ISessionCollection session, bool sessionEnabl
239243

240244
private class TestSessionCollection : ISessionCollection
241245
{
242-
private Dictionary<string, byte[]> _innerDict = new Dictionary<string, byte[]>();
246+
private Dictionary<string, byte[]> _innerDictionary = new Dictionary<string, byte[]>();
243247

244248
public byte[] this[string key]
245249
{
246250
get
247251
{
248-
return _innerDict[key];
252+
return _innerDictionary[key];
249253
}
250254

251255
set
252256
{
253-
_innerDict[key] = value;
257+
_innerDictionary[key] = value;
254258
}
255259
}
256260

257261
public void Clear()
258262
{
259-
_innerDict.Clear();
263+
_innerDictionary.Clear();
260264
}
261265

262266
public IEnumerator<KeyValuePair<string, byte[]>> GetEnumerator()
263267
{
264-
return _innerDict.GetEnumerator();
268+
return _innerDictionary.GetEnumerator();
265269
}
266270

267271
public void Remove(string key)
268272
{
269-
_innerDict.Remove(key);
273+
_innerDictionary.Remove(key);
270274
}
271275

272276
public void Set(string key, ArraySegment<byte> value)
273277
{
274-
_innerDict[key] = value.AsArray();
278+
_innerDictionary[key] = value.AsArray();
275279
}
276280

277281
public bool TryGetValue(string key, out byte[] value)
278282
{
279-
return _innerDict.TryGetValue(key, out value);
283+
return _innerDictionary.TryGetValue(key, out value);
280284
}
281285

282286
IEnumerator IEnumerable.GetEnumerator()
283287
{
284-
return _innerDict.GetEnumerator();
288+
return _innerDictionary.GetEnumerator();
285289
}
286290
}
287291
}

0 commit comments

Comments
 (0)