Esempio n. 1
0
        public IEnumerable <T> ReadAll(object param)
        {
            ParameterException.Check(param != null, ParameterError.NotExistParameter);
            DbCommand command = EntityManager.Connection.CreateCommand();

            command.CommandText = QueryBuilder.ReadAll(param);
            command.CommandType = CommandType.Text;
            if (!ParameterGeneratorCache.ContainsKey(param.GetType()))
            {
                ParameterGeneratorCache.Add(param.GetType(), CreateParamInfoGenerator(param));
            }
            var fillParameterGenerator = ParameterGeneratorCache[param.GetType()];

            fillParameterGenerator(command, param);

            EntityManager.Connection.Open();

            using (var reader = command.ExecuteReader())
            {
                if (!ConstructInstanceCache.ContainsKey(typeof(T)))
                {
                    ConstructInstanceCache.Add(typeof(T), CreateConstructInstance(reader));
                }
                var constructingInstance = ConstructInstanceCache[typeof(T)];
                while (reader.Read())
                {
                    object row = constructingInstance(reader);
                    yield return((T)row);
                }
            }
            EntityManager.Connection.Close();
        }
Esempio n. 2
0
        public IEnumerable <T> ReadAll()
        {
            DbCommand command = EntityManager.Connection.CreateCommand();

            command.CommandText = QueryBuilder.ReadAll();
            command.CommandType = CommandType.Text;

            EntityManager.Connection.Open();

            using (var reader = command.ExecuteReader())
            {
                if (!ConstructInstanceCache.ContainsKey(typeof(T)))
                {
                    ConstructInstanceCache.Add(typeof(T), CreateConstructInstance(reader));
                }
                var constructingInstance = ConstructInstanceCache[typeof(T)];

                while (reader.Read())
                {
                    object row = constructingInstance(reader);
                    yield return((T)row);
                }
            }

            EntityManager.Connection.Close();
        }
Esempio n. 3
0
        public async Task <IEnumerable <T> > ReadAllAsync(object param)
        {
            DbDataReader    reader   = null;
            IEnumerable <T> result   = null;
            bool            isClosed = EntityManager.Connection.State == ConnectionState.Closed;

            ParameterException.Check(param != null, ParameterError.NotExistParameter);
            DbCommand command = EntityManager.Connection.CreateCommand();

            command.CommandText = QueryBuilder.ReadAll(param);
            command.CommandType = CommandType.Text;
            if (!ParameterGeneratorCache.ContainsKey(param.GetType()))
            {
                ParameterGeneratorCache.Add(param.GetType(), CreateParamInfoGenerator(param));
            }
            var fillParameterGenerator = ParameterGeneratorCache[param.GetType()];

            fillParameterGenerator(command, param);

            var source      = new CancellationTokenSource();
            var cancelToken = source.Token;
            var behavior    = CommandBehavior.SequentialAccess | CommandBehavior.SingleResult;

            try
            {
                await EntityManager.Connection.OpenAsync().ConfigureAwait(false);

                reader = await command.ExecuteReaderAsync(GetBehavior(isClosed, behavior), cancelToken)
                         .ConfigureAwait(false);

                if (!ConstructInstanceCache.ContainsKey(typeof(T)))
                {
                    ConstructInstanceCache.Add(typeof(T), CreateConstructInstance(reader));
                }
                var constructingInstance = ConstructInstanceCache[typeof(T)];
                isClosed = false;
                result   = ExecuteReaderSync(reader, constructingInstance);
                return(result);
            }

            finally
            {
                if (isClosed)
                {
                    EntityManager.Connection.Close();
                }
            }
        }
Esempio n. 4
0
        public async Task <IEnumerable <T> > ReadAllAsync()
        {
            DbDataReader    reader   = null;
            IEnumerable <T> result   = null;
            bool            isClosed = EntityManager.Connection.State == ConnectionState.Closed;
            var             command  = EntityManager.Connection.CreateCommand();

            command.CommandText = QueryBuilder.ReadAll();
            command.CommandType = CommandType.Text;

            var source      = new CancellationTokenSource();
            var cancelToken = source.Token;
            var behavior    = CommandBehavior.SequentialAccess | CommandBehavior.SingleResult;

            try
            {
                await EntityManager.Connection.OpenAsync().ConfigureAwait(false);

                reader = await command.ExecuteReaderAsync(GetBehavior(isClosed, behavior), cancelToken)
                         .ConfigureAwait(false);

                if (!ConstructInstanceCache.ContainsKey(typeof(T)))
                {
                    ConstructInstanceCache.Add(typeof(T), CreateConstructInstance(reader));
                }
                var constructingInstance = ConstructInstanceCache[typeof(T)];
                isClosed = false;
                result   = ExecuteReaderSync(reader, constructingInstance);
                return(result);
            }

            finally
            {
                if (isClosed)
                {
                    EntityManager.Connection.Close();
                }
            }
        }
Esempio n. 5
0
        public T Read(int id)
        {
            DbCommand command = EntityManager.Connection.CreateCommand();

            command.CommandText = QueryBuilder.Read(id);
            command.CommandType = CommandType.Text;
            EntityManager.Connection.Open();

            T result = default(T);

            using (var reader = command.ExecuteReader())
            {
                reader.Read();
                if (!ConstructInstanceCache.ContainsKey(typeof(T)))
                {
                    ConstructInstanceCache.Add(typeof(T), CreateConstructInstance(reader));
                }
                var constructingInstance = ConstructInstanceCache[typeof(T)];
                result = constructingInstance(reader);
            }
            EntityManager.Connection.Close();

            return(result);
        }
Esempio n. 6
0
        public async Task <T> ReadAsync(int id)
        {
            DbCommand command = EntityManager.Connection.CreateCommand();

            command.CommandText = QueryBuilder.Read(id);
            command.CommandType = CommandType.Text;
            await EntityManager.Connection.OpenAsync().ConfigureAwait(false);

            T result = default(T);

            using (var reader = await command.ExecuteReaderAsync().ConfigureAwait(false))
            {
                reader.Read();
                if (!ConstructInstanceCache.ContainsKey(typeof(T)))
                {
                    ConstructInstanceCache.Add(typeof(T), CreateConstructInstance(reader));
                }
                var constructingInstance = ConstructInstanceCache[typeof(T)];
                result = constructingInstance(reader);
            }

            EntityManager.Connection.Close();
            return(result);
        }