Access Models in Static Class methods RRS feed

  • Question

  • Could any one help me to solve this issue.

    I have 3 projects in .NET core, and rather than having the codes mentioned in the ValidationMethods wanted to read them from appSettings.

    1 > Startup, where the ConfigManager model class is being loaded with the appropriate data from the appSettings.Env.Json file. 

    2 > Containing all the Models in one project containing the custom attributes and using reflection calling the ValidationMethods in Static class 
    [SpaceCount(Category = "F01", Offset = 337, Length = 1, Type = "N", ValidateMethod = "ValidatePayeeCode", ErrorCode = "032")]
    public string PayeeCode { get; set; }

    3 > have the Validators in static class and methods 

      public static ValidationMethodResponse ValidateIdentificationNumber(string inputValue, string type, string errorCode)
                ValidationMethodResponse retValue = new ValidationMethodResponse();

                if (inputValue == "12345" || inputValue == "67890")
                    retValue.Result = inputValue;
                    retValue.ErrorResult.ErrorCode = errorCode;

                return retValue;

    Friday, May 17, 2019 10:20 AM

All replies

  • I really think you're overthinking this problem. What exactly are you gaining by using reflection to dynamically validate stuff here?

    The only thing I could remotely see as being useful to configure would be the input values themselves. You can easily do that by having the validation method rely on a service/repo to get the list of valid inputs. You don't need reflection for that.

    Have you tried implementing this logic first as a regular validation attribute/method with hard coded values. That is where you should start. Then take a step back and extract the inputs to a separate class used by validation. At that point I suspect you'll have everything you need, it will already be tested, you won't need reflection and it'll be a simple extension of the existing, simple code you started with.

    Also, please use the Insert Code Block to put code into your posts. This keeps your code separate from your questions.

    Michael Taylor

    Friday, May 17, 2019 2:06 PM
  • What kind of Core solution is this Web, console or what, becuase none of what you are doing makes any sense?
    Friday, May 17, 2019 4:10 PM