Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I'm new to Entity framework and Repository pattern. I'm trying to implement Repository Decorator pattern which contain basically Auditable and Archivable classes and extends the Attribute class. But when I add them on any entity class as:

C#
[Auditable]
public class Student{
public int Id;
public string Name;
}


Using entity framework code first approach, the entity 'Student' supposed to generate columns Id,Name and the columns CreatedBy, Created, UpdatedBy and Updated from IAuditable interface. But what it was generating is only columns Id and Name. So what is the correct way of implementing Repository Decorator pattern using entity framework and how to apply Auditable attribute on entity classes.?

Here I'm providing links to get some idea about repository decorator pattern.

https://efpatterns.codeplex.com/discussions/282699

https://efpatterns.codeplex.com/

Here is AuditableAttribute class extending Attribute:

C#
using System;
namespace EntityFramework.Patterns.Extensions{
    public class AuditableAttribute : Attribute { }
}

Generic AuditableRepository class:
C#
  using System;
  using System.Threading;
  using EntityFramework.Patterns.Extensions;

  namespace EntityFramework.Patterns.Decorators
  {
     public class AuditableRepository<t> : RepositoryDecoratorBase<t>
     where T : class
     {

     public AuditableRepository(IRepository<t> surrogate) : base(surrogate) {
     }

     public override void Insert(T entity)
     {
         IAuditable auditable = entity as IAuditable;
         if (auditable != null)
         {
             auditable.CreatedBy = Thread.CurrentPrincipal.Identity.Name;
             auditable.Created = DateTime.Now;
         }
         base.Insert(entity);
     }

     public override void Update(T entity)
     {
         IAuditable auditable = entity as IAuditable;
         if (auditable != null)
        {
           auditable.UpdatedBy = Thread.CurrentPrincipal.Identity.Name;
           auditable.Updated = DateTime.Now;
        }
         base.Update(entity);
      }
   }
}

Here is the interface.

C#
using System;

namespace EntityFramework.Patterns.Extensions
{
public interface IAuditable
{
    string CreatedBy { get; set; }
    DateTime? Created { get; set; }
    string UpdatedBy { get; set; }
    DateTime? Updated { get; set; }
}


I think that's enough code for getting the idea what I was going to implement. I have searched all the platform but couldn't get the help.
Posted
Updated 17-Feb-15 23:43pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900