HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Review Repository and UnitOfWork implementation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
unitofworkrepositoryandimplementationreview

Problem

I've done a LOT of reading about creating a Unit Of Work and Repository based implementation for my Entity Framework based application. I have come up with the following - what problems can you find?

The theory of making the actual SQL Server unit of work, context, and repository coupled to entity framework is that these three implementations will not be used independently. I can create a fake unit Unit of work and Repository for unit testing without worrying about the context as the Unit of Work is responsible for instantiating the concrete repository.

p.s. Do I need to implement IDisposable on the repository?

Dgc.CriminalJustice.Model

Repository Interface

public interface IRepository : IDisposable
                  where T : class
{
    void Add(T entity);
    void Delete(T entity);
    void Update(T entity);
    T GetById(int id);
    IEnumerable All();
    IEnumerable Find(Expression> filter = null, string includeProperties = "");
}


Unit of work interface

public interface IUnitOfWork : IDisposable 
{
    IRepository PersonRepository { get; }
    void Save();
}


Dgc.CriminalJustice.Data.SqlServer

Entity Framework Context

public class CriminalJusticeContext : DbContext
{
    static CriminalJusticeContext()
    {
        Database.SetInitializer(null);
    }

    public CriminalJusticeContext()
        : base("Name=CriminalJusticeContext")
    {
    }

    public DbSet Addresses { get; set; }
    public DbSet People { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new Mapping.AddressMap());
        modelBuilder.Configurations.Add(new Mapping.PersonMap());
        modelBuilder.Configurations.Add(new Mapping.AliasMap());
        modelBuilder.Configurations.Add(new Mapping.CaseNoteMap());
    }

}


Entity framework based Repository

```
public class Repository : IRepository
where T : class
{
private CriminalJusticeContext context;
private I

Solution

In your Repository, you might want to check the EntityState of your entity before manipulating them in your DbSet like this:

public  void Add(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Detached)
        {
            dbEntityEntry.State = EntityState.Added;
        }
        else
        {
            DbSet.Add(entity);
        }
    }

    public void Update(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State == EntityState.Detached)
        {
            DbSet.Attach(entity);
        }
        dbEntityEntry.State = EntityState.Modified;
    }

    public void Delete(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Deleted)
        {
            dbEntityEntry.State = EntityState.Deleted;
        }
        else
        {
            DbSet.Attach(entity);
            DbSet.Remove(entity);
        }
    }

Code Snippets

public  void Add(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Detached)
        {
            dbEntityEntry.State = EntityState.Added;
        }
        else
        {
            DbSet.Add(entity);
        }
    }

    public void Update(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State == EntityState.Detached)
        {
            DbSet.Attach(entity);
        }
        dbEntityEntry.State = EntityState.Modified;
    }

    public void Delete(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Deleted)
        {
            dbEntityEntry.State = EntityState.Deleted;
        }
        else
        {
            DbSet.Attach(entity);
            DbSet.Remove(entity);
        }
    }

Context

StackExchange Code Review Q#27766, answer score: 3

Revisions (0)

No revisions yet.