Esempio n. 1
0
        /// <summary>
        /// Calls the stored procedure and maps the result to the returned entity
        /// </summary>
        /// <typeparam name="TStoredProcedure">Type of stored procedure</typeparam>
        /// <param name="initialise">Action to initialise stored procedure - e.g. set up input parameters</param>
        /// <param name="execute">Function to execute the stored procedure and return a data reader</param>
        /// <returns>Populated entity</returns>
        public TEntity From <TStoredProcedure>(Action <TStoredProcedure> initialise, Expression <Func <TStoredProcedure, IDataReader> > execute)
            where TStoredProcedure : IDisposable, new()
        {
            if (_map == null)
            {
                throw new InvalidOperationException("Map must be populated from With<> call");
            }
            TEntity entity = new TEntity();

            using (TStoredProcedure sp = new TStoredProcedure())
            {
                initialise(sp);
                Func <TStoredProcedure, IDataReader> executor = execute.Compile();
                using (IDataReader reader = executor.Invoke(sp))
                {
                    if (entity is IList)
                    {
                        _map.PopulateList((IList)entity, reader);
                    }
                    else
                    {
                        if (reader.Read())
                        {
                            _map.Populate(entity, reader);
                        }
                    }
                }
            }
            return(entity);
        }
Esempio n. 2
0
        private void RunMap(TEntity entity, IDataReader reader)
        {
            IList list = entity as IList;

            if (list != null)
            {
                _dataReaderMap.PopulateList(list, reader);
            }
            else
            {
                if (reader.Read())
                {
                    _dataReaderMap.Populate(entity, reader);
                }
            }
        }