Skip to content

Commit 3010add

Browse files
committed
Pass IQueryable data by reference to event handler
1 parent 0a69e99 commit 3010add

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

src/Examples/Examples.Mvc/Controllers/CustomersController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public ActionResult GetCustomersData()
4646

4747
public class CustomJQDataTableAttribute : JQDataTableAttribute
4848
{
49-
public override void OnDataProcessed(object data, RequestInfoModel requestInfoModel)
49+
public override void OnDataProcessed(ref object data, RequestInfoModel requestInfoModel)
5050
{
51-
var processedData = ((IQueryable<CustomerViewModel>)data).Where(x => x.CustomerID > 10 && x.CustomerID < 20);
52-
var list = processedData.ToList();
53-
data = list;
51+
var list = ((IQueryable<CustomerViewModel>)data).ToList();
52+
list.ForEach(x => x.Person.FirstName = $"FN {x.Person.FirstName}");
53+
data = list.AsQueryable();
5454
}
5555
}
5656
}

src/JQDT.MVC/JQDataTableAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public override void OnActionExecuted(ActionExecutedContext filterContext)
4040
/// </summary>
4141
/// <param name="data">The data.</param>
4242
/// <param name="requestInfoModel">The request information model.</param>
43-
public virtual void OnDataProcessed(object data, RequestInfoModel requestInfoModel)
43+
public virtual void OnDataProcessed(ref object data, RequestInfoModel requestInfoModel)
4444
{
4545
}
4646

src/JQDT/Application/ApplicationBase.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public ResultModel Execute()
5252
var dataProcessChain = this.GetDataProcessChain(requestModel.Helpers.DataCollectionType);
5353
var processedData = dataProcessChain.ProcessData(data, requestModel);
5454

55-
this.OnDataProcessed(processedData, requestModel);
55+
// Call on data processed event
56+
this.PerformDataProcessorEventHandler(ref processedData, requestModel);
5657

5758
result.Data = processedData.ToList().Select(x => (object)x).ToList();
5859
result.Draw = requestModel.TableParameters.Draw;
@@ -79,6 +80,18 @@ public ResultModel Execute()
7980
/// <returns>Data collection as <see cref="IQueryable{T}"/></returns>
8081
protected abstract IQueryable<T> GetData();
8182

83+
/// <summary>
84+
/// Performs the data processor event handler. Boxes the data collection, pass it by reference and then unbox it.
85+
/// </summary>
86+
/// <param name="data">The data.</param>
87+
/// <param name="requestInfoModel">The request information model.</param>
88+
private void PerformDataProcessorEventHandler(ref IQueryable<T> data, RequestInfoModel requestInfoModel)
89+
{
90+
var dataAsObj = (object)data;
91+
this.OnDataProcessed(ref dataAsObj, requestInfoModel);
92+
data = (IQueryable<T>)dataAsObj;
93+
}
94+
8295
private string FormatException(Exception ex)
8396
{
8497
var builder = new StringBuilder();

src/JQDT/Delegates/DataProcessorEventHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
/// </summary>
88
/// <param name="data">The data.</param>
99
/// <param name="requestInfoModel">The request information model.</param>
10-
public delegate void DataProcessorEventHandler(object data, RequestInfoModel requestInfoModel);
10+
public delegate void DataProcessorEventHandler(ref object data, RequestInfoModel requestInfoModel);
1111
}

0 commit comments

Comments
 (0)