This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WidgetResolver.cs
67 lines (53 loc) · 2.55 KB
/
WidgetResolver.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
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace LeeConlin.Kentico12.MVC.WidgetResolver
{
public class WidgetResolver : IWidgetResolver
{
private IWidgetRegistry WidgetRegistry { get; }
public WidgetResolver(IWidgetRegistry widgetRegistry)
{
WidgetRegistry = widgetRegistry;
}
/// <inheritdoc />
public TWidgetModel Resolve<TWidgetModel>(JObject parsedWidgetData) where TWidgetModel : IWidgetModel
{
var obj = parsedWidgetData
.ToObject<TWidgetModel>(JsonSerializer.Create(new JsonSerializerSettings { ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor }));
var codename = parsedWidgetData.Property("name").Value.ToString();
var action = WidgetRegistry.GetAfterAction(codename);
action.DynamicInvoke(obj);
return obj;
}
/// <inheritdoc />
public IWidgetModel Resolve(JObject parsedWidgetData, Type type)
{
var codename = parsedWidgetData.Property("name").Value.ToString();
var beforeAction = WidgetRegistry.GetBeforeAction(codename);
if(beforeAction != null) parsedWidgetData = (JObject)beforeAction.DynamicInvoke(parsedWidgetData);
var obj = parsedWidgetData?.ToObject(type, JsonSerializer.Create(new JsonSerializerSettings { ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor }));
var afterAction = WidgetRegistry.GetAfterAction(codename);
if (afterAction != null) obj = afterAction.DynamicInvoke(obj);
return (IWidgetModel) obj;
}
/// <inheritdoc />
public TWidgetModel Resolve<TWidgetModel>(JObject parsedWidgetData, Func<JObject, TWidgetModel> outFunc) where TWidgetModel : IWidgetModel
{
var obj = outFunc.Invoke(parsedWidgetData);
var codename = parsedWidgetData.Property("name").Value.ToString();
var action = WidgetRegistry.GetAfterAction(codename);
action.DynamicInvoke(obj);
return obj;
}
/// <inheritdoc />
public IWidgetModel Resolve(JObject parsedWidgetData, Func<JObject, IWidgetModel> outFunc)
{
var obj = outFunc.Invoke(parsedWidgetData);
var codename = parsedWidgetData.Property("name").Value.ToString();
var action = WidgetRegistry.GetAfterAction(codename);
action.DynamicInvoke(obj);
return (IWidgetModel)obj;
}
}
}