/
ExAttributeSwitch.cs
75 lines (60 loc) · 3.26 KB
/
ExAttributeSwitch.cs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using LazZiya.ExpressLocalization.DataAnnotations.Adapters;
using Microsoft.AspNetCore.Mvc.DataAnnotations;
using Microsoft.Extensions.Localization;
using System;
using System.ComponentModel.DataAnnotations;
#if NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2
using Microsoft.AspNetCore.Mvc.DataAnnotations.Internal;
#endif
namespace LazZiya.ExpressLocalization.DataAnnotations
{
/// <summary>
/// A switch to return the relevant Ex attribute or null if no Ex attribute is detected
/// </summary>
public class ExAttributeSwitch
{
/// <summary>
/// Get the ex attribute adapter
/// </summary>
/// <param name="attribute"></param>
/// <param name="stringLocalizer"></param>
/// <returns></returns>
public static IAttributeAdapter GetAttributeAdapter<T>(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
where T : class
{
if (attribute == null)
throw new ArgumentNullException(nameof(attribute));
var type = attribute.GetType();
if (type == typeof(ExRequiredAttribute))
return new RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer);
#if NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2
if (type == typeof(ExMaxLengthAttribute))
return new MaxLengthAttributeAdapter((MaxLengthAttribute)attribute, stringLocalizer);
if (type == typeof(ExMinLengthAttribute))
return new MinLengthAttributeAdapter((MinLengthAttribute)attribute, stringLocalizer);
if (type == typeof(ExCompareAttribute))
return new CompareAttributeAdapter((CompareAttribute)attribute, stringLocalizer);
if (type == typeof(ExRangeAttribute))
return new RangeAttributeAdapter((RangeAttribute)attribute, stringLocalizer);
if (type == typeof(ExRegularExpressionAttribute))
return new RegularExpressionAttributeAdapter((RegularExpressionAttribute)attribute, stringLocalizer);
if (type == typeof(ExStringLengthAttribute))
return new StringLengthAttributeAdapter((StringLengthAttribute)attribute, stringLocalizer);
#elif NETCOREAPP3_0 || NETCOREAPP3_1
if (type == typeof(ExMaxLengthAttribute))
return new ExMaxLengthAttributeAdapter((ExMaxLengthAttribute)attribute, stringLocalizer);
if (type == typeof(ExMinLengthAttribute))
return new ExMinLengthAttributeAdapter((ExMinLengthAttribute)attribute, stringLocalizer);
if (type == typeof(ExCompareAttribute))
return new ExCompareAttributeAdapter<T>((ExCompareAttribute)attribute, stringLocalizer);
if (type == typeof(ExRangeAttribute))
return new ExRangeAttributeAdapter<T>((ExRangeAttribute)attribute, stringLocalizer);
if (type == typeof(ExRegularExpressionAttribute))
return new ExRegularExpressionAttributeAdapter<T>((ExRegularExpressionAttribute)attribute, stringLocalizer);
if (type == typeof(ExStringLengthAttribute))
return new ExStringLengthAttributeAdapter((ExStringLengthAttribute)attribute, stringLocalizer);
#endif
return null;
}
}
}