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

get old / new value of changed entity member

2 Answers 233 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.
tango
Top achievements
Rank 1
tango asked on 22 Aug 2015, 07:57 AM

Hello everybody,

I was looking for some method / function by means of which i can get new values of a changed entity member,

as i already can get old values of changed members by doing this...

 

IEnumerable<string> modifiedFields = _context.GetMemberNames(_​entityName, Telerik.OpenAccess.ObjectState.Dirty);
foreach (var array in modifiedFields)
         MessageBox.Show("OLD = " + _context.GetOriginalValue<string>(_securityData, array) );

 

how to get new values ?

2 Answers, 1 is accepted

Sort by
0
tango
Top achievements
Rank 1
answered on 22 Aug 2015, 10:03 AM
GOT A WORK AROUND, EXPERTS PLEASE VERIFY !!!
 
GivenEntity entObj = new GivenEntity();
 
//FETCHED AND DID SOME CHANGES IN ENTITY HERE
 
//FETCHING CONTEXT CHANGES
Telerik.OpenAccess.ContextChanges contextChanges = _context.GetChanges();
 
//CHECK WHETHER ANY FIELDS UPDATED AGAINST GIVEN ENTITY
IList<GivenEntity> Updates = contextChanges.GetUpdates<GivenEntity>();​
 
//IF THERE ARE FIELDS WHICH WERE UPDATED THEN STEP IN AND IDENTIFY THOSE FIELDS
if (Updates.Count > 0)
{
    //FETCHING ALL FIELDS FROM ENTITY WHERE WERE MODIFIED
    IEnumerable<string> modifiedFields = _context.GetMemberNames(entObj, Telerik.OpenAccess.ObjectState.Dirty);
 
 
    //LOOPING THROUGH EACH MODIFIED FIELD TO GET ITS OLD AND NEW VALUE
    foreach (var array in modifiedFields)
    {
        //FETCHING ENTITY ROW
        var b = Updates.FirstOrDefault();
 
        //FETCHING ALL PROPERTIES / ATTRIBUTES AGAINST THAT ENTITY ROW
        var c = b.GetType().GetProperties();
 
        //CHECKING WHETHER MODIFIED FIELD EXIST IN ENTITY PROPERTIES / ATTRIBUTES OR NOT
        var d = c.Where(g => g.Name.ToLower().Equals(array.ToLower())).FirstOrDefault();
 
        //IF EXISTS THEN GETTING ITS CURRENT / NEW VALUE
        var newValue = d.GetValue(b, null);
 
        //HERE GETTING ITS ORIGINAL / OLD VALUE
        var oldValue = _context.GetOriginalValue<string>(_entObj, array);
 
        MessageBox.Show("OLD = " + oldValue  + " , NEW = " + newValue );
    }
}
 
EXPERTS PLEASE VERIFY !!! IF I AM DOING ANY THING WRONG HERE
0
Simeon Simeonov
Telerik team
answered on 26 Aug 2015, 02:59 PM
Hello tango,

Thank you for contacting us.

The short answer to your question is - yes, this is the right way to get the updated value and the original value for a given managed entity. I just wanted to point out some possible improvements for your code:

1/ In your code you get a random GivenEntity that is updated in the given context. If you have more then one updated entities in the context there is no guaranty that the first entity returned by .GetUpdates<GivenEntity>() will be the entity you want to work with (in your case entObj). For that reason I would suggest that you get the entity you want with something like this code:
var updatedEntObj = Updates.FirstOrDefault(e => e.Id == entObj.Id);

In the example code above you should use a property of the entity that you know will not be modified (like auto-increment Id).

2/ In order to get the properties of the GivenEntity type you use the following code:
var c = b.GetType().GetProperties();

Because you know the type of the entities with which you are working you can use the following code outside the loop:
var c = typeof(GivenEntity).GetProperties();

3/ When you get the original value of a given modified property you always cast the result to string. Because you are looping though all the modified properties of the given entity in the general case there is no guaranty that all of them will be of type string. For that reason I would suggest to get the original value as object like this:
var oldValue = context.GetOriginalValue<object>(entObj, array);
 
I hope you find this information helpful.

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
tango
Top achievements
Rank 1
Answers by
tango
Top achievements
Rank 1
Simeon Simeonov
Telerik team
Share this question
or