Empty string or not!

15 October 2011

One of the most used methods of the .Net framework I think is String.IsNullOrEmpty(), often used like:

if (String.IsNullOrEmpty(value))

or:

if (!String.IsNullOrEmpty(value))

A few years ago someone notified me that the ‘!’ operator is not very noticeable and therefore its more clear to read it like this:

if (String.IsNullOrEmpty(value) == false)

Code should be self-documenting and readable as a story, because there is a good chance that other developers have to maintain it later! These days a .Net developer is blessed with extension methods. One of the great benefits of extension methods is that they can be applied to null-references without crashing your application! So with the string extensions below you can write the previous statements like:

if (value.IsNullOrEmpty())

or:

if (value.IsNotNullOrEmpty())
public static class StringExtensions
{
  public static bool IsNullOrEmpty(this string value)
  {
    return String.IsNullOrEmpty(value);
  }

  public static bool IsNotNullOrEmpty(this string value)
  {
    return String.IsNullOrEmpty(value) == false;
  }
}
Advertisements

DateTime extensions for holyday and businessday calculations

1 September 2011

Extension methods are a very powerful feature of .Net. Here is an example of DateTime extensions to determine the holydays of a certain year. It also provides an algorithm to add workingdays to a DateTime object. The Easter calculation method was found here.

public static class DateTimeExtensions
{
	/// <summary>
	/// Telt werkbare dagen op bij een <see cref="DateTime"/>.
	/// </summary>
	/// <param name="date">De datum.</param>
	/// <param name="days">Het aantal werkdagen.</param>
	/// <param name="extraHolidays">De extra niet werkbare dagen.</param>
	/// <returns>Een <see cref="DateTime"/> waarvan de waarde een som is van de opgegeven datum en het aantal werkbare dagen.</returns>
	/// <remarks>
	/// Standaard wordt al rekening gehouden met Nieuwjaarsdag, Pasen, Hemelvaart, Pinksteren en Kerst
	/// (<seealso cref="DateTimeExtensions.Holydays"/>). Andere niet werkbare dagen, zoals Koninginnedag en
	/// Bevrijdingsdag, kunnen hier opgegeven worden.
	/// </remarks>
	public static DateTime AddBusinessDays(this DateTime date, int days, IEnumerable<DateTime> extraHolidays = null)
	{
		int year = date.Year;
		IEnumerable<DateTime> holydays = extraHolidays == null ? Holydays(year) : Holydays(year).Union(extraHolidays);

		int direction = days < 0 ? -1 : 1;

		date = date.Date;
		while (days != 0)
		{
			date = date.AddDays(direction);
			if (year != date.Year)
			{
				year = date.Year;
				holydays = extraHolidays == null ? Holydays(year) : Holydays(year).Union(extraHolidays);
			}

			if (date.IsWeekend() == false && holydays.Contains(date) == false)
				days -= direction;
		}

		return date;
	}

	public static bool IsWeekend(this DateTime datetime)
	{
		return datetime.DayOfWeek == DayOfWeek.Saturday || datetime.DayOfWeek == DayOfWeek.Sunday;
	}

	public static bool IsNewYearsDay(this DateTime datetime)
	{
		return datetime.Date == NewYearsDay(datetime.Year);
	}

	// Eerste Paasdag (zondag)
	public static bool IsEaster(this DateTime datetime)
	{
		return datetime.Date == Easter(datetime.Year);
	}

	// Tweede Paasdag (maandag)
	public static bool IsEasterMonday(this DateTime datetime)
	{
		return datetime.Date == EasterMonday(datetime.Year);
	}

	// Hemelvaartsdag (donderdag)
	public static bool IsAscensionDay(this DateTime datetime)
	{
		return datetime.Date == AscensionDay(datetime.Year);
	}

	// Eerste Pinksterdag
	public static bool IsWhitSunday(this DateTime datetime)
	{
		return datetime.Date == WhitSunday(datetime.Year);
	}

	// Tweede Pinksterdag
	public static bool IsWhitMonday(this DateTime datetime)
	{
		return datetime.Date == WhitMonday(datetime.Year);
	}

	// Eerste Kerstdag
	public static bool IsChristmasDay(this DateTime datetime)
	{
		return datetime.Date == ChristmasDay(datetime.Year);
	}

	// Tweede Kerstdag
	public static bool IsBoxingDay(this DateTime datetime)
	{
		return datetime.Date == BoxingDay(datetime.Year);
	}

	public static DateTime NewYearsDay(int year)
	{
		return new DateTime(year, 1, 1);
	}

	public static DateTime Easter(int year)
	{
		// Gauss Calculation
		int month = 3;

		// Determine the Golden number:
		int goldenNumber = year % 19 + 1;

		// Determine the century number:
		int century = year / 100 + 1;

		// Correct for the years who are not leap years:
		int leapYearCorrection = (3 * century) / 4 - 12;

		// Mooncorrection:
		int moonCorrection = (8 * century + 5) / 25 - 5;

		// Find sunday:
		int sunday = (5 * year) / 4 - leapYearCorrection - 10;

		// Determine epact (age of moon on 1 januari of that year (follows a cycle of 19 years):
		int epact = (11 * goldenNumber + 20 + moonCorrection - leapYearCorrection) % 30;

		if (epact == 24) epact++;
		if ((epact == 25) && (goldenNumber > 11)) epact++;

		// Get the full moon:
		int fullMoon = 44 - epact;
		if (fullMoon < 21) { fullMoon = fullMoon + 30; }

		// Up to sunday:
		int day = (fullMoon + 7) - ((sunday + fullMoon) % 7);

		// Easterdate:
		if ( day > 31 )
		{
			day = day - 31;
			month = 4;
		}

		return new DateTime(year, month, day);
	}

	public static DateTime EasterMonday(int year)
	{
		return Easter(year).AddDays(1);
	}

	public static DateTime AscensionDay(int year)
	{
		return Easter(year).AddDays(39);
	}

	public static DateTime WhitSunday(int year)
	{
		return Easter(year).AddDays(49);
	}

	public static DateTime WhitMonday(int year)
	{
		return Easter(year).AddDays(50);
	}

	public static DateTime ChristmasDay(int year)
	{
		return new DateTime(year, 12, 25);
	}

	public static DateTime BoxingDay(int year)
	{
		return ChristmasDay(year).AddDays(1);
	}

	public static IEnumerable<DateTime> Holydays(int year)
	{
		return new DateTime[] {
			NewYearsDay(year),
			Easter(year),
			EasterMonday(year),
			AscensionDay(year),
			WhitSunday(year),
			WhitMonday(year),
			ChristmasDay(year),
			BoxingDay(year)
		};
	}
}

Web Service Software Factory: Beyond the Service Interface (part 1)

30 August 2011

The Web Service Software Factory still is one of my favorite .Net tools, so I was very pleased that is fully available as Visual Studio Tools extension, which makes the installation of it a piece of cake!

The overall architecture of services built using the Service Factory worked very well for the services I build in the last view years. I am a great fan of this figure illustrating the layers of a service:

IC406596

However the Web Service Software Factory helps you creating the service interface layer, you are on your own when it comes to the other two.

In this post I will explain my way of implementing the business and resource access layers.

Business Entities

Every project should start with implementing the domain model, because it represents the vocabulary and key concepts of the problem domain. The entities are POCO classes (Plain Old CLR Objects) and should only contain properties and associations with other entities. Entities should not implement business logic other than validation. So methods on entity classes are only used for advanced ways of getting and setting the properties and associations that cannot be done through the getters and setters alone.

It’s wise to include one or more class diagrams in your entity project to have a complete overview on what you are doing.

Business Logic

The Web Service Software Factory documentation talks about the Transaction Script or Business Action pattern to implement the entry point into the Business Layer when called from higher layers. Just doing this can result in huge monolithic classes with a lot of methods. To prevent this I use Business Action interfaces in my projects with just one method:

public interface IBusinessAction
{
	Response Execute(Request request);
}

Where Request and Response can be business entities, for example:

public interface ICustomerSalesOrdersSelectAction
{
	ICollection<SalesOrder> Execute(Customer customer);
}

Sometimes a request can’t be just one business entity, or string, or integer (an ID for example). In that case a specific request can be added to the business logic. Not to the business entities, because it is just a query-like request, for example:

public class CustomerSalesOrdersPagedRequest
{
	public Customer Customer { get; set; }
	public int Skip { get; set; }
	public int Take { get; set; }
}

The Business Action interfaces can be implemented directly by classes from the Data Access Logic (repositories) or Service Agents. The usage of interfaces gives us the option of resolving their implementation through a dependency container (as Unity) from within the Service Interface Layer! This way the Business Logic doesn’t need to have references to Resource Access Layer components, unlike what is written in the Web Service Software Factory documentation.

To be continued…


Solving the “XmlSerializer and ‘not expected’ Inherited Types” problem without implementing IXmlSerializable

2 October 2008

Last week I ran into the following exception when serializing an object into xml:

System.InvalidOperationException: There was an error generating the XML document. —>  System.InvalidOperationException: The type <Type> was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

The business object I was trying to serialize had a similar construction like these classes:

public class FooParent
{
	public Foo MyFoo { get; set; }
	public List<Foo> MyFoos { get; set; }
}

public class Foo
{
	public int X { get; set; }
	public int Y { get; set; }
}

However the Foo members of the FooParent where not set with Foo objects, but with SqlFoo objects derived from Foo and with a class definition in a different assembly:

public class SqlFoo : Foo
{
	public SqlFoo() : this(0) {}

	public SqlFoo(int id)
	{
		this.ID = id;
	}

	public int ID { get; set; }
}

So, the next test example will throw the InvalidOperationException:

FooParent target = new FooParent();
target.MyFoo = new SqlFoo(1);
target.MyFoos = new List<Foo>(new SqlFoo[] { new SqlFoo(2) });

XmlSerializer serializer = new XmlSerializer(typeof(FooParent));
StringWriter writer = new StringWriter();
serializer.Serialize(writer, target);

After consulting my dear friend Dr. Google, I found a nice solution from Simon Hewitt. I really liked the usage of the implicit casting operators! But I didn’t like implementing the IXmlSerializable interface. There is too much thinking involved about XmlAttributeOverrides and XmlReader, and I wishes there would be a more intuitively solution.

Luckily I also had an Epiphany of my own!  I tried this Foo wrapper, which continues the idea of using the casting operators:

public class XmlFooWrapper
{
	private Foo model;

	public XmlFooWrapper() : this(new Foo()) {}

	public XmlFooWrapper(Foo model)
	{
		this.model = model;
	}

	public static implicit operator Foo(XmlFooWrapper wrapper)
	{
		return wrapper != null ? wrapper.model : null;
	}

	public static implicit operator XmlFooWrapper(Foo model)
	{
		return model!= null ? new XmlFooWrapper(model) : null;
	}

	public int X
	{
		get { return model.X; }
		set { model.X = value; }
	}

	public int Y
	{
		get { return model.Y; }
		set { model.Y = value; }
	}
}

The FooParent was altered by adding the [XmlElement(Type = typeof(XmlFooWrapper))] attribute to the MyFoo member. The List<> member was decorated with the [XmlArrayItem(ElementName=“Foo”, Type=typeof(XmlFooWrapper))] attribute:

public class FooParent
{
	[XmlElement(Type = typeof(XmlFooWrapper))]
	public Foo MyFoo { get; set; }

	[XmlArrayItem(ElementName = "Foo", Type = typeof(XmlFooWrapper))]
	public List<Foo> MyFoos { get; set; }
}

It worked! Running the test example again results in the following xml:

<FooParent>
  <MyFoo>
    <X>0</X>
    <Y>0</Y>
  </MyFoo>
  <MyFoos>
    <Foo>
      <X>0</X>
      <Y>0</Y>
    </Foo>
  </MyFoos>
</FooParent>

“Elfproef” algorithm in C# for the Dutch “Burgerservicenummer”

25 September 2008

In the Netherlands everyone has a unique personal number called “burgerservicenummer“.

It’s a 9 digit number that satisfies to an alternative on the so-called elfproef: if the burgerservicenummer is represented by ABCDEFGHI, then 9*A + 8*B + 7*C + 6*D + 5*E + 4*F + 3*G + 2*H + (-1*I) must be a multiple of 11.

The following is a fast integer-based algorithm in C# to validate a burgerservicenummer:

public static bool ValidateElfProef(int value)
{
	int divisor = 1000000000;
	int total = 0;
	int result = value;
	for (int i = 9; i > 1; i--)
		total += i * Math.DivRem(result, divisor /= 10, out result);

	int rest;
	Math.DivRem(total, 11, out rest);

	return result == rest;
}

public static bool ValidateElfProef(string value)
{
	return ValidateElfProef(Convert.ToInt32(value));
}

.NET style random generator with a normal or Gaussian distribution in C#

31 March 2008

Recently I was looking for a .NET style random generator with a normal or Gaussian distribution. I found a nice example on Koders.com, which usses the Box-Muller transformation, and build the algorithm into a class derived from System.Random. A mean of 0.5 and standard deviation of 1/2pi is used to keep the values returned by Sample() between 0 and 1. Values falling outside the range [ 0.0, 1.0 ) are replace by the next sample. It might not be perfect for scientific use, but it’s good enough to suit my needs.

public class RandomNormal : Random
{
	private double cache;
	private bool isCacheFilled = false;

	const double mean = 0.5;
	const double standardDeviation = 0.5 / Math.PI;

	private double Trunc(double value)
	{
		if (value < 0.0 || value >= 1.0) return this.Sample();
		return value;
	}

	protected override double Sample()
	{
		if (isCacheFilled)
		{
			isCacheFilled = false;
			return cache;
		}

		double r = 0.0;
		double x = 0.0;
		double y = 0.0;

		do
		{
			x = 2.0 * base.Sample() - 1.0;
			y = 2.0 * base.Sample() - 1.0;
			r = x * x + y * y;
		}
		while (r >= 1.0 || r == 0.0);

		double z = Math.Sqrt(-2.0 * Math.Log(r) / r);

		cache = Trunc(mean + standardDeviation * x * z);
		isCacheFilled = true;

		return Trunc(mean + standardDeviation * y * z);
	}
}