This method really shouldn't be generic, switching on typeof is generally a clue here. While generics are nice and this does allow the caller to know what object they'll get back, this is essentially a method that takes
object and switches, which is not good. Can you think of a solution that better uses Polymorphism?
The easiest thing here would be to dump the generics and just use overloads, moving any shared code into a private method. But, you can't then call default(T) on all the other types when you use overloads, you will need to decide what objects these method applies to. I like overloading but this isn't the solution I would use.
public WebFolder GetContentByNodeIdSync(WebFolder folder, Guid nodeId)
public Image GetContentByNodeIdSync(Image image, Guid nodeId)
However, I think the real problem here is that we're violating the Single Responsibility Principal. Chances are the Objects
Image etc should encapsulate the logic for their respective
GetContentByNodeIdSync, rather than lumping it together here. If it's not your class then inherit. Implement some interface with this method.
//Much better. SOLID, you might say.
public interface MyInterface
public void/MyInterface GetContentByNodeIdSync(Guid nodeId);
MyInterface foo = new WebFolder();
var maybeNoOutput = foo.GetContentByNodeIdSync();
//Important question, are we creating more objects or just mutating this one?
//(is it really a 'get'?, to me it looks like we're populating our object with the dto info)
While I''m banging on about the SRP what is this mapper helper class doing anyway? Maybe it would be better to put User-Defined Conversions in the classes themselves to cast them from their Data Transfer Objects and vice versa.
static public explicit operator WebFolder(WebFolderDTO folder)
return Convert From DTO
static public explicit operator WebFolderDTO (WebFolder folder)
return Convert To DTO
var folder = new WebFolder();
folder.someProperty = "foo";
var folderDTO = (WebFolderDTO)folder;
var recoveredFolder = (WebFolder)folderDTO;
Assert.IsTrue(recoveredFolder.someProperty == "foo")