forked from smartstore/SmartStoreNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIMergedData.cs
More file actions
47 lines (41 loc) · 1.21 KB
/
Copy pathIMergedData.cs
File metadata and controls
47 lines (41 loc) · 1.21 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
using System.Collections.Generic;
using System.Web.Hosting;
using SmartStore.Core.Data;
using SmartStore.Core.Infrastructure;
namespace SmartStore.Core
{
public interface IMergedData
{
bool MergedDataIgnore { get; set; }
Dictionary<string, object> MergedDataValues { get; }
}
public static class IMergedDataExtensions
{
public static T GetMergedDataValue<T>(this IMergedData mergedData, string key, T defaultValue)
{
if (mergedData.MergedDataValues == null)
return defaultValue;
if (mergedData.MergedDataIgnore)
return defaultValue;
if (mergedData is BaseEntity && HostingEnvironment.IsHosted)
{
// This is absolutely bad coding! But I don't see any alternatives.
// When the passed object is a (EF)-trackable entity,
// we cannot return the merged value while EF performs
// change detection, because entity properties could be set to modified,
// where in fact nothing has changed.
var dbContext = EngineContext.Current.Resolve<IDbContext>();
if (dbContext.IsDetectingChanges())
{
return defaultValue;
}
}
object value;
if (mergedData.MergedDataValues.TryGetValue(key, out value))
{
return (T)value;
}
return defaultValue;
}
}
}