Connecting Tech Pros Worldwide Help | Site Map

IClonable deep vs shallow, best practise

 
LinkBack Thread Tools Search this Thread
  #1  
Old June 23rd, 2006, 06:15 AM
bonk
Guest
 
Posts: n/a
Default IClonable deep vs shallow, best practise

I have come across the need to distinguish between the creation of a
deep and a shallow copy and with great interest I have read this
article:

http://blogs.msdn.com/brada/archive/...03/125427.aspx

This artivle seems to hint that I should not use System.IClonable but
instead define my own interface(s) for cloning. Now since this article
is rather old and since they did not obsolete IClonable there might be
a new "best practise".

How do I implement Cloning of objects correctly if I want to explicitly
distinguish between deep and shallow copies? Should I

a) use System.IClonable for deep copies and override
object.MemberwiseClone for shallow copies ?

b) define my own IClonable, for example like this:
interface IClonable <T>
{
/// <summary>
/// creates a deep or shallow copy of the current object
/// </summary>
/// <param name="deep">
/// if <c>true</c> a deep copy should be returned,
/// wich means that the copied object and all
/// objects referenced by the object are copied recursively
/// if <c>false</c>a shallow copy should be retruned,
/// wich means that only the top level references
/// are copied
///</param>
/// <returns>a deep or a shallow copy of the current
object</returns>
T Clone (bool deep);
}

c) do something else I did not think of yet ?


  #2  
Old June 23rd, 2006, 02:05 PM
Claes Bergefall
Guest
 
Posts: n/a
Default Re: IClonable deep vs shallow, best practise

I don't know of any best practises, but personally I would go with a)

/claes

"bonk" <schwertfischtrombose@gmx.de> wrote in message
news:1151043575.503614.83780@p79g2000cwp.googlegro ups.com...[color=blue]
>I have come across the need to distinguish between the creation of a
> deep and a shallow copy and with great interest I have read this
> article:
>
> http://blogs.msdn.com/brada/archive/...03/125427.aspx
>
> This artivle seems to hint that I should not use System.IClonable but
> instead define my own interface(s) for cloning. Now since this article
> is rather old and since they did not obsolete IClonable there might be
> a new "best practise".
>
> How do I implement Cloning of objects correctly if I want to explicitly
> distinguish between deep and shallow copies? Should I
>
> a) use System.IClonable for deep copies and override
> object.MemberwiseClone for shallow copies ?
>
> b) define my own IClonable, for example like this:
> interface IClonable <T>
> {
> /// <summary>
> /// creates a deep or shallow copy of the current object
> /// </summary>
> /// <param name="deep">
> /// if <c>true</c> a deep copy should be returned,
> /// wich means that the copied object and all
> /// objects referenced by the object are copied recursively
> /// if <c>false</c>a shallow copy should be retruned,
> /// wich means that only the top level references
> /// are copied
> ///</param>
> /// <returns>a deep or a shallow copy of the current
> object</returns>
> T Clone (bool deep);
> }
>
> c) do something else I did not think of yet ?
>[/color]


  #3  
Old June 23rd, 2006, 02:25 PM
Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
Default Re: IClonable deep vs shallow, best practise

Bonk,

See inline:
[color=blue]
> a) use System.IClonable for deep copies and override
> object.MemberwiseClone for shallow copies ?[/color]

I would not recommend overriding MemberwiseClone. It's not going to get
you anything really. It's also very specific in what it does.
[color=blue]
> b) define my own IClonable, for example like this:
> interface IClonable <T>
> {
> /// <summary>
> /// creates a deep or shallow copy of the current object
> /// </summary>
> /// <param name="deep">
> /// if <c>true</c> a deep copy should be returned,
> /// wich means that the copied object and all
> /// objects referenced by the object are copied recursively
> /// if <c>false</c>a shallow copy should be retruned,
> /// wich means that only the top level references
> /// are copied
> ///</param>
> /// <returns>a deep or a shallow copy of the current
> object</returns>
> T Clone (bool deep);
> }[/color]

Making cloning interfaces generic isn't the best idea. Most frameworks
that would take advantage of this don't care what T is, they just need a
copy.
[color=blue]
> c) do something else I did not think of yet ?[/color]

I think that the advice in the article is still the best piece of advice
that there is for your specific need (to determine whether or not it can do
a deep clone or a shallow clone). Basically, define a IDeepClonable
interface, and then a IShallowClonable interface.

There is one problem though. If you have an object that has references
to other objects and you do a deep copy, what do you do when those other
objects don't support deep-copy semantics?

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.