Skip to content

Commit 8c69c08

Browse files
committed
mvc action filter refactored for dependency inversion
1 parent 8e0985e commit 8c69c08

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/JQDT.MVC/JQDataTableAttribute.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Web.Mvc;
55
using JQDT.Application;
6+
using JQDT.DI;
67
using JQDT.Exceptions;
78
using JQDT.Models;
89

@@ -13,6 +14,26 @@
1314
[AttributeUsage(validOn: AttributeTargets.Method, AllowMultiple = false)]
1415
public class JQDataTableAttribute : ActionFilterAttribute
1516
{
17+
private readonly IServiceLocator serviceLocator;
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="JQDataTableAttribute"/> class.
21+
/// </summary>
22+
public JQDataTableAttribute()
23+
: this(new ServiceLocator())
24+
{
25+
}
26+
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="JQDataTableAttribute"/> class.
29+
/// This constructor is used for testing purposes.
30+
/// </summary>
31+
/// <param name="serviceLocator">The service locator.</param>
32+
internal JQDataTableAttribute(IServiceLocator serviceLocator)
33+
{
34+
this.serviceLocator = serviceLocator;
35+
}
36+
1637
/// <summary>
1738
/// Called by the ASP.NET MVC framework after the action method executes.
1839
/// </summary>
@@ -159,7 +180,6 @@ private void PerformOnActionExecuted(ActionExecutedContext filterContext)
159180
{
160181
var dataCollectionType = filterContext.Controller.ViewData.Model.GetType();
161182
var applicationInitizlizationFunction = ExecuteFunctionProvider<ActionExecutedContext>.GetAppInicializationFunc(dataCollectionType, typeof(ApplicationMvc<>));
162-
var serviceLocator = new DI.ServiceLocator();
163183
var formModelBinder = serviceLocator.GetFormModelBinder();
164184
var mvcApplication = applicationInitizlizationFunction(filterContext, serviceLocator, formModelBinder);
165185
this.SubscribeToEvents(mvcApplication);

0 commit comments

Comments
 (0)