pamidur / aspect-injector

AOP framework for .NET (c#, vb, etc)
Apache License 2.0
742 stars 111 forks source link

Multiple Same Validator Aspect On MethodFeature Can Not Wroking #201

Closed huaSoftware closed 1 year ago

huaSoftware commented 1 year ago

Describe the bug

namespace Nebula.Next.SPC.WebAPI.Aspects
{

    [Aspect(Scope.Global)]
    public class ValidatorAspect
    {

        [Advice(Kind.Before)] // you can have also After (async-aware), and Around(Wrap/Instead) kinds
        public void  Before( [Argument(Source.Target)] Func<object[], object> target,
            [Argument(Source.Arguments)] object[] args,
            [Argument(Source.Instance)] object instance,
            [Argument(Source.ReturnType)] Type retType,
            [Argument(Source.Triggers)] Attribute[] triggers)
        {
            var requestServices = GetMeSomeServiceLocator.Instance.GetService<IHttpContextAccessor>();
            HttpRequest request = requestServices.HttpContext.Request;
            var trigger = triggers.OfType<Validator>().FirstOrDefault();
            string name = trigger.Name;
            bool require = trigger.Require;
            int minLen = trigger.MinLen;
            int maxLen = trigger.MaxLen;

            string language = request.Headers.AcceptLanguage;
            string paramsVal = request.Query[name];
            if (require){
                string requireStr;
                if(language.Equals("zh-CN")) {
                    requireStr = "是必填项目";
                }else {
                    requireStr = " is required";
                }
                if(string.IsNullOrEmpty(paramsVal)) {

                    throw new Exception(name+requireStr);
                }
            }

            if(minLen != 0) {
                string minStr;
                if(language.Equals("zh-CN")) {
                    minStr = "小于";
                }else {
                    minStr = " is less than ";
                }
                if(Convert.ToInt32(paramsVal) < minLen) {
                    throw new Exception(name+minStr+minLen.ToString());
                }
            }
             if(maxLen != 0) {
                string maxStr;
                if(language.Equals("zh-CN")) {
                    maxStr = "小于";
                }else {
                    maxStr = " is greater than ";
                }
                if(Convert.ToInt32(paramsVal) > maxLen) {
                    throw new Exception(name+maxStr+minLen.ToString());
                }
            }
        }
    }
}

namespace Nebula.Next.SPC.WebAPI.Aspects
{

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
    [Injection(typeof(ValidatorAspect), Inherited = true,  Priority = 20 )]
    public class Validator: Attribute
    {
        public string Name { get; set; }
        public bool Require { get; set; }
        public int MinLen  { get; set; }
        public int MaxLen  { get; set; }

        public Validator(string name, bool require, int minLen=0, int maxLen=0)
        {
            Name = name;
            Require = require;
            MinLen = minLen;
            MaxLen = maxLen;
        }

    }
}

[HttpGet]
[Validator("id", true)]
[Validator("page", true, 1, 20)]
public IActionResult GetReportById(string id)
{
    var result = _reportServive.GetReportById(id);
    return StatusCodeWrapper.BuildSuccess(new ApiResult(result));
}

To Reproduce I want to step: -->Validator("id", true)-->Validator("page", true, 1, 20)-->Controller Fact: -->Validator("id", true)->Controller Missing my Validator("page", true, 1, 20).

Additional context I cannot find useful information from doc, can you help me fixed my problems?

huaSoftware commented 1 year ago

I find 'triggers' has params, i can use them to reslove this problem.

pamidur commented 1 year ago

I'm glad you managed to resolve the issue. Indeed Triggers param gives you a list of all attributes currently triggering the aspect

Best, Oleksandr Hulyi


From: Blue Tortoise @.> Sent: Tuesday, November 8, 2022 8:56:15 AM To: pamidur/aspect-injector @.> Cc: Subscribed @.***> Subject: Re: [pamidur/aspect-injector] Multiple Same Validator Aspect On MethodFeature Can Not Wroking (Issue #201)

Closed #201https://github.com/pamidur/aspect-injector/issues/201 as completed.

— Reply to this email directly, view it on GitHubhttps://github.com/pamidur/aspect-injector/issues/201#event-7759825821, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AA7HZUGX63OT37TJITJLV4TWHIBR7ANCNFSM6AAAAAAR2ACZP4. You are receiving this because you are subscribed to this thread.Message ID: @.***>