DRY with Lambda Expression (errors management)

In my current project, I have made a miracle with Lambda Expression. Have you ever implemented something like this:

    public abstract class GenericRepository<TEntity> : IRepository<TEntity> where TEntity : classIEntity
    {
        private readonly ILog _logger;

        protected GenericRepository()
            : this(IoC.GetInstance<IUnitOfWork>(), IoC.GetInstance<ILog>()
            )
        {
        }

        protected GenericRepository(IUnitOfWork unitOfWork, ILog logger)
        {
            UnitOfWork = unitOfWork;
            _logger = logger;
        }

        public IUnitOfWork UnitOfWork { getset; }

        public IEnumerable<TEntity> All()
        {
            CodeContract.Contract.Assert(UnitOfWork != null"Unit of work is null");
            CodeContract.Contract.Assert(UnitOfWork.Session != null"Session is null");

            try
            {
                return UnitOfWork.Session.QueryOver<TEntity>().List();
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                throw;
            }
        }

        public int Add(TEntity entity)
        {
            CodeContract.Contract.Assert(UnitOfWork != null"Unit of work is null");
            CodeContract.Contract.Assert(UnitOfWork.Session != null"Session is null");
            CodeContract.Contract.Assert(entity != null"Entity is null");

            try
            {
                return (int)UnitOfWork.Session.Save(entity);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                throw;
            }
        }

        public void Remove(TEntity entity)
        {
            CodeContract.Contract.Assert(UnitOfWork != null"Unit of work is null");
            CodeContract.Contract.Assert(UnitOfWork.Session != null"Session is null");
            CodeContract.Contract.Assert(entity != null"Entity is null");

            try
            {
                UnitOfWork.Session.Delete(entity);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                throw;
            }
        }

        public void Update(TEntity entity)
        {
            CodeContract.Contract.Assert(UnitOfWork != null"Unit of work is null");
            CodeContract.Contract.Assert(UnitOfWork.Session != null"Session is null");
            CodeContract.Contract.Assert(entity != null"Entity is null");

            try
            {
                UnitOfWork.Session.Update(entity);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                throw;
            }
        }

        public IEnumerable<TEntity> GetBy(Expression<Func<TEntity, bool>> condition)
        {
            CodeContract.Contract.Assert(UnitOfWork != null"Unit of work is null");
            CodeContract.Contract.Assert(UnitOfWork.Session != null"Session is null");
            CodeContract.Contract.Assert(condition != null"Expression is null");

            try
            {
                return UnitOfWork.Session.QueryOver<TEntity>()
                        .Where(condition)
                        .List();
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                throw;
            }
        }
    }

