views:

277

answers:

4

I've seen two different implementation of memento on .NET.

One is pretty straightforward - The object creates another instance of itself.

The other is serializing the object using BinaryFormatter and MemoryStream.

Which is the preferred method? Can anyone point out advantages/disadvantages of each approach?

A: 

if you are going to persist the memento, use the serialization method

otherwise, a cloned object is fine

Steven A. Lowe
+1  A: 

I think that the choice of how to create/store the memento depends on how long you want the memento to persist and whether you need to communicate that memento across appdomains. If the memento exists only for a short time and is only used by the same thread, then a cloned object is reasonable. If the memento needs to be persisted or passed off to another appdomain, then serialization would be preferred. If the memento is long lived you may even want to serialize it and store it externally (in a file or DB).

tvanfosson
A: 

Call me crazy and inefficient but I do mine to a StringBuilder and from a string.

Quibblesome
A: 

By the way, I am interested in looking at it from resource usage/developer productivity perspective. I apologize for not stating that first.

Assuming that the memento does not need to be persisted, which is preferred?

From a developer productivity point of view, serialization wins hands down. A few lines that are generic for any object is more efficient than having to manually create a clone that involves possibly private constructors, field assignments, etc.

But then again, perhaps serialization is heavy - I'm not certain.

Jiho Han