bilal-fazlani / tracker-enabled-dbcontext

Tracker-enabled DbContext offers you to implement full auditing in your database
https://tracker-enabled-dbcontext.bilal-fazlani.com/
Other
217 stars 110 forks source link

Getting AuditLogDetail for properties of complex types. #172

Open Salalimo opened 4 years ago

Salalimo commented 4 years ago

Hi,

Thanks for making this library available. I have a couple of comments one of which might be a bug, but I might be missing something. The use case is that I have an entity with a complex type.

I had to do this change in CreateComplexTypeLogDetails in ChangeLogDetailsAuditor to be able to get the value:

                     if (GlobalTrackingConfig.DisconnectedContext)
                        origValue = ((DbPropertyValues)complexTypeOrigValue).GetValue<object>(pi.Name);
                    else
                        origValue = complexTypeOrigValue == null ? null : pi.GetValue(complexTypeOrigValue);

This is due to the different types of objects being returned from

        object OriginalValue(string propertyName)   // in DbEntryValuesWrapper

When the DisconnectedContext flag is not set you are returning an object that is the property itself (an object of complex type) that in turn has the has the properties that we are tracking. When the flag is set you are returning a different type of object and the issue is that it does not contain the properties of the complex type. So trying to retrieve the values using reflection by property name does not work.

BTW I tried using the extension method GetPropertyValue by doing => complexTypeOrigValue.GetPropertyValue, but it would go into an infinite loop. Just out of curiosity if that method can be used after its fixed. It is also not being used anywhere so you may consider removing it.

I also needed to do this change From Comparator comparator = ComparatorFactory.GetComparator(complexTypeObj); To Comparator comparator = ComparatorFactory.GetComparator(pi.GetMethod.ReturnType);

This latter change allows me to record only the properties of the complex type that changed and not all of the properties in the complex object when only one has changed. I understand that someone may consider a change in one of the properties to signify the complex property has changed, so it works for them, but in our usage this is not the case. I just wanted to point it out for your consideration. You may choose to make it an option that some users can configure.

I also made some changes that record the db table names and column names instead of the entity type name and the property names. If you are interested let me know.

Regards.