How to Define your Audits

There are a few different ways to enlist an entity for auditing, with varying degrees of flexibility.

Use your DataContext table properties

This is the best way to create audit definitions, and should handle 95% of use cases. This logic relies on your DataContext having properties for each of your tables. It will use attribute reflection to look up the primary keys of each table.
dataContext.Products.Audit();
dataContext.Categories.Audit();
dataContext.Orders.Audit();

Alternatively, you can explicitly define your primary key fields if you do not want to rely on reflection, or if you have any problems with the implicit method.
dataContext.Products.Audit(p => p.ProductID);
dataContext.Categories.Audit(c => c.CategoryID);
dataContext.Orders.Audit(o => o.OrderID);

Specify the Entity Type manually

Use of this method will be rare, but it will come in handy if your DataContext does not have properties to access your tables.
dataContext.Audit<Product>();
dataContext.Audit<Category>();
dataContext.Audit<Order>();

You can also use this method to define the primary keys.
dataContext.Audit<Product>(p => p.ProductID);
dataContext.Audit<Category>(c => c.CategoryID);
dataContext.Audit<Order>(o => o.OrderID);

How to Audit Associations

You will probably want to audit some "children" relationships eventually. This will come in handy if you want to Audit the Orders table, but you also want to audit all OrderDetails that are related to the Order.

Use the Association Property of your Entity

This is the best way to audit associations, but it assumes that you have an IEnumerable property on your Entity to access the association.
dataContext.Orders.Audit()
       .AuditAssociation(o => o.OrderDetails);


dataContext.Contacts.Audit()
       .AuditAssociation(c => c.Addresses)
       .AuditAssociation(c => c.PhoneNumbers);

Explicity Map the PK/FK Properties

This method will come in handy if you do not have an association property on your entity to rely on
dataContext.Orders.Audit()
       .AuditAssociation<OrderDetail>(od => od.OrderDetailID, od => od.OrderID);


dataContext.Contacts.Audit()
       .AuditAssociation<Address>(a => a.AddressID, a => a.ContactID)
       .AuditAssociation<PhoneNumber>(p => p.PhoneNumberID, p => p.ContactID);

Last edited Jan 12, 2009 at 5:51 PM by mhidinger, version 4

Comments

Florens Sep 3, 2009 at 1:17 PM 
what would be the best way to audit multilevel associations? ie. Store->Ailes->Shelves->Goods
As it is now you get double entries if you try this:
Store.Audit().AuditAssociation(s=>s.Ailes);
Ailes.audit().AuditAssociation(a=a.Shelves);
Shelves.audit().AuditAssociation(s=s.Goods);

the other option would be:
Store.Audit().AuditAssociation(s=>s.Ailes);
Shelves.audit().AuditAssociation(s=s.Goods);

but this does not register the link between ailes and shelves.