I often like to show programming tips around simple things. Over the years, I’ve found that it is easy to get Lazy and do cutting and pasting type coding, however for reliability and maintainability, it’s important to stick to the principle of less is more.
Below is just a simple pattern I often use when doing something that requires processing over a set of images (or anything) multiple times.
foreach (var rec in imageDetails) { const string imageTypeFull = "ImageTypeFull"; const string imageTypeThumb = "ImageTypeThumb"; foreach (var imageType in new List<string> {imageTypeFull, imageTypeThumb}) { var bytes = imageType == imageTypeFull ? db.DetailBytes.Where(a => a.Id == rec.Id).Select(b => b.DataBytes). FirstOrDefault() : db.DetailBytes.Where(a => a.Id == rec.Id).Select(b => b.DataBytes). FirstOrDefault();
…
Basically, all through the foreach loop, I check for which imageType I am and do the appropriate thing. Not a big hint, but something that is easy to forget to do.
HTH’s.






It’s tricky to say without seeing the full method but I would guess I would be injecting a dictionary of some sort into the method, keyed on image type and with values that contain delegates (probably lambdas if simple).
Jim, What would you suggest for doing this? Do you still think Strategy Pattern? Strategy just seems too complex to me for something so simple.
Even if the strategy pattern is overkill in this case, the above code isn’t ideal, as it breaks the open/closed principle.
I would totally agree as long as there is justification for adding the complexity of the strategy pattern. For a simple case of two cases with relatively simple work, I think Strategy Pattern is too big a hammer.
That said, I had not thought of it for this case but it is certainly worth considering. Thanks for pointing it out.
Better to use strategy pattern.