I have an MVC3 application that needs to sync view models with database models. I found myself writing way too much code to copy properties back and forth between the differ
You may consider using Object.MemberwiseClone
method.
The
MemberwiseClone
method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. (docs.microsoft)
The method is protected. It means that you have to implement the Clone
method in your classes. To make things even more fance you can add ICloneable
to your classes as well.
class MyClass: ICloneable
{
// all your code
MyClass Clone()
{
return (MyClass)this.MemberwiseClone();
}
}