Type Conversion

4 posts, 0 answers
  1. Mark Dawson
    Mark Dawson avatar
    19 posts
    Member since:
    Aug 2012

    Posted 19 Jan 2009 Link to this post

    Hi,

    I'm trying to convert a project from one of your compitors ORM products, into OpenAccess and have run aground with converting types. For example:
     
    [Telerik.OpenAccess.Persistent(IdentityField = "_uID")]   
    public class product  
    {  
        private int _uID;  
        private string _salePrice;  
     
        public int uID { ... }  
        public Currency SalePrice { ... }  
    }  
     
     

    The problem is that _salePrice is stored in the database as a string representing "Currency:float",  for example "GBP:99.99"

    I need a way of converting the string to the Currency object, which is not Persistent.


    Thanks,
    Mark.

  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 20 Jan 2009 Link to this post

    Hi Mark,

    You should make this conversion in the set and get methods of the SalePrice property. When creating the Currency object you can use something similar to the code below for extracting the name and the value from string:

    string[] separated = _salePrice.Split(':'); 
    float price = float.Parse(separated[1]); 

    Then you simply create and return a new Currency instance.
    To enable setting the value, override the ToString() method of the Currency class to return the appropriate string representation:

    public override string ToString() 
       return currency + ":" + value; 

    And in the set method just apply it to the _salePrice field:

    set { _salePrice = value.ToString(); } 

    Hope this helps.

    All the best,
    Alexander
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. Mark Dawson
    Mark Dawson avatar
    19 posts
    Member since:
    Aug 2012

    Posted 20 Jan 2009 Link to this post

    Hi,

    Sorry, I wish it was that easy.

    The Currency class does more than just keep track of the Currency ISO code and value amount.  It also does conversions, etc and can't easily be converted to a single float and a corresponding string.

    My private field is a string.  My public property is an instance of the Currency class, for example

    private string dbSalePrice;   // persisted to database  
     
     
    private Currency _price;  // marked as Transient.  
    public Currency SalePrice  
    {  
        get   
        {  
             if (_price == null)   
                    _price = new Currency(dbSalePrice);  // initalise using the database string  
     
             return _price  
        }  

    That part is easy enough, the problem then is that in my code, if I change Product.SalePrice.Amount, that change is not reflected back in the dbSalePrice private (and persistent) field.  At this stage I'm not caling the set part of SalePrice, I'm calling the set part of Amount within my Currency class.  Doing so doesn't allow the dbSalePrice to update.

    I think I can get around this by using IInstanceCallbacks to populating the dbSalePrice on PreStore().  I was just hoping there would be a more elegant way of marking it up so that OpenAccess would know it needs to update dbSalePrice automatically before it saves the record which is how my previous ORM product worked.

    Regards,
    Mark.
  5. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 23 Jan 2009 Link to this post

    Hello Mark Dawson,

    The problem is not directly related to OpenAccess. What I think is the best option here, is to modify your Currency class to implement the INotifyPropertyChanged interface so you will have an event raised for each change that was made. Then in the event handler you can manually update the dbSalePrice string. Hope this gives you some directions to think about.

    Regards,
    Alexander
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top