I’m working on an asp.net page and need to output html based on a numeric value.
The logic is thus:
If the ReferenceCount is greater than 13, then output a Div-1:
Html.RenderPartial("Tweet", Model.tweets.FirstOrDefault());
If the ReferenceCount is greater than 16, then output a Div-1 and a Div-2:
Html.RenderPartial("Tweet", Model.tweets.FirstOrDefault());
Html.RenderPartial("TShirt", Model.tweets.FirstOrDefault());
If the ReferenceCount is greater than 17, then output a Div-1 and a Div-2 and a Div-3:
Html.RenderPartial("Tweet", Model.tweets.FirstOrDefault());
Html.RenderPartial("TShirt", Model.tweets.FirstOrDefault());
Html.RenderPartial("Banner", Model.tweets.FirstOrDefault());
If the ReferenceCount is greater than 22, then output a Div-1 and a Div-2 and a Div-3 and a Div-4:
Html.RenderPartial("Tweet", Model.tweets.FirstOrDefault());
Html.RenderPartial("TShirt", Model.tweets.FirstOrDefault());
Html.RenderPartial("Banner", Model.tweets.FirstOrDefault());
Html.RenderPartial("Tweet", Model.tweets.FirstOrDefault());
etc for a total of 27 more evaluations ….
Does anyone have any good ideas how to structure this logic flow as efficiently as possible?
I’d rather not have so many if statements, and I considered a Switch statement, which would allow you to fall through various Case tests, but you don’t seem to be able to have expressions in the Case statements of Switches in C#.
Thanks,
Scott
If you really have this pattern, you should abstract it away into something like this:
This way, if something changes, you just modify
settings. Even better: you could load it from a settings file and modifying wouldn’t require changing the code at all.