Auditable Repository sample code

Dec 12, 2011 at 10:38 PM


Are there any available samples I could have a look how do you use AuditableRepository and how does it ties together with generic repository when you update or insert a new entity?

Many Thanks.

Dec 12, 2011 at 11:17 PM

Hi madness2K4,

You might want to decorate your generic repository with the AuditableRepository when you work with entities that implements IAuditable interface.

To decorate the repository you can use this syntax :

using (EfContext ctx = new EfContext())
       DbContextAdapter adpt = new DbContextAdapter(ctx);
       IRepository<WorkBench> repo =
           new AuditableRepository<WorkBench>(
                new Repository<WorkBench>(adpt)

        IEnumerable<WorkBench> a = repo.GetAll();

The particularity of the AuditableRepository is that when it detect that the entity implements IAuditable, it will :

  1. Set Created and CreatedBy properties of the entity when inserting a new item.
  2. Set Updated and UpdatedBy properties of the entity when updating an existing item.


Note that if you mark your entity with the Auditable attribute :

public class WorkBench
    public int Id { get; set; }
    public string Name { get; set; }

the IAuditable interface will be automatically implemented at compile time thanks to the AOP engine.

I hope this will help you getting started with repository decorators.
Dec 13, 2011 at 11:03 AM

Hi Fab_Michellonet,

Thanks for replying, one other thing in the database table WorkBench would it have Created, CreatedBy, Updated and UpdatedBy columns?


Dec 13, 2011 at 4:10 PM

Since the entity implements the interface IAuditable (manually or automatically through the attribute), the table will be materialized with the columns Created, CreatedBy, Updated and UpdatedBy.

Dec 13, 2011 at 8:54 PM

When you said "the table will be materialized with the columns" you're using code first approach?

If so, from database first approach would I need those columns in the table already then generate my model and then add Auditable attribute to my POCO class?

Would that work or I would need to remove Created, CreatedBy, Updated and UpdatedBy on my POCO and let the Auditable attribute implement it?

Dec 13, 2011 at 9:38 PM
Edited Nov 18, 2013 at 9:19 AM
Yes, I assumed (wrongly) that you were using Code First. Now, if you are using Database First it should have to : - Add the columns Created, CreatedBy, Updated and UpdatedBy in table Workbench - Generate the model - Add the interface IAuditable on your POCO. (Since Created, CreatedBy, Updated and UpdatedBy properties already exist in the model, you shouldn't have to implement anything) You should be able to successfully use AuditableRepository
Jan 28, 2013 at 8:27 PM

How would you use the decorator for the AuditableRepository when creating a composition root using Unity?

AuditableRepository takes an IRepository, in other to proxy it, but if I need to resove IRepository to an AuditableRepository, how is that setup in the IoC container?

If I don't set this up, I get an exception from SQL about datetime2 data type not matching DateTime.