Repository navigation
Expand file tree
/
Copy pathSmartMetadataProvider.cs
More file actions
51 lines (45 loc) · 1.84 KB
/
Copy pathSmartMetadataProvider.cs
File metadata and controls
51 lines (45 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Web.Mvc;
namespace SmartStore.Web.Framework.Modelling
{
/// <summary>
/// This MetadataProvider adds some functionality on top of the default CachedDataAnnotationsModelMetadataProvider.
/// It adds custom attributes (implementing IModelAttribute) to the AdditionalValues property of the model's metadata
/// so that it can be retrieved later.
/// </summary>
public class SmartMetadataProvider : CachedDataAnnotationsModelMetadataProvider
{
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
[SuppressMessage("ReSharper", "SuspiciousTypeConversion.Global")]
protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(IEnumerable<Attribute> attributes, Type containerType, Type modelType, string propertyName)
{
var metadata = base.CreateMetadataPrototype(attributes, containerType, modelType, propertyName);
var attrs = attributes.OfType<IModelAttribute>().ToList();
foreach (var attr in attrs)
{
if (metadata.AdditionalValues.ContainsKey(attr.Name))
{
throw new SmartException("There is already an attribute with the name of '{0}' on this model.".FormatCurrent(attr.Name));
}
metadata.AdditionalValues.Add(attr.Name, attr);
}
return metadata;
}
protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
{
var result = base.CreateMetadataFromPrototype(prototype, modelAccessor);
if (prototype.AdditionalValues.Count > 0)
{
var attrs = prototype.AdditionalValues.Where(x => x.Value is IModelAttribute).ToArray();
if (attrs.Any())
{
result.AdditionalValues.AddRange(attrs);
}
}
return result;
}
}
}