Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable implicit conversion of decimal to money as static configuration option #94

Open
jkone27 opened this issue Feb 2, 2023 · 1 comment

Comments

@jkone27
Copy link

jkone27 commented Feb 2, 2023

When using Money in constructor a typical dangerous thing is the conversion of decimal to Money as implicit, especially in a microservices world, you never know what currency is going to be based on the locale, so developers can make mistake passing a decimal where a money with currency is expected instead. I suggest providing configurability to exclude the op implicit on the constructor for money, or making it throw an exception when configured correctly (e.g. statically) .

https://stackoverflow.com/questions/46190907/overriding-implicit-operators-in-c-sharp

maybe something like?

NodaMoney.ImplicitDecimal = false; 

or some other way of configuring this, else this requires for now this workaround solution

class MyClass {
     public MyClass(Money money) { ... }
     
    // this works or using a subclass of money where implicit is redefined also works, but it's not the easiest solution
     public MyClass(decimal money){ 
           throw new ArgumentException('do not allow implicit for currency issues with locale'); 
           }

}
@ondrejpialek
Copy link

We've run into this problem too and would 100% prefer this to be configurable. To be honest I would prefer this to not be there at all, so that it would not even compile (or these being in a separate NuGet package, which is probably not possible and / or practical). Alternatively this could be solved by a Roslyn analyzer I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants