I have made a HTMLHelper extension method to be used from the Razor view. When this method gets called, some logic is required to get information from some other class/method. This class uses dependency injection.
Now I'm not sure how can I call that class from the extension method.
Please see below for the code-
public static class VehicleTrackingFieldAccessHtmlHelper
{
public static IHtmlString VehicleTrackingFieldAccessFor<TModel, TValue>(
this HtmlHelper<TModel> html,
Expression<Func<TModel, TValue>> expression,
int VT_ID,
long OU_ID)
{
var x = ModelMetadata.FromLambdaExpression<TModel, TValue>(expression, html.ViewData);
if (x.AdditionalValues.ContainsKey(CreateNewVehicleTrackingInput.FieldAccessKey))
{
var vtField = (Enums.Database.VT_Fields)x.AdditionalValues[CreateNewVehicleTrackingInput.FieldAccessKey];
// Error/Problem is at this line
// This constructor requires an argument (_vehicleTrackingFieldAccessAppService) to be passed.
// But we don't have access to it here.
return new VehicleTrackingFieldAccessHtmlHandler().ProcessFields(vtField, VT_ID, OU_ID).Result;
}
return new HtmlString("");
}
}
public class VehicleTrackingFieldAccessHtmlHandler
{
private readonly IVehicleTrackingFieldAccessAppService _vehicleTrackingFieldAccessAppService;
public VehicleTrackingFieldAccessHtmlHandler(IVehicleTrackingFieldAccessAppService vehicleTrackingFieldAccessAppService)
{
_vehicleTrackingFieldAccessAppService = vehicleTrackingFieldAccessAppService;
}
private async Task<IHtmlString> ProcessFields(Enums.Database.VT_Fields vtField, int VT_ID, long OU_ID)
{
var fieldAccess = await GetUserPermissionsForVtFields(VT_ID, OU_ID);
switch (vtField)
{
case Enums.Database.VT_Fields.Customer_First_Name:
return new HtmlString("");
default:
return new HtmlString("");
}
}
}
Thanks
3 Answer(s)
-
0
Resolve the class:
using (var vehicleTrackingFieldAccessHtmlHandler = IocManager.Instance.ResolveAsDisposable<VehicleTrackingFieldAccessHtmlHandler>()) { return vehicleTrackingFieldAccessHtmlHandler.Object.ProcessFields(vtField, VT_ID, OU_ID).Result; }
-
0
Thanks for the reply! Can you please explain what does this do?
-
0
This uses IocManager.Instance, which is an IIocResolver, to resolve VehicleTrackingFieldAccessHtmlHandler like DI. ResolveAsDisposable returns IDisposableDependencyObjectWrapper, so ".Object" accesses your resolved "Handler". The disposable wrapper is assigned in a "using" statement to ensure that "Handler" is released to avoid memory leak.
Read more in ABP documentation:
- Dependency Injection: <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Dependency-Injection#DocIocHelper">https://aspnetboilerplate.com/Pages/Doc ... cIocHelper</a>