get old / new value of changed entity member

3 posts, 0 answers
  1. tango
    tango avatar
    5 posts
    Member since:
    Oct 2014

    Posted 22 Aug 2015 Link to this post

    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. tango
    tango avatar
    5 posts
    Member since:
    Oct 2014

    Posted 22 Aug 2015 Link to this post

    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
  3. DevCraft banner
  4. Simeon Simeonov
    Admin
    Simeon Simeonov avatar
    24 posts

    Posted 26 Aug 2015 Link to this post

    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.
Back to Top