Skip to content

Commit af0c8a4

Browse files
committed
smartstore#323 Web API: Resolve dependencies through constructor injection
1 parent addfbf7 commit af0c8a4

4 files changed

Lines changed: 33 additions & 23 deletions

File tree

src/Plugins/Api.WebApi/Controllers/Api/PaymentsController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
using SmartStore.Core.Infrastructure;
2-
using SmartStore.Core.Plugins;
1+
using SmartStore.Core.Plugins;
32
using SmartStore.Services.Payments;
43
using SmartStore.Web.Framework.WebApi;
54
using SmartStore.Web.Framework.WebApi.OData;
65
using SmartStore.Web.Framework.WebApi.Security;
76
using System.Linq;
87
using System.Web.Http;
8+
using System;
99

1010
namespace SmartStore.Plugin.Api.WebApi.Controllers.Api
1111
{
1212
[WebApiAuthenticate(Permission = "ManagePaymentMethods")]
1313
public class PaymentsController : ApiController
1414
{
15-
private readonly IPluginFinder _pluginFinder;
15+
private readonly Lazy<IPluginFinder> _pluginFinder;
1616

17-
public PaymentsController()
17+
public PaymentsController(Lazy<IPluginFinder> pluginFinder)
1818
{
19-
_pluginFinder = EngineContext.Current.Resolve<IPluginFinder>();
19+
_pluginFinder = pluginFinder;
2020
}
2121

2222
[WebApiQueryable(PagingOptional = true)]
@@ -25,7 +25,7 @@ public IQueryable<PluginDescriptor> GetMethods()
2525
if (!ModelState.IsValid)
2626
throw this.ExceptionInvalidModelState();
2727

28-
var query = _pluginFinder
28+
var query = _pluginFinder.Value
2929
.GetPlugins<IPaymentMethod>(false)
3030
.Select(x => x.PluginDescriptor)
3131
.AsQueryable();

src/Plugins/Api.WebApi/Controllers/OData/OrdersController.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using SmartStore.Core.Domain.Orders;
44
using SmartStore.Core.Domain.Payments;
55
using SmartStore.Core.Domain.Shipping;
6-
using SmartStore.Core.Infrastructure;
76
using SmartStore.Services.Orders;
87
using SmartStore.Web.Framework.WebApi;
98
using SmartStore.Web.Framework.WebApi.OData;
@@ -12,12 +11,20 @@
1211
using System.Linq;
1312
using System.Web.Http;
1413
using System.Web.Http.OData;
14+
using System;
1515

1616
namespace SmartStore.Plugin.Api.WebApi.Controllers.OData
1717
{
1818
[WebApiAuthenticate(Permission = "ManageOrders")]
1919
public class OrdersController : WebApiEntityController<Order, IOrderService>
2020
{
21+
private readonly Lazy<IOrderProcessingService> _orderProcessingService;
22+
23+
public OrdersController(Lazy<IOrderProcessingService> orderProcessingService)
24+
{
25+
_orderProcessingService = orderProcessingService;
26+
}
27+
2128
protected override IQueryable<Order> GetEntitySet()
2229
{
2330
var query =
@@ -116,7 +123,7 @@ public Order PaymentPaid(int key, ODataActionParameters parameters)
116123
Service.UpdateOrder(entity);
117124
}
118125

119-
EngineContext.Current.Resolve<IOrderProcessingService>().MarkOrderAsPaid(entity);
126+
_orderProcessingService.Value.MarkOrderAsPaid(entity);
120127
return null;
121128
});
122129
return entity;
@@ -130,18 +137,17 @@ public Order PaymentRefund(int key, ODataActionParameters parameters)
130137
this.ProcessEntity(() =>
131138
{
132139
bool online = parameters.GetValue<string, bool>("Online");
133-
var orderProcessingService = EngineContext.Current.Resolve<IOrderProcessingService>();
134-
140+
135141
if (online)
136142
{
137-
var errors = orderProcessingService.Refund(entity);
143+
var errors = _orderProcessingService.Value.Refund(entity);
138144

139145
if (errors.Count > 0)
140146
return errors[0];
141147
}
142148
else
143149
{
144-
orderProcessingService.RefundOffline(entity);
150+
_orderProcessingService.Value.RefundOffline(entity);
145151
}
146152
return null;
147153
});
@@ -155,7 +161,7 @@ public Order Cancel(int key)
155161

156162
this.ProcessEntity(() =>
157163
{
158-
EngineContext.Current.Resolve<IOrderProcessingService>().CancelOrder(entity, true);
164+
_orderProcessingService.Value.CancelOrder(entity, true);
159165

160166
return null;
161167
});

src/Plugins/Api.WebApi/Controllers/OData/ProductsController.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,24 @@
99
using SmartStore.Core.Domain.Discounts;
1010
using SmartStore.Core.Domain.Directory;
1111
using SmartStore.Core.Domain.Media;
12-
using SmartStore.Core.Infrastructure;
1312
using SmartStore.Core;
13+
using System;
1414

1515
namespace SmartStore.Plugin.Api.WebApi.Controllers.OData
1616
{
1717
[WebApiAuthenticate(Permission = "ManageCatalog")]
1818
public class ProductsController : WebApiEntityController<Product, IProductService>
1919
{
20+
private readonly Lazy<IWorkContext> _workContext;
21+
private readonly Lazy<IPriceCalculationService> _priceCalculationService;
22+
23+
public ProductsController(Lazy<IWorkContext> workContext,
24+
Lazy<IPriceCalculationService> priceCalculationService)
25+
{
26+
_workContext = workContext;
27+
_priceCalculationService = priceCalculationService;
28+
}
29+
2030
protected override IQueryable<Product> GetEntitySet()
2131
{
2232
var query =
@@ -147,9 +157,6 @@ public IQueryable<ProductBundleItem> GetProductBundleItems(int key)
147157

148158
this.ProcessEntity(() =>
149159
{
150-
var engine = EngineContext.Current;
151-
var priceCalculationService = engine.Resolve<IPriceCalculationService>();
152-
153160
if (lowestPrice)
154161
{
155162
if (entity.ProductType == ProductType.GroupedProduct)
@@ -164,19 +171,17 @@ public IQueryable<ProductBundleItem> GetProductBundleItems(int key)
164171
Product lowestPriceProduct;
165172
var associatedProducts = Service.PrepareProductSearchQuery(searchContext);
166173

167-
result = priceCalculationService.GetLowestPrice(entity, associatedProducts, out lowestPriceProduct);
174+
result = _priceCalculationService.Value.GetLowestPrice(entity, associatedProducts, out lowestPriceProduct);
168175
}
169176
else
170177
{
171178
bool displayFromMessage;
172-
result = priceCalculationService.GetLowestPrice(entity, out displayFromMessage);
179+
result = _priceCalculationService.Value.GetLowestPrice(entity, out displayFromMessage);
173180
}
174181
}
175182
else
176183
{
177-
var customer = engine.Resolve<IWorkContext>().CurrentCustomer;
178-
179-
result = priceCalculationService.GetFinalPrice(entity, null, customer, decimal.Zero, true, 1);
184+
result = _priceCalculationService.Value.GetFinalPrice(entity, null, _workContext.Value.CurrentCustomer, decimal.Zero, true, 1);
180185
}
181186
return null;
182187
});

src/Plugins/Api.WebApi/Controllers/WebApiController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using SmartStore.Services.Localization;
77
using SmartStore.Services.Security;
88
using SmartStore.Web.Framework.Controllers;
9-
using SmartStore.Web.Framework.UI;
109
using SmartStore.Web.Framework.WebApi;
1110
using System;
1211
using System.Web.Mvc;

0 commit comments

Comments
 (0)