views:

49

answers:

3

I've 2 classes. One is a linq-to-sql class and the other is a seriazable data-dump class.

Examples:

Class TableNotes Id UserId NoteText ... functionality

[Serializable] Class NoteDump NoteText ... functionality


They both would share a common set of functionality. I decide it is best for me not to duplicate this functionality and thus add it to TableNotes only. I will use the NoteDump object only for dumping data.

The question is, I want to covert a NoteDump to a TableNotes. Which class should handle the conversion and why? I.e TableNotes could take a NoteDump or NoteDump could return a new object of type of TableNotes.. or I could use a conversion operator.

+1  A: 

Really, the choice is yours. I personally use the "FromXXXX" convention inside the class it will become. In this case, it would be a method on TableNotes:

static TableNotes FromNoteDump(NoteDump noteDump)
{
    // conversion
}

If you need them to convert back and forth (or you think there could be future classes with a similar structure), consider inheriting from a base class or interface.

drharris
@drharris What about using a constructor instead?
Curtis White
You could just as easily do this using a constructor, but I personally don't use that format. I feel a constructor overload couples the classes a little more tightly than using a separate method. In reality it doesn't, but it makes me feel like the intent of the conversion is expressed more coherently by using a more verbose way of doing it.
drharris
A: 

How about a third class who's job it is to hide the other classes?

  • You can have the third class do all the conversions between any of these classes.
  • If you use ONLY the third class in all your other code, by building up whatever interfaces you need to from the lower two classes into the third, you'll effectively abstract away the fact that there are multiple versions of the class from the rest of your code.
Willfulwizard
+1  A: 

LINQ implements AsQueryable and AsEnumerable on various classes.

You could implement AsNoteDump() on your TableNotes class...

NoteDump noteDump = new TableNotes().AsNoteDump();
roosteronacid
@roosteronacid Thanks for this idea.
Curtis White