Yeah. Many try...catch... you use for error management. And I think it is very stupid, and violate the DRY (Don't repeat  yourself) principle. I give a example, one day, I will change the error management, so I must to go from the begin this class to end this class for modify it. Once again, it is very stupid and take a lot of time to manage. So I always think one solution for resolve it some weeks ago. And now in new project, I apply one technical that use the lambda expression for inverting code control.
It is very simply. I think after I present it at here, anyone also know about it.
I just make a helper class as here:

    public static class CatchExceptionHelper
    {
        /// <summary>
        /// Tries the catch action.
        /// for example, void Foo() is a action
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="logger">The logger.</param>
        public static void TryCatchAction(Action action, ILog logger)
        {
            try
            {
                action();
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                throw;
            }
        }

        /// <summary>
        /// Tries the catch function.
        /// for example, int Foo() is a function
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="function">The function.</param>
        /// <param name="logger">The logger.</param>
        /// <returns></returns>
        public static T TryCatchFunction<T>(Func<T> function, ILog logger)
        {
            try
            {
                return function();
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                throw;
            }
        }
    }

After that  just do that as here:

    public abstract class GenericRepository<TEntity> : IRepository<TEntity> where TEntity : classIEntity
    {
        private readonly ILog _logger;

        protected GenericRepository()
            : this(IoC.GetInstance<IUnitOfWork>(), IoC.GetInstance<ILog>()
            )
        {
        }

        protected GenericRepository(IUnitOfWork unitOfWork, ILog logger)
        {
            UnitOfWork = unitOfWork;
            _logger = logger;
        }

        public IUnitOfWork UnitOfWork { getset; }

        public IEnumerable<TEntity> All()
        {
            CodeContract.Contract.Assert(UnitOfWork != null"Unit of work is null");
            CodeContract.Contract.Assert(UnitOfWork.Session != null"Session is null");

            return CatchExceptionHelper.TryCatchFunction(() =>
            {
                return UnitOfWork.Session.QueryOver<TEntity>().List();
            }, _logger);
        }

        public int Add(TEntity entity)
        {
            CodeContract.Contract.Assert(UnitOfWork != null"Unit of work is null");
            CodeContract.Contract.Assert(UnitOfWork.Session != null"Session is null");
            CodeContract.Contract.Assert(entity != null"Entity is null");

            return CatchExceptionHelper.TryCatchFunction(() =>
            {
                return (int)UnitOfWork.Session.Save(entity);
            }, _logger);
        }

        public void Remove(TEntity entity)
        {
            CodeContract.Contract.Assert(UnitOfWork != null"Unit of work is null");
            CodeContract.Contract.Assert(UnitOfWork.Session != null"Session is null");
            CodeContract.Contract.Assert(entity != null"Entity is null");

            CatchExceptionHelper.TryCatchAction(() => UnitOfWork.Session.Delete(entity), _logger);
        }

        public void Update(TEntity entity)
        {
            CodeContract.Contract.Assert(UnitOfWork != null"Unit of work is null");
            CodeContract.Contract.Assert(UnitOfWork.Session != null"Session is null");
            CodeContract.Contract.Assert(entity != null"Entity is null");

            CatchExceptionHelper.TryCatchAction(() => UnitOfWork.Session.Update(entity), _logger);
        }

        public IEnumerable<TEntity> GetBy(Expression<Func<TEntity, bool>> condition)
        {
            CodeContract.Contract.Assert(UnitOfWork != null"Unit of work is null");
            CodeContract.Contract.Assert(UnitOfWork.Session != null"Session is null");
            CodeContract.Contract.Assert(condition != null"Expression is null");

            return CatchExceptionHelper.TryCatchFunction(() =>
            {
                return UnitOfWork.Session.QueryOver<TEntity>()
                        .Where(condition)
                        .List();
            }, _logger);
        }
    }

Now we shall be easy to change error management any time that not touch to this class, just modify helper class. But as you see we still need to check the Code Contract at the begin of each method. I also think it is also not good. So I will re factoring something like that:

    public abstract class GenericRepository<TEntity> : IRepository<TEntity> where TEntity : classIEntity
    {
        private readonly ILog _logger;

        protected GenericRepository()
            : this(IoC.GetInstance<IUnitOfWork>(), IoC.GetInstance<ILog>()
            )
        {
        }

        protected GenericRepository(IUnitOfWork unitOfWork, ILog logger)
        {
            UnitOfWork = unitOfWork;
            _logger = logger;
        }

        public IUnitOfWork UnitOfWork { getset; }

        public IEnumerable<TEntity> All()
        {
            return CheckContractViolated(() =>
            {
                return CatchExceptionHelper.TryCatchFunction(() =>
                {
                    return UnitOfWork.Session.QueryOver<TEntity>().List();
                }, _logger);
            });
        }

        public int Add(TEntity entity)
        {
            CodeContract.Contract.Assert(entity != null"Entity is null");

            return CheckContractViolated(() =>
            {
                return CatchExceptionHelper.TryCatchFunction(() =>
                {
                    return (int)UnitOfWork.Session.Save(entity);
                }, _logger);
            });
        }

        public void Remove(TEntity entity)
        {
            CodeContract.Contract.Assert(entity != null"Entity is null");

            CheckContractViolated(() =>
            {
                CatchExceptionHelper.TryCatchAction(() => UnitOfWork.Session.Delete(entity), _logger);
            });
        }

        public void Update(TEntity entity)
        {
            CodeContract.Contract.Assert(entity != null"Entity is null");

            CheckContractViolated(() =>
            {
                CatchExceptionHelper.TryCatchAction(() => UnitOfWork.Session.Update(entity), _logger);
            });
        }

        public IEnumerable<TEntity> GetBy(Expression<Func<TEntity, bool>> condition)
        {
            CodeContract.Contract.Assert(condition != null"Expression is null");
            return CheckContractViolated(() =>
            {
                return CatchExceptionHelper.TryCatchFunction(() =>
                {
                    return UnitOfWork.Session.QueryOver<TEntity>()
                            .Where(condition)
                            .List();
                }, _logger);
            });
        }

        private void CheckContractViolated(Action action)
        {
            CheckContractViolated();

            action();
        }

        private TReturn CheckContractViolated<TReturn>(Func<TReturn> function)
        {
            CheckContractViolated();

            return function();
        }

        private void CheckContractViolated()
        {
            CodeContract.Contract.Assert(UnitOfWork != null"Unit of work is null");
            CodeContract.Contract.Assert(UnitOfWork.Session != null"Session is null");
        }
    }

As a code above, I add three functions with named is CheckContractViolated and just delegate the responsibility to it.If anyone don't like delegated the LambdaExpression for manage the contract, you only need the third CheckContractViolated method at end of this class. But I like Lambda Expression :D. Very simple, but with me it is very cool! Happy coding!

Shout it    kick it on DotNetKicks.com

Published Friday, November 19, 2010 9:59 PM by thangchung

Comments

# re: DRY with Lambda Expression (errors management)

Friday, November 19, 2010 11:50 AM by smirnov.os

Why do you need UnitOfWork if everywhere you use only Session?

# re: DRY with Lambda Expression (errors management)

Friday, November 19, 2010 12:06 PM by thangchung

AS you see, I expose the UnitOfWork to outside. And if we have a complex scenario, we will do as:

var uow = Repository.UnitOfWork;

Repository.All();

Repository.GetBy(x=>x....);

Repository.Save(<Entity>);

uow.Commit();

and something like that. My sample just a demo, it is not good for real project. I only want to make a practice on the DRY. Thanks for your comment, smirnov!

# re: DRY with Lambda Expression (errors management)

Friday, November 19, 2010 1:16 PM by webbes

You do know that you end up with code that is a miracle indeed don't you? The one that has to solve the bugs behind you will not have a single clue of what's going on. DRY is nice but KISS is a lot nicer. IMHO

# re: DRY with Lambda Expression (errors management)

Saturday, November 20, 2010 5:15 AM by Denis

Hello. Why you introduced  CheckContractViolated(Action action) and TReturn CheckContractViolated<TReturn>(Func<TReturn> function) methods?

IMHO it will be more readable

public void Update(TEntity entity)

{

           CodeContract.Contract.Assert(entity != null, "Entity is null");

           CheckContractViolated();

           CatchExceptionHelper.TryCatchAction(() => UnitOfWork.Session.Update(entity), _logger);

       }