Persisting type 'object'  
Author Message
Terotech.Com Ltd





PostPosted: LINQ Project General, Persisting type 'object' Top

Hi again,

has anyone tried (or discovered) how we can use Linq to Sql (notice I've used the new name there) to persist objects into VARBINARY columns

I know its not a nice requirement, but I want to be able to do it.

Regards,

D




Visual Studio 200819  
 
 
Terotech.Com Ltd





PostPosted: LINQ Project General, Persisting type 'object' Top

Nearly got it.

Persist the object as an IMAGE datatype in the SQL table, for this the object must implement the IConvertible interface and be serialisable (as in the partial example below).

The problem now comes from the fact that the return type is object, so Link to SQL aka DLinq doesn't know how to deserialise it ....... the revised question is therefore how can I control the retrieval of the data from the datastore and inflate the object myself (I will know the datatype of the object in advance and so be able to deserialise it myself)

Thanks in advance,

D

 

 

 

    [Serializable]

    [Table(Name="PersistentClass")]

    public class PersistentClass : IConvertible

    {

        private object _owner;

        private string _typeName;       

 

        public PersistentClass()

        {

            _owner = null;

        }

 

        [Column(Name="TypeName", Storage="_typeName", Id=true)]

        public string TypeName

        {

            get { return _typeName; }

            set { _typeName = value; }

        }

 

        [Column(Name="Owner", Storage="_owner", DBType="IMAGE")]

        public object Owner

        {

            get { return _owner; }

            set { _owner = value; }

        }

 

 

 



 
 
Matt Warren





PostPosted: LINQ Project General, Persisting type 'object' Top

That's an interesting extensibility point. We'll consider it.

 
 
Terotech.Com Ltd





PostPosted: LINQ Project General, Persisting type 'object' Top

Hi Matt,

     even better would be the ability to persist generic types, that way I would be able to strongly type the return value to T. I really wanted to have a generic class, which i could then define as CustomProperty<T>, where T is a known interface, unfortunately I can't then seem to define a Table<CustomProperty<T>> in my datacontext derived class. i have however found the solution to the original problem, a sample is below;

 

        [Column(Name="PropertyValue", Storage="_propertyValue", DBType="IMAGE")]

        public object PropertyValue

        {

            get

            {

                if (_propertyValue != null)

                {

                    lock (_propertyValue)

                    {

                        byte[] buf = _propertyValue as byte[];

                        if (buf != null)

                        {

                            using (MemoryStream strm = new MemoryStream(buf))

                            {

                                BinaryFormatter fmt = new BinaryFormatter();

                                _propertyValue = fmt.Deserialize(strm);

                            }

                        }

                    }

                }

 

                // *** Return the Value ***

                return(_propertyValue);

            }

            set

            {

                // *** Has the value been changed ***

                if ((this._propertyValue != value))

                {

                    // *** Raise PropertyChanging ***

                    this.OnPropertyChanging("PropertyValue");

 

                    // *** Update the Storage value ***

                    this._propertyValue = value;

 

                    // *** Raise PropertyChanged ***

                    this.OnPropertyChanged("PropertyValue");

                }

            }

        }

[Updated]

I don't know why it didn't work for me before, but I think I've cracked it using the following:

 

    /// <summary>< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    /// The Interface that special types must implement

    /// </summary>

    public interface ISpecialType

    {

 

    }

 

    /// <summary>

    ///

    /// </summary>

    /// <typeparam name="T"></typeparam>

    [Table(Name="SpecialType")]

    public class SpecialType<T> where T : ISpecialType

    {

        private T _storage;

 

        /// <summary>

        ///

        /// </summary>

        public SpecialType()

        {

            _storage = default(T);

        }

 

        /// <summary>

        ///

        /// </summary>

        [System.Data.DLinq.Column(Name="CustomValue", Storage="_storage", DBType="IMAGE")]

        public T CustomValue

        {

            get{return(_storage);}

            set{_storage = value;}

        }

 

    }

 

then declaring the table as:

 

public System.Data.DLinq.Table<SpecialType<ISpecialType>> SpecialTypes;       

 

 

using a mixture of both the first and second samples I reckon I should now be able to do this.

Regards,

D