I used to revise the code, and I came across an iterator block implementation that I was not sure about. At the integration level of the system in which the client calls an external 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 units used at the logical level. The general class of translators 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, this is a translator class; it is intended to translate a collection into a parameter into a return type of a method. However, this time it is 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? Thus, I do not see the benefits of creating a translator class. Could this lead to unexpected behavior?
james lewis
source share