NHibernate, audit and calculated column values

How can I set some special column values ​​when updating / inserting objects through NHibernate without extending domain classes with special properties?

eg. each table contains audit columns such as CreatedBy, CreatedDate, UpdateBy, UpdDate. But I do not want to add these classes to domain classes. I want the modedl Percious Ignorance domain to be as high as possible.

+4
source share
5 answers

You might want to look at IUserType NHibernate.

At the bottom of the next page is an example in which ayende removes some encryption logic from an object and lets NHibernate just take care of it.

http://ayende.com/Blog/archive/2008/07/31/Entities-dependencies-best-practices.aspx

+1
source

After hours of hacking at NHibernate, I found a compromised solution on how to keep domain level classes isolated from the infrastructure level. Only one “victim” here is point number 1 in the list below:

1) I introduced the DomainObject base class for all persistent objects in a domain with only one private field:

private IDictionary _infrastructureProperties = new Dictionary<object, object>(); 

2) Added the following section to class mapping:

 <dynamic-component name='_infrastructureProperties' access='field'> <property name='CreateBy' column='CreatedBy' /> <property name='CreateDate' column='CreatedDate' /> </dynamic-component> 

3) An interceptor has been introduced that sets these property values.

4) Optional. We could also implement good settings with the setting that the "role" of each class plays in the application, and then to work with certain properties of the role in Interceptor. For instance. this configuration may indicate that Product is a TenantScopeObject, and the interceptor will set the TenantID property to the current tenant ID registered in the system.

+1
source
+1
source

This is not the same as “not adding these properties,” but the last time I saw this, the engineer turned to him, introducing specific NHibernate classes and deriving them from a common abstract base class (for example, MyAuditable) that implemented the properties which you don't like the most. Thus, you need to solve the problem only once.

0
source

Display timestamp data using NHibernate ICompositeUserType and Create Interceptor Timestamp in NHibernate

I found these articles helpful. Obviously, this is not PI because you are tied to NH / SQL.

Most IoC containers now have interceptors, so you can intercept your changes and queue them. If UoW discards your changes, you can also save your audit trail.

0
source

Source: https://habr.com/ru/post/1276898/


All Articles