Skip to content

Commit e0c77dd

Browse files
committed
Refactoring: application instance exposed in the action filter attribute
1 parent e262ece commit e0c77dd

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

src/JQDT.MVC/JQDataTableAttribute.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ public override void OnActionExecuted(ActionExecutedContext filterContext)
3838
private void PerformOnActionExecuted(ActionExecutedContext filterContext)
3939
{
4040
var dataCollectionType = filterContext.Controller.ViewData.Model.GetType();
41-
var applicationExecuteFunction = ExecuteFunctionProvider<ActionExecutedContext>.GetExecuteFunction(dataCollectionType, typeof(ApplicationMvc<>));
4241
var dependencyResolver = new DI.DependencyResolver();
43-
var result = (ResultModel)applicationExecuteFunction(filterContext, dependencyResolver);
42+
var applicationInitizlizationFunction = ExecuteFunctionProvider<ActionExecutedContext>.GetAppInicializationFunc(dataCollectionType, typeof(ApplicationMvc<>));
43+
var mvcApplication = applicationInitizlizationFunction(filterContext, dependencyResolver);
44+
var result = (ResultModel)mvcApplication.Execute();
4445

4546
filterContext.Result = this.FormatResult(new
4647
{

src/JQDT.WebAPI/JQDataTableAttribute.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public override void OnActionExecuted(HttpActionExecutedContext actionExecutedCo
3535
private void PerformOnActionExecuted(HttpActionExecutedContext actionExecutedContext)
3636
{
3737
var modelType = ((System.Net.Http.ObjectContent)actionExecutedContext.Response.Content).ObjectType;
38-
var applicationExecuteFunction = ExecuteFunctionProvider<HttpActionExecutedContext>.GetExecuteFunction(modelType, typeof(ApplicationWebApi<>));
38+
var applicationInitizlizationFunction = ExecuteFunctionProvider<HttpActionExecutedContext>.GetAppInicializationFunc(modelType, typeof(ApplicationWebApi<>));
3939
var dependencyResolver = new DI.DependencyResolver();
40-
var result = (ResultModel)applicationExecuteFunction(actionExecutedContext, dependencyResolver);
40+
var webApiApplication = applicationInitizlizationFunction(actionExecutedContext, dependencyResolver);
41+
var result = (ResultModel)webApiApplication.Execute();
4142
var formattedObjectResult = this.GetObjectResult(result);
4243
actionExecutedContext.Response.Content = new ObjectContent(typeof(object), formattedObjectResult, new JsonMediaTypeFormatter());
4344
}

src/JQDT/Application/ApplicationBase.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/// The <see cref="ApplicationBase.Execute(System.Collections.Specialized.NameValueCollection, System.Linq.IQueryable{T})"/> should be called
1515
/// </summary>
1616
/// <typeparam name="T">Data Collection Generic Type</typeparam>
17-
public abstract class ApplicationBase<T>
17+
public abstract class ApplicationBase<T> : IApplicationBase
1818
{
1919
private readonly IDependencyResolver dependencyResolver;
2020

@@ -28,9 +28,11 @@ public ApplicationBase(IDependencyResolver dependencyResolver)
2828
}
2929

3030
/// <summary>
31-
/// Application entry point method. Should be called from the ActionFilter.
31+
/// Application entry point method. Executes all data processors.
3232
/// </summary>
33-
/// <returns><see cref="ResultModel"/></returns>
33+
/// <returns>
34+
/// Processed data as <see cref="ResultModel" />
35+
/// </returns>
3436
public ResultModel Execute()
3537
{
3638
ResultModel result = new ResultModel();

src/JQDT/Application/ExecuteFunctionProvider.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public static class ExecuteFunctionProvider<TContext>
1313
{
1414
private static ConcurrentDictionary<Type, Func<TContext, DI.IDependencyResolver, object>> executionFunctionsCache = new ConcurrentDictionary<Type, Func<TContext, DI.IDependencyResolver, object>>();
15+
private static ConcurrentDictionary<Type, Func<TContext, DI.IDependencyResolver, IApplicationBase>> appInitFunctionsCache = new ConcurrentDictionary<Type, Func<TContext, DI.IDependencyResolver, IApplicationBase>>();
1516

1617
/// <summary>
1718
/// Gets the execute function.
@@ -42,5 +43,33 @@ public static class ExecuteFunctionProvider<TContext>
4243

4344
return executeFunc;
4445
}
46+
47+
/// <summary>
48+
/// Gets the application initialization function.
49+
/// </summary>
50+
/// <param name="dataCollectionType">Type of the data collection.</param>
51+
/// <param name="appType">Type of the application.</param>
52+
/// <returns><see cref="Func{T, TResult}"/> that return new <see cref="IApplicationBase"/> instance</returns>
53+
public static Func<TContext, DI.IDependencyResolver, IApplicationBase> GetAppInicializationFunc(Type dataCollectionType, Type appType)
54+
{
55+
Func<TContext, DI.IDependencyResolver, IApplicationBase> executeFunc = null;
56+
57+
if (!appInitFunctionsCache.TryGetValue(dataCollectionType, out executeFunc))
58+
{
59+
Type[] typeArgs = { dataCollectionType.GenericTypeArguments.First() };
60+
var genericAppType = appType.MakeGenericType(typeArgs);
61+
62+
var contextExpr = Expression.Parameter(typeof(TContext), "context");
63+
var dependencyResolverExpr = Expression.Parameter(typeof(DI.IDependencyResolver));
64+
var appConstructorInfo = genericAppType.GetConstructors().First();
65+
var newAppExpr = Expression.New(appConstructorInfo, contextExpr, dependencyResolverExpr);
66+
var lambda = Expression.Lambda(newAppExpr, contextExpr, dependencyResolverExpr);
67+
68+
executeFunc = (Func<TContext, DI.IDependencyResolver, IApplicationBase>)lambda.Compile();
69+
executionFunctionsCache.TryAdd(dataCollectionType, executeFunc);
70+
}
71+
72+
return executeFunc;
73+
}
4574
}
4675
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace JQDT.Application
2+
{
3+
using JQDT.Models;
4+
5+
/// <summary>
6+
/// Base interface for <see cref="ApplicationBase{T}"/>
7+
/// </summary>
8+
public interface IApplicationBase
9+
{
10+
/// <summary>
11+
/// Application entry point method. Executes all data processors.
12+
/// </summary>
13+
/// <returns>Processed data as <see cref="ResultModel"/></returns>
14+
ResultModel Execute();
15+
}
16+
}

src/JQDT/JQDT.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<ItemGroup>
6060
<Compile Include="Application\ApplicationBase.cs" />
6161
<Compile Include="Application\ExecuteFunctionProvider.cs" />
62+
<Compile Include="Application\IApplicationBase.cs" />
6263
<Compile Include="DataProcessing\ColumnsFilterDataProcessing\ColumnsFilterDataProcessor.cs" />
6364
<Compile Include="DataProcessing\Common\ConstantExpressionBuilder.cs" />
6465
<Compile Include="DataProcessing\Common\ContainsExpressionBuilder.cs" />

0 commit comments

Comments
 (0)