/// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <param name="queryAction"></param>
        /// <param name="serviceContract"></param>
        /// <param name="queryContract"></param>
        /// <returns></returns>
        protected OperationResult <TEntity> ExecuteQuery <TEntity>(TEntity entity, DbQueryActions queryAction, IDataServiceContract serviceContract, IDbQueryContract queryContract)
            where TEntity : class
        {
            using (var _operationContext = new DbQueryOperationContext())
            {
                if (!IsExceptionalType(entity))
                {
                    var _memberDescriptor = _operationContext.DescriptorManager.GetDescriptor(entity);
                    var _serviceContract  = serviceContract ?? _memberDescriptor.ServiceContract;
                    var _operationSession = _operationContext.CreateSession(_serviceContract, queryAction);

                    _operationSession.Open();

                    ExecuteQueryImpl(_operationSession, entity, _memberDescriptor, _serviceContract, queryContract);

                    _operationSession.Close();
                }
                else
                {
                    var _entities = entity as IList;

                    for (var i = 0; i < _entities.Count; i++)
                    {
                        if (!_operationContext.HasErrors)
                        {
                            var _memberDescriptor = _operationContext.DescriptorManager.GetDescriptor(_entities[i]);
                            var _serviceContract  = serviceContract ?? _memberDescriptor.ServiceContract;
                            var _operationSession = _operationContext.CreateSession(_serviceContract, queryAction);

                            _operationSession.Open();

                            ExecuteQueryImpl(_operationSession, _entities[i], _memberDescriptor, _serviceContract, queryContract);

                            _operationSession.Close();

                            continue;
                        }

                        break;
                    }
                }

                if (_operationContext.HasErrors)
                {
                    return(new OperationResult <TEntity>(entity, _operationContext.Errors));
                }
            }

            return(new OperationResult <TEntity>(entity));
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        protected OperationResult <TEntity> ExecuteQuery <TEntity>(Action <IDbQueryParameterizable> predicate = default(Action <IDbQueryParameterizable>))
            where TEntity : class, new()
        {
            var _entity = new TEntity();

            var _stackFrame      = StackTraceHelpers.GetStackFrameByIndex(1);
            var _queryContract   = RetrieveDbQueryContract(_stackFrame);
            var _serviceContract = RetrieveDataServiceContract(_stackFrame);

            VerifyQueryContract(_queryContract);
            VerifyServiceContract(_serviceContract);

            using (var _operationContext = new DbQueryOperationContext())
            {
                InitializeRequiredProperties(_operationContext, _queryContract);

                var _memberDescriptor = _operationContext.DescriptorManager.GetDescriptor(_entity);
                var _operatingSession = _operationContext.CreateSession(_serviceContract, _queryContract.QueryAction);
                var _queryContext     = _operatingSession.CreateDbQueryContext(_memberDescriptor);

                InitializeRequiredProperties(_queryContext, _queryContract);

                _operatingSession.Open();

                if (default(Action <IDbQueryParameterizable>) != predicate)
                {
                    predicate(_queryContext);
                }

                var _queryResult = _queryContext.Execute();

                if (null != _queryResult && _queryResult.HasResult)
                {
                    var _resultMapper = _operatingSession.CreateDbQueryResultMapper(_memberDescriptor);

                    _resultMapper.Map(_entity, _queryResult);
                }

                _operatingSession.Close();

                if (_operationContext.HasErrors)
                {
                    return(new OperationResult <TEntity>(_entity, _operationContext.Errors));
                }
            }

            return(new OperationResult <TEntity>(_entity));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        protected OperationResult <IDbQueryResult> ExecuteQuery(Action <IDbQueryParameterizable> predicate = default(Action <IDbQueryParameterizable>))
        {
            var _queryResult = (IDbQueryResult)null;

            var _stackFrame      = StackTraceHelpers.GetStackFrameByIndex(1);
            var _queryContract   = RetrieveDbQueryContract(_stackFrame);
            var _serviceContract = RetrieveDataServiceContract(_stackFrame);

            VerifyQueryContract(_queryContract);
            VerifyServiceContract(_serviceContract);

            using (var _operationContext = new DbQueryOperationContext())
            {
                InitializeRequiredProperties(_operationContext, _queryContract);

                var _operatingSession = _operationContext.CreateSession(_serviceContract, _queryContract.QueryAction);
                var _queryContext     = _operatingSession.CreateDbQueryContext(_serviceContract);

                InitializeRequiredProperties(_queryContext, _queryContract);

                _operatingSession.Open();

                if (default(Action <IDbQueryParameterizable>) != predicate)
                {
                    predicate(_queryContext);
                }

                _queryResult = _queryContext.Execute();

                _operatingSession.Close();

                if (_operationContext.HasErrors)
                {
                    return(new OperationResult <IDbQueryResult>(_queryResult, _operationContext.Errors));
                }
            }

            return(new OperationResult <IDbQueryResult>(_queryResult));
        }