Using 'new' in a projection?

Posted by davenewza on Programmers See other posts from Programmers or by davenewza
Published on 2012-11-09T11:22:11Z Indexed on 2012/11/09 17:20 UTC
Read the original article Hit count: 238

Filed under:
|
|
|
|

I wish to project a collection from one type (Something) to another type (SomethingElse). Yes, this is a very open-eneded question, but which of the two options below do you prefer?

Creating a new instance using new:

var result = query.Select(something => new SomethingElse(something));

Using a factory:

var result = query.Select(something => SomethingElse.FromSomething(something));

When I think of a projection, I generally think of it as a conversion. Using new gives me this idea that I'm creating new objects during a conversion, which doesn't feel right. Semantically, SomethingElse.FromSomething() most definitely fits better. Although, the second option does require addition code to setup a factory, which could become unnecessarily compulsive.

© Programmers or respective owner

Related posts about c#

Related posts about design