-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathUmbracoApiControllerBase.cs
164 lines (142 loc) · 6.12 KB
/
UmbracoApiControllerBase.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Web;
using System.Web.Http;
using Microsoft.Owin;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Mapping;
using Umbraco.Core.Persistence;
using Umbraco.Core.Services;
using Umbraco.Web.Security;
using Umbraco.Web.WebApi.Filters;
namespace Umbraco.Web.WebApi
{
/// <summary>
/// Provides a base class for Umbraco API controllers.
/// </summary>
/// <remarks>These controllers are NOT auto-routed.</remarks>
[FeatureAuthorize]
public abstract class UmbracoApiControllerBase : ApiController
{
// note: all Umbraco controllers have two constructors: one with all dependencies, which should be used,
// and one with auto dependencies, ie no dependencies - and then dependencies are automatically obtained
// here from the Current service locator - this is obviously evil, but it allows us to add new dependencies
// without breaking compatibility.
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoApiControllerBase"/> class with auto dependencies.
/// </summary>
/// <remarks>Dependencies are obtained from the <see cref="Current"/> service locator.</remarks>
protected UmbracoApiControllerBase()
: this(
Current.Factory.GetInstance<IGlobalSettings>(),
Current.Factory.GetInstance<IUmbracoContextAccessor>(),
Current.Factory.GetInstance<ISqlContext>(),
Current.Factory.GetInstance<ServiceContext>(),
Current.Factory.GetInstance<AppCaches>(),
Current.Factory.GetInstance<IProfilingLogger>(),
Current.Factory.GetInstance<IRuntimeState>(),
Current.Factory.GetInstance<UmbracoHelper>(),
Current.Factory.GetInstance<UmbracoMapper>()
)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoApiControllerBase"/> class with all its dependencies.
/// </summary>
protected UmbracoApiControllerBase(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper, UmbracoMapper umbracoMapper)
{
UmbracoContextAccessor = umbracoContextAccessor;
GlobalSettings = globalSettings;
SqlContext = sqlContext;
Services = services;
AppCaches = appCaches;
Logger = logger;
RuntimeState = runtimeState;
Umbraco = umbracoHelper;
Mapper = umbracoMapper;
}
[Obsolete("This constructor is obsolete since it doesn't inject the UmbracoMapper. The UmbracoMapper will be resolved from the service locator Current.Mapper, which is not good for testability. Inject the UmbracoMapper using full constructor injection instead.")]
protected UmbracoApiControllerBase(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper)
{
UmbracoContextAccessor = umbracoContextAccessor;
GlobalSettings = globalSettings;
SqlContext = sqlContext;
Services = services;
AppCaches = appCaches;
Logger = logger;
RuntimeState = runtimeState;
Umbraco = umbracoHelper;
// not good for testability, hence the obsolete.
Mapper = Current.Mapper;
}
/// <summary>
/// Gets a unique instance identifier.
/// </summary>
/// <remarks>For debugging purposes.</remarks>
internal Guid InstanceId { get; } = Guid.NewGuid();
/// <summary>
/// Gets the Umbraco context.
/// </summary>
public virtual IGlobalSettings GlobalSettings { get; }
/// <summary>
/// Gets the Umbraco context.
/// </summary>
public virtual UmbracoContext UmbracoContext => UmbracoContextAccessor.UmbracoContext;
/// <summary>
/// Gets the Umbraco context accessor.
/// </summary>
public virtual IUmbracoContextAccessor UmbracoContextAccessor { get; }
/// <summary>
/// Gets the sql context.
/// </summary>
public ISqlContext SqlContext { get; }
/// <summary>
/// Gets the services context.
/// </summary>
public ServiceContext Services { get; }
/// <summary>
/// Gets the application cache.
/// </summary>
public AppCaches AppCaches { get; }
/// <summary>
/// Gets the logger.
/// </summary>
public IProfilingLogger Logger { get; }
/// <summary>
/// Gets the runtime state.
/// </summary>
internal IRuntimeState RuntimeState { get; }
/// <summary>
/// Gets the application URL.
/// </summary>
protected Uri ApplicationUrl => RuntimeState.ApplicationUrl;
/// <summary>
/// Gets the membership helper.
/// </summary>
public MembershipHelper Members => Umbraco.MembershipHelper;
/// <summary>
/// Gets the Umbraco helper.
/// </summary>
public UmbracoHelper Umbraco { get; }
/// <summary>
/// Gets the mapper.
/// </summary>
public UmbracoMapper Mapper { get; }
/// <summary>
/// Gets the web security helper.
/// </summary>
public WebSecurity Security => UmbracoContext.Security;
/// <summary>
/// Tries to get the current HttpContext.
/// </summary>
protected Attempt<HttpContextBase> TryGetHttpContext()
=> Request.TryGetHttpContext();
/// <summary>
/// Tries to get the current OWIN context.
/// </summary>
protected Attempt<IOwinContext> TryGetOwinContext()
=> Request.TryGetOwinContext();
}
}