Reputation: 11340
Assuming I have an application that allows customers to add/update products into my database over Web API. I have lightweight DTOs like this:
public class ProductDTO
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
What is the industry practice to track entities, assuming I would want to store the changes into an Audit table. E.g., display old price vs new price
Upon receiving the DTO in my WebAPI controller, do I have to query from the database the current record, do an object comparison, and save the differences? - Is this the only way?
Upvotes: 3
Views: 825
Reputation: 14402
Web API has built-in logging and tracing and here is a good walkthrough.
Here is a TraceWriter implementation using log4net or if NLog is more your cup of tea then there is an NLog trace logger, and here is another example using NLog.
If you want to compare changes, then you'd need to set something up manually. You can easily get access to the JSON requests. It would simply be a matter or running a diff between the old and the new objects. An example of which is on StackOverflow. I would log the request headers, request body, endpoint address, an API key, the old object (request) and the diffs. You could then either output the comparisons as a list of differences, or have a side-by-side comparison (as you see in Git / SVN / TFS merge).
Upvotes: 1