VEnhance errors with Nullable user-defined structure

4 posts, 0 answers
  1. Adam
    Adam avatar
    2 posts
    Member since:
    Nov 2008

    Posted 20 Mar 2009 Link to this post

    Hi,

        I have a user-defined structure for Time which wraps a DateTime field. It behaves properly i.e. it builds, reads and writes to the database. VEnhance throws an error when I declare a Nullable Time, which I would expect to work, can anyone guide me?

    C:\Dev\Products\Testbed\Deliverables\Domain\obj\Debug\MSW.MAPS.Testbed.Domain.dll(-1,-1): OpenAccess Enhancer error 0: Unsupported field-type 'System.Nullable`1[MSW.Time]' found for field 'aNullableTime' of class 'MSW.MAPS.Testbed.Domain.CountryExample'. (Unsupported value type 'System.Nullable`1[[MSW.Time, MSW.MAPS.Framework, Version=2.3.3366.15159, Culture=neutral, PublicKeyToken=null]]'. The type is not marked as [Persistent].)

    Here's the usage of Time, (deliberately trimmed down to highlight the point):

    using

     

    Telerik.OpenAccess;

     

     

     

     

    namespace

     

    MSW.MAPS.Testbed.Domain {

     

     

     

    [

    Persistent]

     

     

     

    public partial class CountryExample {  

     

     

     

     

    private Time someTime;  

     

    private Time? aNullableTime;

      

     

    internal CountryExample() { }

    [

    FieldAlias("someTime")]

     

     

    public Time SomeTime {

     

        get { return someTime; }

     

        set { someTime = value; }

    }

    [

    FieldAlias("aNullableTime")]

     

     

     

    public Time? ANullableTime {

     

     

        get { return aNullableTime; }

     

     

        set { aNullableTime = value; }

    }

    }

    }

     



    Here's the source code for Time:

    using

     

    System;

     

     

    using

     

    System.Collections.Generic;

     

     

    using

     

    System.Linq;

     

     

    using

     

    System.Text.RegularExpressions;

     

     

     

    using Telerik.OpenAccess;

     

     

     

     

    namespace

     

    MSW {

     

     


    [
    Serializable]

     

     

    [

    Persistent]

     

     

     

    public struct Time : IFormattable {

     

     

        private DateTime data; 
        
    public Time(int hour, int minute, int second) : this(new DateTime(1900, 1, 1, hour, minute, second)) {} 
        
    public Time(DateTime dateTime) {data = dateTime;}

     

     

        public int Hour { get { return Data.Hour; } } 

     

        public int Minute { get { return Data.Minute; } }

     

        public int Second { get { return Data.Second; } } 

     

        //Allows OpenAccess to compile - do not use, value-types should be immutable.

         

    internal DateTime Data {

     

     

            get { return data; }

     

            set { data = value; }

     

        }

     

     

     

        public override string ToString() {return Data.ToLongTimeString();}

     

     

        public string ToString(string format, IFormatProvider formatProvider) { return Data.ToString(format, formatProvider); } 

     

     

        public string ToXmlString() { return Data.ToString(Second != 0 ? "HH:mm:ss" : "HH:mm"); }

     

        
        public
    override bool Equals(object obj) { 
            
    if (obj == null) return false;

            

    if (this.GetType() != obj.GetType()) return false;

     

            

    return Data.Equals(((Time)obj).Data);

     

        }

     

     

        public override int GetHashCode() {  return Data.GetHashCode(); }

     

     

     

     

     

     

     

    }

    }



    TIA,
    Adam
  2. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 20 Mar 2009 Link to this post

    Hi Adam,
    Nullable structs are not supported. The struct fields are mapped to additional columns in the same table. We can not decide if the complete struct is null or only the containing fields. Please make the struct fields nullables instead.

    Kind regards,
    Jan Blessenohl
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. Peter
    Peter avatar
    23 posts
    Member since:
    Sep 2008

    Posted 15 Apr 2009 Link to this post

    Jan,

    We understand the point, but could you please consider allowing nullable user-defined structs when at least one of the underlying fields is not nullable (ie when you can decide whether it's the whole struct or just a field that's null)?  For us, it would be sufficient to allow nullable if all the fields were not nullable (and/or have metadata telling OpenAccess what to do).
    [It may just be my take on when to use structs, but I struggle to come up with scenarios where I'd use a struct with nullable fields I really can't think of a case where I'd use a struct with all nullable fields. (I guess string fields are the awkward case - I might not allow a null, but OpenAccess couldn't tell.)]

    What we're trying to do is implement a suite of standard value types for our domain that behave like the .NET framework's built-in ones.  We can work around this 'no nullable user-defined structs' limitation - we're currently using a pair of structs in each case: one 'ordinary' and one 'nullable' - but it's awkward and has a broad impact that I'd prefer to avoid.

    Also, these structs are strict value types (ie immutable).  So, we originally implemented them so that the fields were only set in the constructor (they should be marked as readonly, but I didn't expect that to work with OpenAccess).  However, VEnhance couldn't handle that code and was only happy when we added setter properties (deduced from the examples).  That's open to misuse, so could you please also look at supporting immutable user-defined structs (ie with nothing that changes the fields outside the constructor).

    Best regards
    Peter
  5. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 28 Apr 2009 Link to this post

    Hello Peter,
    I have made an example for you, the way the struct is stored is what we would do internally if we would support nullable structs.

    The additonal properties are only for linq support, if you use oql you do not need them.

    I have no idea how many nullable structs you have. If there are only a few this solution might work for you.

    Sincerely yours,
    Jan Blessenohl
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top