Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct usage of iterator blocks

Tags:

iterator

c#

.net

I was refactoring some code earlier and I came across an implementation of an iterator block I wasn't too sure about. In an integration layer of a system where the client is calling an extrernal API for some data I have a set of translators that take the data returned from the API and translate it into collections of business entities used in the logic layer. A common translator class will look like this:

// translate a collection of entities coming back from an extrernal source into business entities
public static IEnumerable<MyBusinessEnt> Translate(IEnumerable<My3rdPartyEnt> ents) {

    // for each 3rd party ent, create business ent and return collection
    return from ent in ents
           select new MyBusinessEnt {
               Id = ent.Id,
               Code = ent.Code
           };
}

Today I came across the following code. Again, it's a translator class, it's purpose is to translate the collection in the parameter into the method return type. However, this time it's an iterator block:

// same implementation of a translator but as an iterator block
public static IEnumerable<MyBusinessEnt> Translate(IEnumerable<My3rdPartyEnt> ents) {
    foreach(var ent in ents)
    {
        yield return new MyBusinessEnt {
            Id = ent.Id,
            Code = ent.Code
        };
    }
}

My question is: is this a valid use of an iterator block? I can't see the benefit of creating a translator class in this way. Could this result in some unexpected behaviour?

like image 756
james lewis Avatar asked Feb 10 '12 17:02

james lewis


3 Answers

Your two samples do pretty much exactly the same thing. The query version will be rewritten into a call to Select, and Select is written exactly like your second example; it iterates over each element in the source collection and yield-returns a transformed element.

This is a perfectly valid use of an iterator block, though of course it is no longer necessary to write your own iterator blocks like this because you can just use Select.

like image 108
Eric Lippert Avatar answered Nov 11 '22 11:11

Eric Lippert


Yes, that's valid. The foreach has the advantage of being debuggable,so I tend to prefer that design.

like image 43
eouw0o83hf Avatar answered Nov 11 '22 10:11

eouw0o83hf


The first example is not an iterator. It just creates and returns an IEnumerable<MyBusinessEnt>.

The second is an iterator and I don't see anything wrong with it. Each time the caller iterates over the return value of that method, the yield will return a new element.

like image 41
John Saunders Avatar answered Nov 11 '22 12:11

John Saunders