예제 #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();
        }
예제 #2
0
        public async Task <T> CreateAsync(T data)
        {
            ParameterException.Check(data != null, ParameterError.NotExistData);
            DbCommand command = EntityManager.Connection.CreateCommand();

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

            fillParameterGenerator(command, data);

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

            var id = Convert.ToInt32(await command.ExecuteScalarAsync().ConfigureAwait(false));

            EntityManager.Connection.Close();

            if (!ConstructDataInstanceCache.ContainsKey(typeof(T)))
            {
                ConstructDataInstanceCache.Add(typeof(T), CreateConstructDataInstance());
            }
            var constructingInstance = ConstructDataInstanceCache[typeof(T)];
            var result = constructingInstance(data, id);

            return(result);
        }
예제 #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();
                }
            }
        }
예제 #4
0
        public void DeleteAll(object param)
        {
            ParameterException.Check(param != null, ParameterError.NotExistParameter);
            DbCommand command = EntityManager.Connection.CreateCommand();

            command.CommandText = QueryBuilder.DeleteAll(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();
            command.ExecuteNonQuery();
            EntityManager.Connection.Close();
        }