Return to Snippet

Revision: 23283
at February 3, 2010 08:33 by bokkers


Updated Code
// Interface defining all repository functions

    interface IRepository<EntityType, IdType>
    {
        IQueryable<EntityType> GetAll();
        EntityType GetById(IdType id);
        void Add(EntityType newEntity);
        void Delete(EntityType entity);
        void Save();
    }

// Repository base class (needs override)

    public class Repository<EntityType, IdType> : IRepository<EntityType, IdType>
    {
        protected Func<IQueryable<EntityType>> QueryFunction { set; get; }
        protected Func<IdType, EntityType> GetByIdFunction { set; get; }
        protected Action SaveAction { set; get; }
        protected Action<EntityType> AddAction { set; get; }
        protected Action<EntityType> DeleteAction { set; get; }

        public IQueryable<EntityType> GetAll()
        {
            if (QueryFunction == null)
                throw new RepositoryException("QueryFunction property not set");

            try
            {
                return QueryFunction();
            }
            catch (Exception e)
            {
                throw new RepositoryException("Error in GetAll", e);
            }
        }

        public EntityType GetById(IdType id)
        {
            if (GetByIdFunction == null)
                throw new RepositoryException("GetByIdFunction property not set");

            try
            {
                EntityType entity = GetByIdFunction(id);
                return entity;
            }
            catch(Exception e)
            {
                throw new RepositoryException("Error in GetByID", e);
            }
        }

        public void Add(EntityType newEntity)
        {
            if (AddAction == null)
                throw new RepositoryException("AddAction property not set");

            try
            {
                AddAction(newEntity);
            }
            catch(Exception e)
            {
                throw new RepositoryException("Error in Add", e);
            }
        }

        public void Delete(EntityType entity)
        {
            if (DeleteAction == null)
                throw new RepositoryException("DeleteAction property not set");

            try
            {
                DeleteAction(entity);
            }
            catch (Exception e)
            {
                throw new RepositoryException("Error in Delete", e);
            }
        }

        public void Save()
        {
            if (SaveAction == null)
                throw new RepositoryException("SaveAction property not set");

            try
            {
                SaveAction();
            }
            catch (Exception e)
            {
                throw new RepositoryException("Error in Save", e);
            }
        }
    }

    public class RepositoryException : Exception
    {
        public RepositoryException(string message)
            : base(message)
        {
        }

        public RepositoryException(string message, Exception inner)
            : base(message, inner)
        {
        }
    }

// Example repository implementation, using Entity Framework as DAL

    public class PersonRepository : Repository<Person, int>
    {
        DatabaseEntities _context;
        public PersonRepository()
        {
            _context = new DatabaseEntities();

            QueryFunction = () =>  _context.Person;
            GetByIdFunction = id => _context.Person.FirstOrDefault(p => p.ID == id);

            AddAction = p => _context.AddToPerson(p);
            DeleteAction = p => _context.DeleteObject(p);
            SaveAction = () => _context.SaveChanges();
        }        
    }

Revision: 23282
at February 3, 2010 08:31 by bokkers


Initial Code
// Interface defining all repository functions

    interface IRepository<EntityType, IdType>
    {
        IQueryable<EntityType> GetAll();
        EntityType GetById(IdType id);
        void Add(EntityType newEntity);
        void Delete(EntityType entity);
        void Save();
    }

// Repository base class (needs override)

    public class Repository<EntityType, IdType> : IRepository<EntityType, IdType>
    {
        protected Func<IQueryable<EntityType>> QueryFunction { set; get; }
        protected Func<IdType, EntityType> GetByIdFunction { set; get; }
        protected Action SaveAction { set; get; }
        protected Action<EntityType> AddAction { set; get; }
        protected Action<EntityType> DeleteAction { set; get; }

        public IQueryable<EntityType> GetAll()
        {
            if (QueryFunction == null)
                throw new RepositoryException("QueryFunction property not set");

            try
            {
                return QueryFunction();
            }
            catch (Exception e)
            {
                throw new RepositoryException("Error in GetAll", e);
            }
        }

        public EntityType GetById(IdType id)
        {
            if (GetByIdFunction == null)
                throw new RepositoryException("GetByIdFunction property not set");

            try
            {
                EntityType entity = GetByIdFunction(id);
                return entity;
                //return  QueryFunction().FirstOrDefault(entity => id.Equals(GetIdFunction(entity)));
            }
            catch(Exception e)
            {
                throw new RepositoryException("Error in GetByID", e);
            }
        }

        public void Add(EntityType newEntity)
        {
            if (AddAction == null)
                throw new RepositoryException("AddAction property not set");

            try
            {
                AddAction(newEntity);
            }
            catch(Exception e)
            {
                throw new RepositoryException("Error in Add", e);
            }
        }

        public void Delete(EntityType entity)
        {
            if (DeleteAction == null)
                throw new RepositoryException("DeleteAction property not set");

            try
            {
                DeleteAction(entity);
            }
            catch (Exception e)
            {
                throw new RepositoryException("Error in Delete", e);
            }
        }

        public void Save()
        {
            if (SaveAction == null)
                throw new RepositoryException("SaveAction property not set");

            try
            {
                SaveAction();
            }
            catch (Exception e)
            {
                throw new RepositoryException("Error in Save", e);
            }
        }
    }

    public class RepositoryException : Exception
    {
        public RepositoryException(string message)
            : base(message)
        {
        }

        public RepositoryException(string message, Exception inner)
            : base(message, inner)
        {
        }
    }

// Example repository implementation, using Entity Framework as DAL

    public class PersonRepository : Repository<Person, int>
    {
        DatabaseEntities _context;
        public PersonRepository()
        {
            _context = new DatabaseEntities();

            QueryFunction = () =>  _context.Person;
            GetByIdFunction = id => _context.Person.FirstOrDefault(p => p.ID == id);

            AddAction = p => _context.AddToPerson(p);
            DeleteAction = p => _context.DeleteObject(p);
            SaveAction = () => _context.SaveChanges();
        }        
    }

Initial URL


Initial Description


Initial Title
IRepository using lambda expressions

Initial Tags


Initial Language
C#