Base solution for your next web application
Open Closed

Custom EnableQueryAttribute and Dependency Injection #9340


User avatar
0
tom.ohle created

I have an AbpODataEntityController and I want to add a custom FilterBcEnableQueryAttribute on the Get class. However, I cannot figure out how to implement dependency injection in the FilterBcEnableQueryAttribute class. How could I get an instantiated version of my IPermissionPolicyManager class, for example?

` public class WipsSearchController : AbpODataEntityController<WipSearch, long>, ITransientDependency {

    public WipsSearchController(IRepository<WipSearch, long> repository) : base(repository) 
    {
    }
    
    
    [DontWrapResult]
    //compile error here since no empty constructor; if I add empty constructor, the dependencies are not initialized
    [FilterBcEnableQuery]
    [AbpMvcAuthorize(AppPermissions.BusinessRule_Wips_View)] 
    public IQueryable<WipSearch> Get()
    {
        return base.Get();
    }
}

public class FilterBcEnableQueryAttribute : EnableQueryAttribute, ITransientDependency
{
    private readonly IPermissionPolicyManager _permissionPolicyManager;
    private readonly IWipBcPolicyHandler _wipBcPolicyHandler;


    public FilterBcEnableQueryAttribute(IPermissionPolicyManager permissionPolicyManager,
        IWipBcPolicyHandler wipBcPolicyHandler)
    {           
        _permissionPolicyManager = permissionPolicyManager;
        _wipBcPolicyHandler = wipBcPolicyHandler;
    }

    public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
    {
        base.OnActionExecuted(actionExecutedContext);
    }

    public override void OnActionExecuting(ActionExecutingContext context)
    {
        var args = context.HttpContext.ODataFeature();
        var bcOrgIds = new List<long>();
        var userPolicies = _permissionPolicyManager.GetUserPermissionPolicies(AppPermissions.BusinessRule_Wips_View);
        var bcUserPolicies = userPolicies.Where(p => p.PolicyName == AppPolicies.Wips_Bcs).ToList();
        
        var request = context.HttpContext.Request;
        var url = context.HttpContext.Request.GetEncodedUrl();
        if (url.IndexOf("$filter") == -1)
        {
            context.Result = new RedirectResult("http://localhost:22742/odata/wipssearch?$filter=BcNumber%20eq%20'1609'");
        }
        
        base.OnActionExecuting(context);
    }

} }`


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @tom.ohle

    You can use IocManager.Instance to resolve dependencies of FilterBcEnableQueryAttribute instead of using constructor injection.