Beispiel #1
0
        public static TEntity[] FindAll <TEntity>() where TEntity : new()
        {
            return(With.Transaction <TEntity[]>(delegate(IDbCommand command)
            {
                Mapping mapping = GetMappingFor(typeof(TEntity));
                command.CommandText = GenerateSelect <TEntity>();
                using (IDataReader reader = command.ExecuteReader())
                {
                    List <TEntity> entities = new List <TEntity>();
                    while (reader.Read())
                    {
                        TEntity entity = new TEntity();
                        object value = reader.GetValue(reader.GetOrdinal(mapping.PrimaryKey.Key));

                        mapping.PrimaryKey.Value.SetValue(entity,
                                                          Convert.ChangeType(value, mapping.PrimaryKey.Value.PropertyType), null);

                        foreach (KeyValuePair <string, PropertyInfo> kvp in mapping.Columns)
                        {
                            value = reader.GetValue(reader.GetOrdinal(kvp.Key));
                            if (value == DBNull.Value)
                            {
                                value = null;
                            }

                            kvp.Value.SetValue(entity,
                                               Convert.ChangeType(value, kvp.Value.PropertyType), null);
                        }

                        entities.Add(entity);
                    }
                    return entities.ToArray();
                }
            }));
        }
Beispiel #2
0
 public static void Transaction(Proc proc)
 {
     With.Transaction(delegate
     {
         proc();
     });
 }
Beispiel #3
0
        public static bool Delete <TEntity>(TEntity entity)
        {
            return(With.Transaction <bool>(delegate(IDbCommand command)
            {
                Mapping mapping = GetMappingFor(typeof(TEntity));

                command.CommandText = GenerateDelete <TEntity>();
                object value = mapping.PrimaryKey.Value.GetValue(entity, null);
                AddParameter(command, "@id", value);

                return command.ExecuteNonQuery() != 0;
            }));
        }
Beispiel #4
0
        public static void Save <TEntity>(TEntity entity)
        {
            With.Transaction(delegate(IDbCommand command)
            {
                Mapping mapping = GetMappingFor(typeof(TEntity));

                command.CommandText = GenerateUpdate <TEntity>();
                object value        = mapping.PrimaryKey.Value.GetValue(entity, null);
                AddParameter(command, "@id", value);
                int parameterIndex = 0;
                foreach (KeyValuePair <string, PropertyInfo> kvp in mapping.Columns)
                {
                    value = kvp.Value.GetValue(entity, null);
                    AddParameter(command, "@p" + parameterIndex, value);
                    parameterIndex += 1;
                }

                command.ExecuteNonQuery();
            });
        }
Beispiel #5
0
        public static void Create <TEntity>(TEntity entity)
        {
            With.Transaction(delegate(IDbCommand command)
            {
                Mapping mapping = GetMappingFor(typeof(TEntity));

                command.CommandText = GenerateCreate <TEntity>();

                int parameterIndex = 0;
                foreach (KeyValuePair <string, PropertyInfo> kvp in mapping.Columns)
                {
                    object value = kvp.Value.GetValue(entity, null);
                    AddParameter(command, "@p" + parameterIndex, value);
                    parameterIndex += 1;
                }

                object obj = command.ExecuteScalar();
                mapping.PrimaryKey.Value.SetValue(entity,
                                                  Convert.ChangeType(obj, mapping.PrimaryKey.Value.PropertyType), null);
            });
        }