Example #1
0
 public LazyLoadingEnumerator(IDataReader reader, IDataRecordMapper <TEntity> mapper)
 {
     if (reader == null)
     {
         throw new ArgumentNullException("reader");
     }
     _reader = reader;
     _mapper = mapper;
 }
            public IDatabaseBuilder UseDataRecordMapper <T>(IDataRecordMapper <T> mapper)
            {
                if (mapper == null)
                {
                    throw new ArgumentNullException(nameof(mapper));
                }

                _dataRecordMapperFactory.UseCustomMapper(mapper);
                return(this);
            }
Example #3
0
 public LazyLoadingResult(IDbCommand command, IDataRecordMapper <TEntity> mapper)
 {
     if (command == null)
     {
         throw new ArgumentNullException("command");
     }
     if (mapper == null)
     {
         throw new ArgumentNullException("mapper");
     }
     _command = command;
     _mapper  = mapper;
 }
Example #4
0
        private static IEnumerable <T> ToEnumerable <T>(IDataReader dataReader, IDataRecordMapper mapper)
        {
            if (dataReader.FieldCount == 0)
            {
                yield break;
            }

            while (dataReader.Read())
            {
                yield return(mapper.MapDataRecord <T>(dataReader));
            }
            yield break;
        }
Example #5
0
        /// <summary>
        /// Gets the enumerable from reader.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader">The reader.</param>
        /// <returns>IEnumerable&lt;T&gt;.</returns>
        /// <exception cref="System.ArgumentNullException">reader</exception>
        public static IEnumerable <T> GetEnumerableFromReader <T>(IDataReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            StringComparer comparer = StringComparer.InvariantCultureIgnoreCase;

            Type type = typeof(T);

            try
            {
                while (reader.Read())
                {
                    T newObj = (T)Activator.CreateInstance(type, true);

                    IDataRecordMapper mapper = newObj as IDataRecordMapper;
                    if (mapper != null)
                    {
                        mapper.MapValues(reader);
                    }
                    else
                    {
                        var properties = type.GetProperties();
                        //resort to reflection to map the object
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            if (!reader.IsDBNull(i))
                            {
                                string name  = reader.GetName(i);
                                object value = reader.GetValue(i);

                                var pi = properties.FirstOrDefault(x => comparer.Equals(x.Name, name));
                                if (pi != null)
                                {
                                    pi.SetValue(newObj, value, null);
                                }
                            }
                        }
                    }

                    yield return(newObj);
                }
            }
            finally
            {
                reader.Dispose();
            }
        }
Example #6
0
        /// <summary>
        /// Register a specific mapper.
        /// </summary>
        /// <typeparam name="TEntityType">Type which should be mapped from a query.</typeparam>
        /// <param name="mapper">Mapper</param>
        public void Register <TEntityType>(IDataRecordMapper <TEntityType> mapper) where TEntityType : class
        {
            if (mapper == null)
            {
                throw new ArgumentNullException("mapper");
            }

            var entityType = typeof(TEntityType);

            if (_mappers.ContainsKey(entityType))
            {
                throw new MappingException(
                          string.Format("There is already an mapper ({0}) for entity type {1}. Cant register {2}.",
                                        _mappers[entityType].GetType().FullName, entityType.FullName, mapper.GetType().FullName));
            }

            _mappers.Add(entityType, mapper);
        }
Example #7
0
        private IEnumerable <T> ToEnumerable <T>(IConnection connection, DbCommand command, DbDataReader dataReader)
        {
            using (connection)
                using (command)
                    using (dataReader)
                    {
                        if (dataReader.FieldCount == 0)
                        {
                            yield break;
                        }

                        IDataRecordMapper <T> mapper = null;
                        while (dataReader.Read())
                        {
                            mapper ??= _dataRecordMapperFactory.CreateMapper <T>();
                            yield return(mapper.MapDataRecord(dataReader));
                        }
                        yield break;
                    }
        }
 /// <summary>
 /// Creates a new instance of <see cref="SqlServerUnitOfWorkFactory"/>
 /// </summary>
 /// <param name="sqlServerConnectionString">The connection string to use</param>
 /// <param name="dataRecordMapper">The <see cref="IDataRecordMapper"/> implementation to use to create the <see cref="IUnitOfWork{TEntity, TKey}"/> sql server objects</param>
 public SqlServerUnitOfWorkFactory(string sqlServerConnectionString, IDataRecordMapper dataRecordMapper)
 {
     Database        = new SqlServerDatabase(sqlServerConnectionString, dataRecordMapper);
     ProviderFactory = new SqlServerDialectFactory();
 }
Example #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataReaderMapper{T}"/> class.
 /// </summary>
 /// <param name="dataRecordMapper">The <see cref="IDataRecordMapper{T}"/> that is responsible for
 /// mapping a <see cref="IDataRecord"/> to an instance of <typeparamref name="T"/>.</param>
 public DataReaderMapper(IDataRecordMapper <T> dataRecordMapper)
 {
     this.dataRecordMapper = dataRecordMapper;
 }
Example #10
0
 /// <summary>
 /// Creates a new instance of the database class
 /// </summary>
 /// <param name="connectionFactory">The function that will create a new instance of the <see cref="IDbConnection"/> object</param>
 /// <param name="dataRecordMapper">A <see cref="IDataRecordMapper"/> implementation to convert <see cref="IDataRecord"/> into data entities</param>
 public Database(Func <IDbConnection> connectionFactory, IDataRecordMapper dataRecordMapper)
 {
     _connectionFactory = connectionFactory;
     _mapper            = dataRecordMapper;
 }
Example #11
0
 public IDataRecordMapperFactory UseCustomMapper <T>(IDataRecordMapper <T> mapper)
 {
     _customMappers.Add(typeof(T), mapper);
     return(this);
 }
 /// <summary>
 /// Creates a new instance of the <see cref="SqlServerDatabase" class with the provided connection string/>
 /// </summary>
 /// <param name="connectionString">The parameters to create the Sql Server connection</param>
 /// <param name="dataRecordMapper">A <see cref="IDataRecordMapper"/> implementation to convert <see cref="IDataRecord"/> into data entities</param>
 public SqlServerDatabase(string connectionString, IDataRecordMapper dataRecordMapper) : base((() => { return(new SqlConnection(connectionString)); }), dataRecordMapper)
 {
 }