-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: ImageType and ColorType...
PostPosted: Sun Apr 30, 2006 4:47 pm 
Beginner
Beginner

Joined: Thu Dec 08, 2005 6:49 pm
Posts: 49
Some of my domain objects have Color and Image properties that need to be persisted, so I've created a ColorType and an ImageType class.

So far it seems to be working, but I don't fully understand what all the different virtual members are used for or what parameters will be passed.

I just wanted to make sure I haven't done something silly that could cause trouble down the track.

Code:
public class ColorType : ValueTypeType
{
    public ColorType()
        : base(new Int32SqlType()) {}

    private int? ColorToInt32(object Value)
    {
        if (!Color.Empty.Equals(Value))
            return ((Color)Value).ToArgb();
        else
            return null;
    }

    private Color Int32ToColor(object Value)
    {
        if (Value is int)
            return Color.FromArgb((int)Value);
        else
            return Color.Empty;
    }

    public override string ObjectToSQLString(object val)
    {
        return ((Int32Type)NHibernateUtil.Int32).ObjectToSQLString(ColorToInt32(val));
    }

    public override object FromStringValue(string xml)
    {
        return Int32ToColor(NHibernateUtil.Int32.FromStringValue(xml));
    }

    public override object Get(IDataReader rs, string name)
    {
        return Get(rs, rs.GetOrdinal(name));
    }

    public override object Get(IDataReader rs, int index)
    {
        return Int32ToColor(NHibernateUtil.Int32.Get(rs, index));
    }

    public override void Set(IDbCommand cmd, object value, int index)
    {
        NHibernateUtil.Int32.Set(cmd, ColorToInt32(value), index);
    }

    public override string Name
    {
        get { return "Color"; }
    }

    public override Type ReturnedClass
    {
        get { return typeof(Color); }
    }
}


Code:
public class ImageType : MutableType
{
    internal sealed class Png : ImageType
    {
        public Png()
            : base(ImageFormat.Png) { }
    }

    private ImageFormat _Format;

    public ImageType()
        : this(ImageFormat.Bmp) { }

    private ImageType(ImageFormat Format)
        : base(new BinarySqlType())
    {
        _Format = Format;
    }

    private byte[] ImageToBytes(object Value)
    {
        Image val = Value as Image;

        if (val == null)
            return null;
        else
        {
            using (MemoryStream stream = new MemoryStream())
            {
                val.Save(stream, _Format);
                return stream.ToArray();
            }
        }
    }

    private Image BytesToImage(object Value)
    {
        byte[] val = Value as byte[];

        if (val == null)
            return null;
        else
        {
            using (MemoryStream stream = new MemoryStream(val))
            {
                return Image.FromStream(stream);
            }
        }
    }

    public override object DeepCopyNotNull(object val)
    {
        return BytesToImage(NHibernateUtil.Binary.DeepCopyNotNull(ImageToBytes((Image)val)));
    }

    public override object FromStringValue(string xml)
    {
        return BytesToImage((byte[])NHibernateUtil.Binary.FromStringValue(xml));
    }

    public override object Get(IDataReader rs, string name)
    {
        return Get(rs, rs.GetOrdinal(name));
    }

    public override object Get(IDataReader rs, int index)
    {
        return BytesToImage((byte[])NHibernateUtil.Binary.Get(rs, index));
    }

    public override void Set(IDbCommand cmd, object value, int index)
    {
        NHibernateUtil.Binary.Set(cmd, ImageToBytes((Image)value), index);
    }

    public override string ToString(object val)
    {
        return NHibernateUtil.Binary.ToString(ImageToBytes((Image)val));
    }

    public override bool Equals(object x, object y)
    {
        return NHibernateUtil.Binary.Equals(
            (x is Image ? ImageToBytes((Image)x) : (byte[])x),
            (y is Image ? ImageToBytes((Image)y) : (byte[])y));
    }

    public override string Name
    {
        get { return "Image"; }
    }

    public override Type ReturnedClass
    {
        get { return typeof(Image); }
    }
}


Cheers


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.