Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a good architecture for rules of validations

Tags:

c#

validation

I hope you guys can give me some good suggestions. I am thinking how to make a good architecture for C# development. I am trying my best to explain scenarios because im not good at english:

1) Two classes: Blue Bank and Red Bank

2) Third class : Rules Of Validations

3) Blue and Red Banks have several fields (values) such as AccountNumber, Amount, InvoicePeriod etc... Example here (xml):

Blue Bank

  <AccountNumber>Maria 5987593457</AccountNumber>
  <Amount>200.00</Amount>
  <InvoicePeriod>1</InvoicePeriod>

Red Bank

  <AccountNumber>8529458</AccountNumber>
  <Amount>300.00</Amount>
  <InvoicePeriod>0</InvoicePeriod>

Red/Blue Banks have some same rules of validations like Amount fields that must be numberic. But Red/Blue Banks have different rules of validations -- AccountNumber field must be alphanumberic in Blue Bank whereas AccountNumber must be numberic in Red Bank otherwise failed. InvoicePeriod field must be default 1 in Red Bank whereas it must be default 0 in Blue Bank otherwise failed.

My idea is:

I want to create each class of Red / Blue Bank for different rules of validation and then i also create a rules of validation class for the same rules that Blue/Red banks have.

My code here:

Blue Bank class:

  • Validate AccountNumber that must be alphanumberic otherwise failed
  • Validate InvoicePeriod that must be default 1 otherwise failed

Red Bank class:

  • Validate AccountNumber that must be numberic otherwise failed
  • Validate InvoicePeriod that must be default 0 otherwise failed

RulesOfValidation class

  • Validate Amount that must be numberic (same rules for both Red / Blue Bank classes)

How does it work with dictonary <,> with these classes? Or any better suggestion with your example codes?

Your help would be highly appreciated.

like image 595
user1358072 Avatar asked Feb 16 '23 11:02

user1358072


1 Answers

Specification Pattern is a good option for your problem. You can create a specification class for common bank rules and others specification classes for each specific need.

There is some c# libraries for this pattern:

  • https://github.com/giacomelli/KissSpecifications
  • https://github.com/danielcrenna/specifications
like image 88
giacomelli Avatar answered Feb 18 '23 23:02

giacomelli