Skip to content

Commit 9b05ba1

Browse files
committed
Perf: fixed some DI issues with background tasks
1 parent 569a03d commit 9b05ba1

3 files changed

Lines changed: 86 additions & 40 deletions

File tree

src/Libraries/SmartStore.Core/WebHelper.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Web.Hosting;
1111
using SmartStore.Core.Data;
1212
using SmartStore.Core.Domain;
13+
using SmartStore.Core.Domain.Stores;
1314
using SmartStore.Core.Infrastructure;
1415
using SmartStore.Utilities;
1516

@@ -27,6 +28,8 @@ public partial class WebHelper : IWebHelper
2728
private readonly HttpContextBase _httpContext;
2829
private bool? _isCurrentConnectionSecured;
2930

31+
private Store _currentStore;
32+
3033
/// <summary>
3134
/// Ctor
3235
/// </summary>
@@ -203,20 +206,27 @@ private string GetStoreHost(bool useSsl, out bool appPathPossiblyAppended)
203206
{
204207
//let's resolve IWorkContext here.
205208
//Do not inject it via contructor because it'll cause circular references
206-
IStoreContext storeContext;
207-
if (EngineContext.Current.ContainerManager.TryResolve<IStoreContext>(null, out storeContext)) // Unit test safe!
209+
210+
if (_currentStore == null)
208211
{
209-
var currentStore = storeContext.CurrentStore;
210-
if (currentStore == null)
211-
throw new Exception("Current store cannot be loaded");
212+
IStoreContext storeContext;
213+
if (EngineContext.Current.ContainerManager.TryResolve<IStoreContext>(null, out storeContext)) // Unit test safe!
214+
{
215+
_currentStore = storeContext.CurrentStore;
216+
if (_currentStore == null)
217+
throw new Exception("Current store cannot be loaded");
218+
}
219+
}
212220

213-
var securityMode = currentStore.GetSecurityMode(useSsl);
221+
if (_currentStore != null)
222+
{
223+
var securityMode = _currentStore.GetSecurityMode(useSsl);
214224

215225
if (httpHost.IsEmpty())
216226
{
217227
//HTTP_HOST variable is not available.
218228
//It's possible only when HttpContext is not available (for example, running in a schedule task)
219-
result = currentStore.Url.EnsureEndsWith("/");
229+
result = _currentStore.Url.EnsureEndsWith("/");
220230

221231
appPathPossiblyAppended = true;
222232
}
@@ -228,7 +238,7 @@ private string GetStoreHost(bool useSsl, out bool appPathPossiblyAppended)
228238
// Secure URL for shared ssl specified.
229239
// So a store owner doesn't want it to be resolved automatically.
230240
// In this case let's use the specified secure URL
231-
result = currentStore.SecureUrl.EmptyNull();
241+
result = _currentStore.SecureUrl.EmptyNull();
232242

233243
if (!result.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
234244
{
@@ -252,7 +262,7 @@ private string GetStoreHost(bool useSsl, out bool appPathPossiblyAppended)
252262
// So a store owner doesn't want it to be resolved automatically.
253263
// In this case let's use the specified non-secure URL
254264

255-
result = currentStore.Url;
265+
result = _currentStore.Url;
256266
appPathPossiblyAppended = true;
257267
}
258268
}

src/Libraries/SmartStore.Services/Seo/SeoExtensions.cs

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -152,41 +152,64 @@ public static string GetSeName<T>(this T entity)
152152
/// <param name="returnDefaultValue">A value indicating whether to return default value (if language specified one is not found)</param>
153153
/// <param name="ensureTwoPublishedLanguages">A value indicating whether to ensure that we have at least two published languages; otherwise, load only default value</param>
154154
/// <returns>Search engine name</returns>
155-
public static string GetSeName<T>(this T entity, int languageId, bool returnDefaultValue = true,
156-
bool ensureTwoPublishedLanguages = true)
155+
public static string GetSeName<T>(this T entity, int languageId, bool returnDefaultValue = true, bool ensureTwoPublishedLanguages = true)
157156
where T : BaseEntity, ISlugSupported
158157
{
159-
if (entity == null)
160-
throw new ArgumentNullException("entity");
158+
return GetSeName(
159+
entity,
160+
languageId,
161+
EngineContext.Current.Resolve<IUrlRecordService>(),
162+
EngineContext.Current.Resolve<ILanguageService>(),
163+
returnDefaultValue,
164+
ensureTwoPublishedLanguages);
165+
}
161166

162-
string result = string.Empty;
163-
string entityName = typeof(T).Name;
167+
/// <summary>
168+
/// Get search engine name
169+
/// </summary>
170+
/// <typeparam name="T">Entity type</typeparam>
171+
/// <param name="entity">Entity</param>
172+
/// <param name="languageId">Language identifier</param>
173+
/// <param name="returnDefaultValue">A value indicating whether to return default value (if language specified one is not found)</param>
174+
/// <param name="ensureTwoPublishedLanguages">A value indicating whether to ensure that we have at least two published languages; otherwise, load only default value</param>
175+
/// <returns>Search engine name</returns>
176+
public static string GetSeName<T>(this T entity,
177+
int languageId,
178+
IUrlRecordService urlRecordService,
179+
ILanguageService languageService,
180+
bool returnDefaultValue = true,
181+
bool ensureTwoPublishedLanguages = true)
182+
where T : BaseEntity, ISlugSupported
183+
{
184+
if (entity == null)
185+
throw new ArgumentNullException("entity");
164186

165-
var urlRecordService = EngineContext.Current.Resolve<IUrlRecordService>();
166-
if (languageId > 0)
167-
{
168-
//ensure that we have at least two published languages
169-
bool loadLocalizedValue = true;
170-
if (ensureTwoPublishedLanguages)
171-
{
172-
var lService = EngineContext.Current.Resolve<ILanguageService>();
173-
var totalPublishedLanguages = lService.GetLanguagesCount(false);
174-
loadLocalizedValue = totalPublishedLanguages >= 2;
175-
}
176-
//localized value
177-
if (loadLocalizedValue)
178-
{
179-
result = urlRecordService.GetActiveSlug(entity.Id, entityName, languageId);
180-
}
181-
}
182-
//set default value if required
183-
if (String.IsNullOrEmpty(result) && returnDefaultValue)
184-
{
185-
result = urlRecordService.GetActiveSlug(entity.Id, entityName, 0);
186-
}
187+
string result = string.Empty;
188+
string entityName = typeof(T).Name;
187189

188-
return result;
189-
}
190+
if (languageId > 0)
191+
{
192+
//ensure that we have at least two published languages
193+
bool loadLocalizedValue = true;
194+
if (ensureTwoPublishedLanguages)
195+
{
196+
var totalPublishedLanguages = languageService.GetLanguagesCount(false);
197+
loadLocalizedValue = totalPublishedLanguages >= 2;
198+
}
199+
//localized value
200+
if (loadLocalizedValue)
201+
{
202+
result = urlRecordService.GetActiveSlug(entity.Id, entityName, languageId);
203+
}
204+
}
205+
//set default value if required
206+
if (String.IsNullOrEmpty(result) && returnDefaultValue)
207+
{
208+
result = urlRecordService.GetActiveSlug(entity.Id, entityName, 0);
209+
}
210+
211+
return result;
212+
}
190213

191214
/// <summary>
192215
/// Validate search engine name

src/Presentation/SmartStore.Web.Framework/Plugins/FeedPluginHelper.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Web.Mvc;
2020
using System.Threading;
2121
using System.Threading.Tasks;
22+
using SmartStore.Services.Localization;
2223

2324
namespace SmartStore.Web.Framework.Plugins
2425
{
@@ -75,6 +76,18 @@ private ILogger Logger
7576
get { return _logger ?? (_logger = _ctx.Resolve<ILogger>()); }
7677
}
7778

79+
private IUrlRecordService _urlRecordService;
80+
private IUrlRecordService UrlRecordService
81+
{
82+
get { return _urlRecordService ?? (_urlRecordService = _ctx.Resolve<IUrlRecordService>()); }
83+
}
84+
85+
private ILanguageService _languageService;
86+
private ILanguageService LanguageService
87+
{
88+
get { return _languageService ?? (_languageService = _ctx.Resolve<ILanguageService>()); }
89+
}
90+
7891
#endregion
7992

8093
private PromotionFeedSettings BaseSettings
@@ -257,7 +270,7 @@ public string GetBasePrice(Product product)
257270

258271
public string GetProductDetailUrl(Store store, Product product)
259272
{
260-
return "{0}{1}".FormatWith(store.Url, product.GetSeName(Language.Id));
273+
return "{0}{1}".FormatWith(store.Url, product.GetSeName(Language.Id, UrlRecordService, LanguageService));
261274
}
262275

263276
public FeedFileData GetFeedFileByStore(Store store, string secondFileName = null, string extension = null)

0 commit comments

Comments
 (0)