-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathFormatValue.cs
76 lines (65 loc) · 2.62 KB
/
FormatValue.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
76
// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
using System;
#if EF5
using System.Data;
using System.Data.Entity;
using System.Data.Objects;
#elif EF6
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
#elif EFCORE
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
#endif
namespace Z.EntityFramework.Plus
{
public partial class AuditConfiguration
{
/// <summary>Format a value for the specified entry and property name.</summary>
/// <param name="entry">The entry.</param>
/// <param name="propertyName">The property name.</param>
/// <returns>The formatted value.</returns>
#if EF5 || EF6
public string FormatValue(object entity, string propertyName, object currentValue)
#elif EFCORE
public string FormatValue(object entity, string propertyName, object currentValue)
#endif
{
if (EntityValueFormatters.Count > 0)
{
#if EF5 || EF6
var type = ObjectContext.GetObjectType(entity.GetType());
#elif EFCORE
var type = entity.GetType();
#endif
var key = string.Concat(type.FullName, ";", propertyName);
Func<object, object> formatter;
if (!ValueFormatterDictionary.TryGetValue(key, out formatter))
{
if (EntityValueFormatters.Count > 0)
{
foreach (var formatProperty in EntityValueFormatters)
{
formatter = formatProperty(entity, propertyName, currentValue);
if (formatter != null)
{
break;
}
}
}
ValueFormatterDictionary.TryAdd(key, formatter);
}
if (formatter != null)
{
currentValue = formatter(currentValue);
}
}
return currentValue != null && currentValue != DBNull.Value ? currentValue.ToString() : null;
}
}
}