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

Non-persistent property refresh

1 Answer 57 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Paresh
Top achievements
Rank 1
Paresh asked on 06 Oct 2015, 11:01 PM

Hi,

dbcontext.Refresh doesn't refresh/reset non-persistent properties of persistent class, how this can be done?

Example: 

Class User

{

   public string PersistentProperty1 {get;set;}

   public string PersistentProperty2 {get;set;}

   public string Non-PersistentProperty {get;set;}​​

}

 

var userObject = new User();

userObject.PersistentProperty1 = "Test";

userObject.PersistentProperty2 = "Test2";

userObject.Non-PersistentProperty = "Test3";

now if I call dbcontext.Refresh(userObject, OverwriteChangesFromStore);

only persistent properties are getting reset, non-persistent property is still set to "Test3".

 

How can I load complete userObject from DB, so that non-persistent property is set to string.empty or null?

 

Thanks

1 Answer, 1 is accepted

Sort by
0
Simeon Simeonov
Telerik team
answered on 09 Oct 2015, 08:15 AM
Hi Paresh,

Thank you for contacting us. The behavior you are experiencing is expected. No data is stored for your non-persisted properties in the database and for this reason DataAccess cannot reset them to their initial state.

Exactly for such cases we have the IInstanceCallbacks interface. It allows you to plug your custom logic just after an object is fetched/refreshed from the database. Please consider the following example:
public partial class Car : IInstanceCallbacks
{
    public void PostLoad()
    {
        // Plug here logic you want to execute after the object is resolved and loaded in memory (fetched/refreshed)
        this.CarYear = null;
    }
 
    public void PreRemove(IObjectScope objectScope)
    {
        // Plug here logic that you want to execute before removing the object from the database
    }
 
    public void PreStore()
    {
        // Plug here logic that you want to execute before storing the object from the database
    }
}

In this example I have a Car entity and for it the CarYear is a non-persisted property. I have extended the Car class and Implemented the IInstanceCallbacks interface. What I need from this interface is the PostLoad method. It is called after the object is fetched/refreshed and allows me to initialize the transient properties. In the example above I am initializing the CarYear property to null.

I hope you find this information useful. If you have further questions, please don't hesitate to contact us.

Regards,
Simeon Simeonov
Telerik
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Tags
General Discussions
Asked by
Paresh
Top achievements
Rank 1
Answers by
Simeon Simeonov
Telerik team
Share this question
or