예제 #1
0
        public override void DemandLoad(object entity, EntityField[] fields)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            if (fields == null)
            {
                throw new ArgumentNullException("fields");
            }
            if (fields.Length == 0)
            {
                throw ExceptionHelper.CreateZeroLengthArgumentException("fields");
            }

            // check...
            if (EntityType == null)
            {
                throw new ArgumentNullException("EntityType");
            }
            EntityType.AssertIsOfType(entity);

            // new?
            if (this.Storage.IsNew(entity) == true)
            {
                throw new InvalidOperationException("Entity is new.");
            }
            if (this.Storage.IsDeleted(entity, false) == true)
            {
                throw new InvalidOperationException("Entity has been physically deleted.");
            }

            // create a filter...
            object[]  keyValues = this.Storage.GetKeyValues(entity);
            SqlFilter filter    = SqlFilter.CreateGetByIdFilter(this.EntityType, keyValues);

            // jm - 14-07-2006 - Hacked - Matt to sort out
            if (filter == null)
            {
                throw new InvalidOperationException("filter is null.");
            }
            filter.IgnorePartitioning = true;

            // clear the fields...
            filter.Fields.Clear();
            filter.Fields.AddRange(fields);

            // get values...
            object[] values = filter.ExecuteValues();
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            // check...
            if (values.Length != fields.Length)
            {
                throw ExceptionHelper.CreateLengthMismatchException("values", "fields", values.Length, fields.Length);
            }

            // init and set...
            this.Storage.BeginInitialize(entity);
            try
            {
                for (int index = 0; index < fields.Length; index++)
                {
                    // value...
                    EntityField field = fields[index];
                    if (field == null)
                    {
                        throw new InvalidOperationException("field is null.");
                    }
                    object value = values[index];

                    // encrypted?
                    //if(field.IsEncrypted)
                    //{
                    //	if(value == null)
                    //		value = new EncryptedValue(null, (byte[])null);
                    //	else if(value is DBNull)
                    //		value = new EncryptedValue(typeof(DBNull), (byte[])null);
                    //	else
                    //	{
                    //		// switch...
                    //		switch(field.DBType)
                    //		{
                    //			case DbType.String:
                    //			case DbType.StringFixedLength:
                    //			case DbType.AnsiString:
                    //			case DbType.AnsiStringFixedLength:
                    //				value = new EncryptedValue(field.Type, (string)value);
                    //				break;

                    //			default:
                    //				throw new NotSupportedException(string.Format("Cannot handle '{0}' ({1}).", field.DBType, field.DBType.GetType()));
                    //		}
                    //	}
                    //}

                    if ((field.DBType == DbType.AnsiStringFixedLength || field.DBType == DbType.StringFixedLength) && value is string)
                    {
                        value = ((string)value).TrimEnd();
                    }

                    // set...
                    this.Storage.SetValue(entity, field, value, SetValueReason.DemandLoad);
                }
            }
            finally
            {
                this.Storage.EndInitialize(entity);
            }
        }