Skip to main content

Posts

Showing posts from November, 2017

Auditing system with Nhibernate events

This post details the process to create a domain auditing system using NHibernate events. Upon any change to a mapped entity, details of the change are posted to an audit table in the database. Setup Create the AuditEventListener , which inherits from IPostInsertEventListener , IPostUpdateEventListener and IPostDeleteEventListener;  this will enable the logging of inserts, updates and deletes respectively A custom time provider is used here, to append the current date and time to the logging, though using the native DateTime.Now is normally sufficient Add the  OnPostUpdate   method and pass in a  PostUpdateEvent Using the entity  property on the PostUpdateEvent , check to ensure the entity is not an AuditLogEntry , as it makes no sense to audit the audit entity Verify that the old state of the entity is not null. If the old state is null, it is clear that this is not an update, and so make the method throw an error if this is the case U...

Using the Mediatr pipeline with Fluent Validation

This post details the basic setup steps required to implement server side validation using pipeline behaviour native to Mediatr with the Fluent Validation package. Configuration  Install the FluentValidation NuGet package ( Project URL ) into both the Mediatr project and the project where the IOC is initialised e.g the Engine project Create a ValidationBehavior file, in the Mediatr project  Register collections for IValidator and IPipelineBehaviour  in the IOC initialisation. Note that IValidator uses FluentValidation and IPipelineBehaviour  uses Mediatr Validator Setup Add a validator into the Mediatr   project. The FluentMigration  package enables the validator to implement AbstractValidator , as well as calling RuleFor() . In the example below, the validator is ensuring that there are no dogs added without a name. If a user attempts to do so, the error message "Dog has no name" will return -  the request will not be comple...