Esempio n. 1
0
        public async Task <T> ToInstanceAsync <T>(T entity)
            where T : class, new()
        {
            var type = typeof(TData);

            await GetOrCreateCommand(type, RelationName, SchemaName, InsertReturns.Instance);

            BindParameters(type);

            var reader = await Command.ExecuteReaderAsync();

            var setters = ReflectionFactory.SetterDelegatesByPropertyName(typeof(T), Case.Snake);
            await reader.ReadAsync();

            for (var i = 0; i < reader.FieldCount; i++)
            {
                setters[reader.GetName(i)](entity, reader.GetValue(i));
            }

            return(entity);
        }
Esempio n. 2
0
        public static async Task <ICollection <T> > QueryAsync <T>(this NpgsqlCommand command, T entity = null, bool prepare = false)
            where T : class, new()
        {
            if (prepare)
            {
                await command.PrepareAsync();
            }

            var reader = await command.ExecuteReaderAsync();

            var setters = ReflectionFactory.SetterDelegatesByPropertyName(typeof(T), Case.Snake);
            var result  = new List <T>();

            if (entity != null && await reader.ReadAsync())
            {
                for (var i = 0; i < reader.FieldCount; i++)
                {
                    setters[reader.GetName(i)](entity, reader.GetValue(i));
                }
            }
            else
            {
                while (await reader.ReadAsync())
                {
                    var dictionary = new Dictionary <string, object>();
                    for (var i = 0; i < reader.FieldCount; i++)
                    {
                        dictionary[reader.GetName(i)] = reader.GetValue(i);
                    }

                    result.Add(dictionary.ToInstance <T>());
                }
            }

            await reader.CloseAsync();

            await reader.DisposeAsync();

            return(result);
        }