RSS

Repository and Unit of Work that is Fully Generic Part 2

04 Sep

So now we continue with the Unit Of Work Factory and the EF specific Items along with Extensions to the DB Context

UoWFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;

namespace MyCompany.Repo.Generic
{
    public class UoWFactory : IDisposable
    {
        #region Variables
        private readonly string _context;
        private readonly DbContext _DBcontext;
        #endregion

        #region Constructor(s)
        public UoWFactory(string context)
        {
            _context = context;
        }

        public UoWFactory(DbContext context)
        {
            _DBcontext = context;
        }
        #endregion

        #region Public Methods
        public IUnitOfWork GetUoW()
        {
            if (_DBcontext != null)
            {
                return new EFUnitOfWork( _DBcontext);
            }
            else
            {
                /*this will be the EL unit of work which needs to be fleshed out to 
                 * process the LINQ expressions for the IQueryable's and the like 
                 * this was beyond the scope of what I was asked to do for my project
                 * and will need to be finalized at a later time unless I have to convert from EF to EL
                 */                
               throw new NotImplementedException("EL Unit Of Work is currently unavailable");
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion

        #region Protected and Private Methods
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
                if (_DBcontext != null)
                    _DBcontext.Dispose();
        }
        ~UoWFactory()
        {
            Dispose(false);
        }
        #endregion

    }
}

EFRepository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Data.Entity;
using System.Data;
using MyCompany.Repo.Generic.Extensions;

namespace MyCompany.Repo.Generic
{
    public class EFRepository<T> : BaseGenericRepository, IGenericRepository<T> where T : class
    {
        #region Variables
        private readonly DbContext _context;
        #endregion

        #region Constructor(s)
        public EFRepository(DbContext context)
        {
            _context = context;
        }
        #endregion

        #region Public Methods

        public override Type Type
        {
            get { return typeof(T); }
        }

        public T Value { get; set; }
 
        public IQueryable<T> Get
        {
            get { return _context.Set<T>(); }
        }
 
        public IQueryable<T> GetIncluding(params Expression<Func<T, object>>[] includeProperties)
        {
            IQueryable<T> query = _context.Set<T>();
            foreach (var includeProperty in includeProperties)
            {
                query = query.Include(includeProperty);
            }
            return query;
        }
 
        public T Find(object[] keyValues)
        {
            return _context.Set<T>().Find(keyValues);
        }
 
        public void Add(T entity)
        {
            _context.Set<T>().Add(entity);
        }
 
        public void Update(T entity)
        {
            var entry = _context.Entry(entity);
            if (entry.State == EntityState.Detached)
            {
                _context.Set<T>().Attach(entity);
                entry = _context.Entry(entity);
            }
            entry.State = EntityState.Modified;
        }
 
        public void AddOrUpdate(T entity)
        {
            //uses DbContextExtensions to check value of primary key
            _context.AddOrUpdate(entity);
        }
 
        public void Delete(object[] keyValues)
        {
            //uses DbContextExtensions to attach a stub (or the actual entity if loaded)
            var stub = _context.Load<T>(keyValues);
            _context.Set<T>().Remove(stub);
        }
        
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion

        #region Protected and Private Methods
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
                if (_context != null)
                    _context.Dispose();
        }

        ~EFRepository()
        {
            Dispose(false);
        }
        #endregion

    }
}

EFUnitOfWork

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Data;

namespace MyCompany.Repo.Generic
{
    public class EFUnitOfWork : IUnitOfWork
    {
        #region Variables
        private readonly DbContext context;
        private readonly Dictionary<Type, BaseGenericRepository> _dictionary;
        #endregion

        #region Constructor(s)
        public EFUnitOfWork(DbContext context)
        {
            this.context = context;
            this.context.ChangeTracker.DetectChanges();
            this.context.Configuration.AutoDetectChangesEnabled = true;
            _dictionary = new Dictionary<Type, BaseGenericRepository>();
        }
        #endregion

        #region Repository Collection Methods
        
            private void Put<T>(EFRepository<T> item) where T : class
            {
                _dictionary[typeof(T)] = item;
            }

            private EFRepository<T> Get<T>() where T : class
            {
                if (_dictionary.ContainsKey(typeof(T)))
                {
                    return _dictionary[typeof(T)] as EFRepository<T>;
                }
                else
                {                    
                    EFRepository<T> efr = new EFRepository<T>(context);
                    Put<T>(efr);
                    return efr;
                }                
            }

        #endregion

        #region Public Methods


        public IGenericRepository<T> GetRepository<T>() where T : class
        {            
            return Get<T>();
        }

        public void SaveChanges()
        {
            context.SaveChanges();
        }

        public void RollBack()
        {
            //detect all changes (probably not required if AutoDetectChanges is set to true)
            context.ChangeTracker.DetectChanges();

            //get all entries that are changed
            var entries = context.ChangeTracker.Entries().Where(e => e.State != EntityState.Unchanged).ToList();

            //try to discard changes on every entry
            foreach (var dbEntityEntry in entries)
            {
                var entity = dbEntityEntry.Entity;

                if (entity == null) continue;

                if (dbEntityEntry.State == EntityState.Added)
                {
                    //if entity is in Added state, remove it. (there will be problems with Set methods if entity is of proxy type, in that case you need entity base type
                    var set = context.Set(entity.GetType());
                    // now lets actually remove it
                    set.Remove(entity);
                }
                else if (dbEntityEntry.State == EntityState.Modified)
                {

                    context.Entry(dbEntityEntry.Entity).CurrentValues.SetValues(context.Entry(dbEntityEntry.Entity).OriginalValues);
                    //may also need to set back to unmodified -
                    //I'm unsure if EF will do this automatically
                    context.Entry(dbEntityEntry.Entity).State = EntityState.Unchanged;                    
                }
                else if (dbEntityEntry.State == EntityState.Deleted)
                    //entity is deleted... change its values back to default and set it to unmodified
                    context.Entry(dbEntityEntry).CurrentValues.SetValues(context.Entry(dbEntityEntry).OriginalValues);
                    dbEntityEntry.State = EntityState.Unchanged;
            }
        }

        public void Dispose()
        {
            context.Dispose();
        }

        #endregion

    }
}

 In part 3 we will show the extension methods for DBContext and go into useage.


Advertisements
 
Leave a comment

Posted by on September 4, 2013 in C#, Entity Framework, Repository, SQL, Unit Of Work

 

Tags: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: