ASP.net c# : yielding to the power of yield

Here is another thing that I have been learning with ASP.net and C#. For ages I have seen the yield syntax and wondered what it did. I never found a solid example until the weekend.

I wanted to make sure that #projecttime had good error handling in the code, and that it didn’t rely on the UI to do all the error checking. Now the UI to #projecttime will have some error checking to make things faster but when #projecttime really takes off and I have a need to call the code, but skipping past the UI – say in a REST style API call, then having the error checking at the low level is very important…don’t want a bunch of spammers sending all manner of data into the database! And yield came in very handy.

public override IEnumerable GetRuleViolations(Client entity)
{
if (String.IsNullOrEmpty(entity.Name))
yield return new RuleViolation("Please enter a the client's name.  Client names can only contain letters, numbers, hyphens and apostrophes.", "Name");

if(entity.Name.Length > 100)
yield return new RuleViolation("The client's name is too long. (max. length is 100 characters)", "Name");

if (entity.IsNew && !IsUniqueClientName(entity.Name, entity.AccountId))
yield return new RuleViolation("The client's name is not unique.", "Name");
else
{
if (!IsUniqueClientName(entity.Name, entity.Id, entity.AccountId))
yield return new RuleViolation("The client's name is not unique.", "Name");
}

if(entity.Address1.Length > 100)
yield return new RuleViolation("The first line of the address is too long.", "Address1");

if (entity.Address2.Length > 100)
yield return new RuleViolation("The second line of the address is too long.", "Address2");

if (entity.Address3.Length > 100)
yield return new RuleViolation("The third line of the address is too long.", "Address3");

if (entity.PostCode.Length > 10)
yield return new RuleViolation("The post code is too long.", "PostCode");

if (entity.Telephone.Length > 20)
yield return new RuleViolation("The telephone number is too long.", "Telephone");

if (!ProjectTime.Website.Tools.IsEmailAddress(entity.EmailAddress))
yield return new RuleViolation("The email address seems to be incorrect.", "EmailAddress");

if (entity.EmailAddress.Length > 55)
yield return new RuleViolation("The supplied email address is too long.", "EmailAddress");

yield break;
}

What I can now do with my code is call this method and get all the violations back in 1 go. The yield syntax basically stores all the possible return values in a return variable and sends them back at the end. Now yes, you could do this yourself, with a few extra lines of code – a local list object and just to a list.add(obj) – but I feel this is nicer and now that I understand it, easier to read. You are not getting bogged do with exta code that needs to be maintained if things change in your code base.

I then added an extra Tools class that sends me back a HTML formatted list of the errors. But you can do that one yourself.

2 thoughts on “ASP.net c# : yielding to the power of yield”

Leave a Reply

Your email address will not be published.