How to write a generic update method

2 posts, 0 answers
  1. eraashishgupta
    eraashishgupta avatar
    19 posts
    Member since:
    Nov 2008

    Posted 08 Jun 2010 Link to this post

    Hi all,
    I want to write a generic update method to update any of my entity.
    I want to pass the updated entity to that function say user so i will pass the updated user object and now i want that updated object to be persisted in the database.
    So how to do this and remember the update method should be generic. In my situation i have million of similar updates so i dont want to write that code by hand , my create method looks like this , similar generic method iwant for update as well
    public void Create(T item) 
                using (var scope = ObjectScopeProvider1.GetNewObjectScope()) 

  2. Serge
    Serge avatar
    375 posts

    Posted 09 Jun 2010 Link to this post

    Hello eraashishgupta,

     There is no automatic way to persist your changes to the database using this approach. It seems to me that you are using the persistent objects as DTOs (data transfer objects). What you will have to do in the update method is retrieve the object you wish to change from the database and set it's properties with the new values you have in the disconnected object.

    As you have to perform different updates this code will be different for each entity. Here however you have a couple of options. You can create some kind of a reflector solution or consider using AutoMapper.

    Either way there is no easy implementation as this is a crucial point in every application. I suggest taking some time and effort to implement this thoroughly.

    I do hope this helps.

    Have a great day,
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
Back to Top