This is a migrated thread and some comments may be shown as answers.

Type Conversion

3 Answers 107 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mark Dawson
Top achievements
Rank 1
Mark Dawson asked on 19 Jan 2009, 08:17 PM
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.

3 Answers, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 20 Jan 2009, 04:26 PM
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.
0
Mark Dawson
Top achievements
Rank 1
answered on 20 Jan 2009, 04:52 PM
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.
0
Alexander
Telerik team
answered on 23 Jan 2009, 10:02 AM
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.
Tags
Development (API, general questions)
Asked by
Mark Dawson
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Mark Dawson
Top achievements
Rank 1
Share this question
or