diff --git a/src/ExpressiveAnnotations.MvcUnobtrusive.Tests/ValidatorsTest.cs b/src/ExpressiveAnnotations.MvcUnobtrusive.Tests/ValidatorsTest.cs index bb150bd..84634cc 100644 --- a/src/ExpressiveAnnotations.MvcUnobtrusive.Tests/ValidatorsTest.cs +++ b/src/ExpressiveAnnotations.MvcUnobtrusive.Tests/ValidatorsTest.cs @@ -27,6 +27,8 @@ public void Setup() new HttpRequest(string.Empty, "http://tempuri.org", string.Empty), new HttpResponse(new StringWriter()) ); + + MapCache.Instance.Clear(); } [TestMethod] diff --git a/src/ExpressiveAnnotations.MvcUnobtrusive/ExpressiveAnnotations.MvcUnobtrusive.csproj b/src/ExpressiveAnnotations.MvcUnobtrusive/ExpressiveAnnotations.MvcUnobtrusive.csproj index 0f9717b..62ab2a8 100644 --- a/src/ExpressiveAnnotations.MvcUnobtrusive/ExpressiveAnnotations.MvcUnobtrusive.csproj +++ b/src/ExpressiveAnnotations.MvcUnobtrusive/ExpressiveAnnotations.MvcUnobtrusive.csproj @@ -66,6 +66,7 @@ + diff --git a/src/ExpressiveAnnotations.MvcUnobtrusive/MapCache.cs b/src/ExpressiveAnnotations.MvcUnobtrusive/MapCache.cs new file mode 100644 index 0000000..b169bd8 --- /dev/null +++ b/src/ExpressiveAnnotations.MvcUnobtrusive/MapCache.cs @@ -0,0 +1,44 @@ +/* https://github.com/JaroslawWaliszko/ExpressiveAnnotations + * Copyright (c) 2014 Jaroslaw Waliszko + * Licensed MIT: http://opensource.org/licenses/MIT */ + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; + +namespace ExpressiveAnnotations.MvcUnobtrusive +{ + internal class MapCache + { + private static readonly MapCache _instance = new MapCache(); + private static readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); + + private MapCache() + { + } + + public static MapCache Instance + { + get { return _instance; } + } + + public CacheItem GetOrAdd(string key, Func func) + { + return _cache.GetOrAdd(key, func); + } + + public void Clear() + { + _cache.Clear(); + } + } + + internal class CacheItem + { + public IDictionary FieldsMap { get; set; } + public IDictionary ConstsMap { get; set; } + public IDictionary ParsersMap { get; set; } + public IDictionary ErrFieldsMap { get; set; } + public string FormattedErrorMessage { get; set; } + } +} diff --git a/src/ExpressiveAnnotations.MvcUnobtrusive/Validators/ExpressiveValidator.cs b/src/ExpressiveAnnotations.MvcUnobtrusive/Validators/ExpressiveValidator.cs index 51a6618..125d547 100644 --- a/src/ExpressiveAnnotations.MvcUnobtrusive/Validators/ExpressiveValidator.cs +++ b/src/ExpressiveAnnotations.MvcUnobtrusive/Validators/ExpressiveValidator.cs @@ -3,7 +3,6 @@ * Licensed MIT: http://opensource.org/licenses/MIT */ using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics; @@ -20,8 +19,6 @@ namespace ExpressiveAnnotations.MvcUnobtrusive.Validators /// Any type derived from class. public abstract class ExpressiveValidator : DataAnnotationsModelValidator where T : ExpressiveAttribute { - private static readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); - /// /// Constructor for expressive model validator. /// @@ -38,7 +35,7 @@ protected ExpressiveValidator(ModelMetadata metadata, ControllerContext context, var attribId = string.Format("{0}.{1}", attribute.TypeId, annotatedField).ToLowerInvariant(); FieldAttributeType = string.Format("{0}.{1}", typeof(T).FullName, annotatedField).ToLowerInvariant(); - var item = _cache.GetOrAdd(attribId, _ => + var item = MapCache.Instance.GetOrAdd(attribId, _ => { var parser = new Parser(); parser.RegisterMethods(); @@ -192,14 +189,5 @@ private void AssertAttribsQuantityAllowed(int count) throw new InvalidOperationException( string.Format("No more than {0} unique attributes of the same type can be applied for a single field or property.", max)); } - - private class CacheItem - { - public IDictionary FieldsMap { get; set; } - public IDictionary ConstsMap { get; set; } - public IDictionary ParsersMap { get; set; } - public IDictionary ErrFieldsMap { get; set; } - public string FormattedErrorMessage { get; set; } - } } }