ArxOne / MrAdvice

.NET aspect weaver (build task under NuGet package)
MIT License
308 stars 45 forks source link

Parameter for Parameter Advice #168

Closed Sago92 closed 3 years ago

Sago92 commented 3 years ago

Hello,

we want to integrate Mr. Advise into our company Framework. Our goal is it to reduce the parameter validation with a simple advice before a paramter.

Example before Mr. Advice: public static int GetNumberOfPages(string sourceFilePath) { // Validation if (String.IsNullOrEmpty(sourceFilePath)) throw new ArgumentNullException(nameof(sourceFilePath)); if (!File.Exists(sourceFilePath)) throw new FileNotFoundException(nameof(sourceFilePath)); ...

Exmaple after Mr. Advid: public static int GetNumberOfPages([PathNull][FileExist]string sourceFilePath) { ...

In some of our validations we, for example, check an integer for a range (like 1-100 or 1 - 65535). To realize that we have to create an advice for each range.

Is there a way to pass values into the adivce?

For exmaple like that?

public static int CreatePort([Range(1, 65535)]int port) { ...

picrap commented 3 years ago

Hi, did you try? Advices are attribute, so adding parameters to a constructor or in public properties should work.

Sago92 commented 3 years ago

Coulnd't see the wood for the trees ... I am sorry for opening this issue. Adding a constructor to the Advice works perfectly fine to pass parameters.