Editorials

Combine POCOs with Extension Methods

One of the things I dislike about POCOs (Plain Old CLR Objects) is the limited encapsulation available in Dot Net. POCO classes tend to be quite anemic, often implemented using properties only. That’s because the purpose of a POCO class is to transport data in object form. Including other behaviors makes the POCO more powerful, but increases the complexity for usage as a transport object.

Since I came into using Extension objects I have found that extension methods, based on a POCO class, can provide a more encapsulated, less anemic, look and feel for that POCO class, while keeping the base POCO class simple and easy to use in transport. In this case you get the best of both worlds. Extension methods are not a perfect solution because they are limited in capabilities. If you need other object oriented implementations such as events you will have to implement that using inheritance or composition in another class altogether.

Extension method work great when you want to extend the behavior of a class that you do not manage. Using that same principle, you can write extension methods against your POCO class. Let’s use a Person POCO as an example. A person has a number of properties that are combined in different ways to address that person. An object would contain the methods implementing the different ways of addressing a person. In our example, the POCO consists only of the named properties.

public class Person
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string Initial { get; set; }
    public string LastName { get; set; }
    public string Suffix { get; set; }
}

Then we create an additional static class that extends our Person POCO, so that it appears to have methods as a native implementation of the person class.

public static class PersonExtensions
{
    public static string CasualGreeting(this Person person)
    {
        return person.FirstName;
    }

    public static string StandardGreeting(this Person person)
    {
        return string.Format("{0}{1}",
            string.IsNullOrEmpty(person.Title) 
                   ? string.Empty 
                   : person.Title + " ",
            string.IsNullOrEmpty(person.LastName) 
                   ? string.Empty 
                   : person.LastName
            ).Trim();
    }

    public static string FullName(this Person person)
    {
        return  string.Format("{0}{1}{2}{3}{4}",
            string.IsNullOrEmpty(person.Title) 
                   ? string.Empty 
                   : person.Title + " ",
            string.IsNullOrEmpty(person.FirstName) 
                   ? string.Empty 
                   : person.FirstName + " ",
            string.IsNullOrEmpty(person.Initial) 
                   ? string.Empty 
                   : person.Initial + ". ",
            string.IsNullOrEmpty(person.LastName) 
                   ? string.Empty 
                   : person.LastName,
            string.IsNullOrEmpty(person.Suffix) 
                   ? string.Empty 
                   : ", " + person.Suffix
        ).Trim();
    }
}

Now we can create a new Person instance, and call the static methods as if they were already a part of the Person class.

public void Demo()
{
    var myself = new Person
    {
        Title = "Mr",
        FirstName = "Benjamin",
        Initial = "S",
        LastName = "Taylor",
        Suffix = "AAGG",
    };
    Console.WriteLine(myself.CasualGreeting());
    Console.WriteLine(myself.StandardGreeting());
    Console.WriteLine(myself.FullName());
}

Output:
Benjamin
Mr Taylor
Mr Benjamin S. Taylor, AAGG

As you can see, even though the class Person does not have any methods implemented in it directly, I can use extension methods to provide consistent functionality. Now I have the logic for formatting a person’s name in one place, and can use it as if it were already native capabilities of the Person class.

Cheers,

Ben