Beispiel #1
0
        internal Converter <DbDataReader, T> GetConverter <T>(string spName, DbDataReader schema, int resultDepth)
        {
            Type   entityType = typeof(T);
            string key        = String.Concat(spName, entityType.Name, schema.FieldCount.ToString(), resultDepth.ToString());
            var    converter  = (Converter <DbDataReader, T>)Cache[key];

            if (converter == null)
            {
                TypeCode code = Type.GetTypeCode(entityType);
                if (code == TypeCode.Object && entityType != typeof(Guid))
                {
                    var convertibles = GetProperties(spName, entityType);
                    if (schema.FieldCount <= convertibles.Count)
                    {
                        converter = EntityConverter <T> .CreateReadSingle(convertibles.ToArray());
                    }
                }
                if (converter == null)
                {
                    converter = EntityUtility.CreateConverter <T>();
                }
                Cache[key] = converter;
            }
            return(converter);
        }
Beispiel #2
0
        public TEntity GetRow <TEntity>()
        {
            this.CheckDataReader();
            if (_changeType == SqlRowChangedTypes.Deleted)
            {
                return(default(TEntity));
            }

            var converter = EntityUtility.CreateConverter <TEntity>();

            return(converter(DataReader));
        }
Beispiel #3
0
        private void DeriveAssignParametersByEntity(DbCommand cmd, object entity)
        {
            DbParameter[] discoveredParameters = base.GetDeriveParameters(cmd);
            if (cmd.Parameters.Count == 0)
            {
                cmd.Parameters.Add(((ICloneable)discoveredParameters[0]).Clone());
            }
            string spName = cmd.CommandText;
            IEnumerable <PropertyAccess> enumerable = EntityUtility.GetEnumerable(entity.GetType());

            for (int i = 1; i < discoveredParameters.Length; i++)
            {
                DbParameter discoveredParameter = discoveredParameters[i];
                foreach (PropertyAccess property in enumerable)
                {
                    var    attr = (System.Data.Linq.Mapping.ColumnAttribute)Attribute.GetCustomAttribute(property.EntityProperty, typeof(System.Data.Linq.Mapping.ColumnAttribute));
                    string name;
                    if (attr == null)
                    {
                        name = base.Factory.ParameterNamePrefix + property.MappedName;
                    }
                    else
                    {
                        name = base.Factory.ParameterNamePrefix + (string.IsNullOrEmpty(attr.Name) ? property.MappedName : attr.Name);
                    }
                    if (name == discoveredParameter.ParameterName)
                    {
                        object value = property.GetValue(entity) ?? DBNull.Value;
                        int    index = cmd.Parameters.IndexOf(discoveredParameter.ParameterName);
                        if (index == -1)
                        {
                            object cloned = ((ICloneable)discoveredParameter).Clone();
                            ((DbParameter)cloned).Value = value;
                            cmd.Parameters.Add(cloned);
                        }
                        else
                        {
                            cmd.Parameters[index].Value = value;
                        }
                        break;
                    }
                }
            }
        }