Extending your Models by Adding Validation and Custom Logic on Saves
A well written framework is like a stream where the water flows smoothly downhill. Code is not repeated and much of the business and validation logic is provided for in the service layer. Let’s see how that can easily be accomplished in Rock.
Rock allows your models to respond to saves either before they take place in PreSaveChanges()
or after in PostSaveChanges()
. Let’s take a look at each and see some of the options of each.
PreSaveChanges
PreSaveChanges is a great place to place logic to prep data from saving. It also allows you to handle cascading deletes when the model doesn’t handle them intrinsically. Below is an example of an override to the PreSaveChanges for a model. This code should be applied to your model class.
PostSaveChanges
PostSaveChanges get’s called after the save. This is a great to to do things like save details to history, create transactions to process other steps or makes API calls to update remote systems. It’s a lot like its PreSave brother, but at this point you don’t have access to the pre/post state of the values, just the current state of the model after the save.
Last updated