简体   繁体   中英

How to track disconnected entities over Web API

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. Eg, 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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM