-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathModelBase.cs
More file actions
131 lines (110 loc) · 3.45 KB
/
ModelBase.cs
File metadata and controls
131 lines (110 loc) · 3.45 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Newtonsoft.Json;
using SmartStore.Core.Infrastructure;
namespace SmartStore.Web.Framework.Modelling
{
[Serializable]
public sealed class CustomPropertiesDictionary : Dictionary<string, object>
{
}
[Serializable]
public abstract partial class ModelBase
{
private readonly static ContextState<Dictionary<ModelBase, IDictionary<string, object>>> _contextState;
static ModelBase()
{
_contextState = new ContextState<Dictionary<ModelBase, IDictionary<string, object>>>("ModelBase.CustomThreadProperties");
}
protected ModelBase()
{
CustomProperties = new CustomPropertiesDictionary();
}
public virtual void BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
}
/// <summary>
/// Gets a custom property value either from the thread local or the static storage (in this particular order)
/// </summary>
/// <typeparam name="TProperty">Type of property</typeparam>
/// <param name="key">Custom property key</param>
/// <returns>The property value or null</returns>
public TProperty Get<TProperty>(string key)
{
Guard.NotEmpty(key, nameof(key));
IDictionary<string, object> dict;
object value;
if (TryGetCustomThreadProperties(false, out dict) && dict.TryGetValue(key, out value))
{
return (TProperty)value;
}
if (CustomProperties.TryGetValue(key, out value))
{
return (TProperty)value;
}
return default(TProperty);
}
/// <summary>
/// Use this property to store any custom value for your models.
/// </summary>
public CustomPropertiesDictionary CustomProperties { get; set; }
/// <summary>
/// A data bag for custom model properties which only
/// lives during a thread/request lifecycle
/// </summary>
/// <remarks>
/// Use thread properties whenever you need to persist request-scoped data,
/// but the model is potentially cached statically.
/// </remarks>
[JsonIgnore]
public IDictionary<string, object> CustomThreadProperties
{
get
{
IDictionary<string, object> dict;
TryGetCustomThreadProperties(true, out dict);
return dict;
}
}
private bool TryGetCustomThreadProperties(bool create, out IDictionary<string, object> dict)
{
dict = null;
var state = _contextState.GetState();
if (state == null && create)
{
state = new Dictionary<ModelBase, IDictionary<string, object>>();
_contextState.SetState(state);
}
if (state != null)
{
if (!state.TryGetValue(this, out dict))
{
if (create)
{
dict = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
state[this] = dict;
}
}
return dict != null;
}
return false;
}
}
public abstract partial class EntityModelBase : ModelBase
{
[SmartResourceDisplayName("Admin.Common.Entity.Fields.Id")]
public virtual int Id { get; set; }
/// <remarks>
/// This property is required for serialization JSON data of Telerik grids.
/// Without a lower case Id property in JSON results its AJAX operations do not work correctly.
/// Occurs since RouteCollection.LowercaseUrls was set to true in Global.asax.
/// </remarks>
[JsonProperty("id")]
internal int EntityId => Id;
}
public abstract partial class TabbableModel : EntityModelBase
{
public virtual string[] LoadedTabs { get; set; }
}
}