Validations Through Factory Pattern
The project that we are working on, has its validations either at the front end or in the database (SPs). For the purpose of ease of maintenance and scalability, I am planning to integrate the validations into a separate middle layer (probably the Business Layer).
The project can be reused to address some of our existing product upgrade needs, and also for similar types of projects we are looking to execute in the future. Therefore the validations and business rules that we have to implement should be configurable or customized. So for client X, the rules would be different, while for client Y the rules would be different. And the basic framework and database would remain pretty much the same.
I am thinking of implementing the following architecture:
- Entity Framework coupled with service classes to form the DAL for the project
- Factory pattern to implement business rules and custom validations for various entities
- Integrating the business rules within the DAL service class methods to ensure the validations are run transparently
Thus:
- The application would call CustomerService.Add() method and pass it the necessary details.
- CustomerService.Add() method would transparently invoke an instance of a CustomerValidator class and call its Validate() method, passing the parameters received (or entity created from them) to it.
- The Validate() method returns true or false based on the logic designed inside it.
- The CustomerValidator class is instantiated through a factory pattern, so there would be a CustomerValidatorFactory class too. A parameter is passed to this CustomerValidatorFactory to indicate which set of rules to load/run. Each client/customization would have a set of rules (which is actually a different implementation of the CustomerValidator.Validate() method.
- Once the basic framework is in place, for any new clients or customizations we simply have to create a new implementation of the CustomerValidator class.
Is this the right way ?
.