예제 #1
0
        /// <summary>
        /// Loads all rows from the database (in a streaming fashion, allows you to traverse all
        /// objects without worrying about memory usage)
        /// </summary>
        public IEnumerable <T> LoadAll()
        {
            using (var connection = _factory.OpenSqlConnection())
            {
                using (var transaction = connection.BeginTransaction(_settings.TransactionIsolationLevel))
                {
                    using (var command = connection.CreateCommand())
                    {
                        command.Transaction    = transaction;
                        command.CommandTimeout = _settings.CommandTimeoutSeconds;
                        command.CommandType    = CommandType.Text;
                        command.CommandText    = _schemaManager.GetQuery();

                        using (var reader = command.ExecuteReader())
                        {
                            var classMapProperties = _classMap.Properties.ToDictionary(p => p.PropertyName);
                            var lookup             = new DataReaderLookup(reader, classMapProperties);

                            while (reader.Read())
                            {
                                yield return((T)_activator.CreateInstance(lookup));
                            }
                        }
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Loads all rows that match the given criteria. The <paramref name="criteria"/> must be specified on the form
        /// <code>[someColumn] = @someValue</code> where the accompanying <paramref name="args"/> would be something like
        /// <code>new { someValue = "hej" }</code>
        /// </summary>
        public async Task <List <T> > LoadWhereAsync(string criteria, object args = null)
        {
            if (criteria == null)
            {
                throw new ArgumentNullException(nameof(criteria));
            }

            var results = new List <T>();

            using (var connection = _factory.OpenSqlConnection())
            {
                using (var transaction = connection.BeginTransaction(_settings.TransactionIsolationLevel))
                {
                    using (var command = connection.CreateCommand())
                    {
                        command.Transaction    = transaction;
                        command.CommandTimeout = _settings.CommandTimeoutSeconds;
                        command.CommandType    = CommandType.Text;

                        var querySql   = _schemaManager.GetQuery(criteria);
                        var parameters = GetParameters(args);

                        if (parameters.Any())
                        {
                            foreach (var parameter in parameters)
                            {
                                parameter.AddTo(command);
                            }
                        }

                        command.CommandText = querySql;

                        try
                        {
                            using (var reader = await command.ExecuteReaderAsync())
                            {
                                var classMapProperties = _classMap.Properties.ToDictionary(p => p.PropertyName);
                                var lookup             = new DataReaderLookup(reader, classMapProperties);

                                while (reader.Read())
                                {
                                    var instance = (T)_activator.CreateInstance(lookup);

                                    results.Add(instance);
                                }
                            }
                        }
                        catch (Exception exception)
                        {
                            throw new ApplicationException($"Could not execute SQL {querySql}", exception);
                        }
                    }
                }
            }

            return(results);
        }
예제 #3
0
        /// <summary>
        /// Asynchronously loads all rows from the database (in a streaming fashion, allows you to traverse all
        /// objects without worrying about memory usage) using the given <paramref name="connection"/> (possibly also enlisting the command in the given <paramref name="transaction"/>)
        /// </summary>
        public async IAsyncEnumerable <T> LoadAllAsync(SqlConnection connection, SqlTransaction transaction = null)
        {
            using var command = connection.CreateCommand();

            command.Transaction    = transaction;
            command.CommandTimeout = _settings.CommandTimeoutSeconds;
            command.CommandType    = CommandType.Text;
            command.CommandText    = _schemaManager.GetQuery();

            using var reader = await command.ExecuteReaderAsync();

            var classMapProperties = _classMap.Properties.ToDictionary(p => p.PropertyName);
            var lookup             = new DataReaderLookup(reader, classMapProperties);

            while (await reader.ReadAsync())
            {
                yield return((T)_activator.CreateInstance(lookup));
            }
        }