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

How to write a generic update method

1 Answer 92 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
eraashishgupta
Top achievements
Rank 1
eraashishgupta asked on 08 Jun 2010, 01:24 PM
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()) 
            { 
                scope.Transaction.Begin(); 
                scope.Add(item); 
                scope.Transaction.Commit(); 
            } 
        } 

1 Answer, 1 is accepted

Sort by
0
Serge
Telerik team
answered on 09 Jun 2010, 06:15 PM
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,
Serge
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.
Tags
Development (API, general questions)
Asked by
eraashishgupta
Top achievements
Rank 1
Answers by
Serge
Telerik team
Share this question
or