Using custom types

4 posts, 0 answers
  1. Dilshod
    Dilshod avatar
    23 posts
    Member since:
    Feb 2012

    Posted 03 Jun 2015 Link to this post

    Hi,

    In our framework we would like to use our custom types, but couldn't find any documentation on the web.

    Let me explain the usecase:

    For example I have a table Invoice with columns below:

    - Id (Guid)

    - Date (DateTime)

    - Details (collection property)

    - Total (string)

     

    When the class is generated I would like to change the CLR type of Total property from string to MyCustomType. MyCustomType overrides the ToString method and returns the appropriate value.

    How can I achieve this? Is my solution correct? Please correct me if I am wrong.

    Reason I want to use my custom type is I want to do some logic in it.

     

    Thanks,

  2. Yavor Slavchev
    Admin
    Yavor Slavchev avatar
    22 posts

    Posted 08 Jun 2015 Link to this post

    Hi,
    Thank you for using our services.

    If I understand you correctly, you want to use your custom type without storing in the database. If so, I could suggest you to add another property in your entity that will be of your custom type and will not be mapped to the database. This could be achieved by using transient properties in DataAccess. Look at this article for more information how to use these properties. The idea is that these properties do not have corresponding database representation..

    I hope this information is helpful.

    Regards,
    Yavor Slavchev
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. DevCraft banner
  4. Dilshod
    Dilshod avatar
    23 posts
    Member since:
    Feb 2012

    Posted 08 Jun 2015 in reply to Yavor Slavchev Link to this post

    Hi Yavor,

    Thanks for your response. I don't want to use transient properties. I would like my property to be saved to the database. For example I want to do logic for fractions. I would like it to be saved as string value to the database so that when it is loaded my logic in the class or struct will parse the string and use it.

    Is that possible? 

     

    Thanks,

    Dilshod

  5. Yavor Slavchev
    Admin
    Yavor Slavchev avatar
    22 posts

    Posted 11 Jun 2015 Link to this post

    Hello again,
    Thank you for getting back to us.

    In this case, one possible solution is to have one persistent property for the string representation of your object (fraction) and a property that holds the actual object but it is not persistent. Something like the code snippet below:
    public partial class MyEntity
    {
        // This will be persisted
        public string FractionString { get; set; }
    }
     
    public partial class MyEntity
    {
        // This won't be persisted, but will push the values to the persistent FractionString
        public FractionNumber Fraction
        {
            get
            {
                return FractionNumber.Parse(this.FractionString);
            }
            set
            {
                this.FractionString = value.ToString();
            }
         }
    }

    Using this approach you can use your strongly types non-persistent property in your application, but your data layer will persist any changes there using the persistent string one so your data will be safe in the database.
    I hope this will point you in the right direction.

    Regards,
    Yavor Slavchev
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Back to Top