Exemplo n.º 1
0
        public T FirstOrDefaultSync <T>(DbCommand cmd, bool prepare = true) where T : new()
        {
            if (AssignmentCache <T> .Constructor == null)
            {
                AssignmentCache <T> .Constructor = GenerateConstructor <T>();
            }

            if (prepare)
            {
                cmd.Prepare();
            }

            using (var reader = cmd.ExecuteReader())
            {
                if (AssignmentCache <T> .Action == null)
                {
                    AssignmentCache <T> .Action = GenerateAssignment <T>(DefaultNameResolver, reader);
                }

                if (!reader.Read())
                {
                    return(default(T));
                }

                var item = AssignmentCache <T> .Constructor();

                AssignmentCache <T> .Action(reader, item);

                while (reader.Read())
                {
                }
                return(item);
            }
        }
Exemplo n.º 2
0
        public async Task <T> FirstOrDefault <T>(DbCommand cmd, bool prepare = true) where T : new()
        {
            if (AssignmentCache <T> .Constructor == null)
            {
                AssignmentCache <T> .Constructor = GenerateConstructor <T>();
            }

            if (prepare)
            {
                await cmd.PrepareAsync();
            }

            using (var reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false))
            {
                if (AssignmentCache <T> .Action == null)
                {
                    AssignmentCache <T> .Action = GenerateAssignment <T>(DefaultNameResolver, reader);
                }

                if (!reader.Read())
                {
                    return(default(T));
                }

                var item = AssignmentCache <T> .Constructor();

                AssignmentCache <T> .Action(reader, item);

                while (reader.Read())
                {
                }
                return(item);
            }
        }
Exemplo n.º 3
0
        public async Task ForEach <T>(DbCommand cmd, Func <T, Task> action, bool prepare = true) where T : new()
        {
            if (AssignmentCache <T> .Constructor == null)
            {
                AssignmentCache <T> .Constructor = GenerateConstructor <T>();
            }

            if (prepare)
            {
                await cmd.PrepareAsync();
            }

            using (var reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false))
            {
                if (AssignmentCache <T> .Action == null)
                {
                    AssignmentCache <T> .Action = GenerateAssignment <T>(DefaultNameResolver, reader);
                }

                while (reader.Read())
                {
                    var item = AssignmentCache <T> .Constructor();

                    AssignmentCache <T> .Action(reader, item);
                    await action(item);
                }
            }
        }
Exemplo n.º 4
0
        public List <T> ToListSync <T>(DbCommand cmd, bool prepare = true) where T : new()
        {
            var result = new List <T>();

            if (AssignmentCache <T> .Constructor == null)
            {
                AssignmentCache <T> .Constructor = GenerateConstructor <T>();
            }

            if (prepare)
            {
                cmd.Prepare();
            }

            using (var reader = cmd.ExecuteReader())
            {
                if (AssignmentCache <T> .Action == null)
                {
                    AssignmentCache <T> .Action = GenerateAssignment <T>(DefaultNameResolver, reader);
                }

                while (reader.Read())
                {
                    var item = AssignmentCache <T> .Constructor();

                    AssignmentCache <T> .Action(reader, item);

                    result.Add(item);
                }
            }

            return(result);
        }
Exemplo n.º 5
0
        public async Task <List <T> > ToList <T>(DbCommand cmd) where T : new()
        {
            var result = new List <T>();

            if (AssignmentCache <T> .Constructor == null)
            {
                AssignmentCache <T> .Constructor = GenerateConstructor <T>();
            }

            using (var reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false))
            {
                if (AssignmentCache <T> .Action == null)
                {
                    AssignmentCache <T> .Action = GenerateAssignment <T>(DefaultNameResolver, reader);
                }

                while (reader.Read())
                {
                    var item = AssignmentCache <T> .Constructor();

                    AssignmentCache <T> .Action(reader, item);

                    result.Add(item);
                }
            }

            return(result);
        }
Exemplo n.º 6
0
        public async Task ForEach <T>(DbCommand cmd, Func <T, Task> action) where T : new()
        {
            using (var reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false))
            {
                if (AssignmentCache <T> .Action == null)
                {
                    AssignmentCache <T> .Action = GenerateAssignment <T>(DefaultNameResolver, reader);
                }

                while (reader.Read())
                {
                    var item = new T();
                    AssignmentCache <T> .Action(reader, item);
                    await action(item);
                }
            }
        }