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

Using custom types

3 Answers 38 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dilshod
Top achievements
Rank 1
Dilshod asked on 03 Jun 2015, 07:08 AM

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,

3 Answers, 1 is accepted

Sort by
0
Yavor Slavchev
Telerik team
answered on 08 Jun 2015, 06:52 AM
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.
0
Dilshod
Top achievements
Rank 1
answered on 08 Jun 2015, 03:29 PM

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

0
Yavor Slavchev
Telerik team
answered on 11 Jun 2015, 02:58 PM
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.
Tags
Data Access Free Edition
Asked by
Dilshod
Top achievements
Rank 1
Answers by
Yavor Slavchev
Telerik team
Dilshod
Top achievements
Rank 1
Share this question
or