2
votes

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:

[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:

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

   } 

Generic AuditableRepository class:

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.

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; }
    }

}

1
Show your AuditableAttribute implementation please. - SBirthare
AuditableAttribute class is empty and extending Attibute as: 'public class AuditableAttribute : Attribute { }' - user3578682
Do I need to add some code in AuditableAttribue class.? - user3578682
I have not seen the link you posted, in order to get columns generated, your Student class needs to derive from base class that has those extra default column. - SBirthare
I could not found the code that demonstrate what you are trying to achieve in the given link. You need to share more code, how are you expecting extra columns to be generated by EF. Either implement it into Student class or derive from base class that has common properties. - SBirthare

1 Answers

3
votes

So, what it would seem you have there is some dead code (or, more accurately, some not-yet-live code): it appears the author stubbed this out as a good idea some years ago, and it's been left on the vine ever since. You can see his last commit was almost 1.5 years ago, and the last one before that was almost the same time span.

Something that's not quite as widely downloaded from nuget.org but is more actively maintained is the excellent Highway.Data Framework, which my company uses on our projects – it even has an IAuditableInterceptor that's fully implemented! (Caveat: wish I could say that I've actually used this feature, but the rest of the framework is top-notch.)

Even better – if you're just learning EF – start with the basic EF6 nuget package and get comfortable with that first. That way, you won't be left guessing whether EF is fouling you up, or some unimplemented, third-party, library.