binary dreams

a world of 1s and 0s

How to use C# properties with SOLID class design principles

It wasn't so long ago I was using properties without any consideration to how they SHOULD be used. I'm going to show you how you can use them properly with the help of SOLID Class Design Principles.

It wasn't so long ago I was using C# properties without any consideration to how they SHOULD be used. I'm going to show you how you can use them properly with the help of SOLID Class Design Principles.

Case 1:

This is a typical class you might have written.

public class Person
{
public string Forename { get; set; }
public string Surname { get; set; }
public Person() {}
}

STOP! Using public setters here with the person attributes breaks the Single Responsibility Principle (SRP) because the class has more than one reason to change. The class breaks this with two reasons - Forename and Surname.

To fix this, make the setters of those properties private and create an Update method like so:

public class Person
{
public string Forename { get; private set; }
public string Surname { get; private set; }
public void Update(string forename, string surname)
{
Forename = forename;
Surname = surname;
}
}

Now the class has one reason to change rather than two.

In general an object should not have it's state changed unless it was changed by an action. Remember OO design is about data hiding, how an objects attribute changes is of no concern to its consumer. Only the object needs to know how it changes.

Case 2:

How would you check the object is valid? Check each property like this:

var person = new Person();
person.Update("forename", "surname");
if (person.Surname != null && person.Forename != null)
{
SaveToDatabase(person);
}

STOP! This breaks the Dependency Inversion Principle (DIP) - Depend on abstractions not on concretions. We need to create a validation method to abstract us from the attributes. The properties should not be checked outside of the class but from inside the class.

Lets rewrite it.

Create an interface

public interface IValidate
{
	bool IsValid();
}

Now change the Person class.

public class Person : IValidate
{
public string Forename { get; private set; }
public string Surname { get; private set; }
public void Update(string forename, string surname)
{
Forename = forename;
Surname = surname;
}
public bool IsValid()
{
return (Forename != null) && (Surname != null);
}
}

With the Person class rewritten you would use it like so.

var person = new Person();
person.Update("forename", "surname");
if (person.IsPersonValid())
{
SaveToDatabase(person);
}

Comments (5) -

  • Obdulia Boesiger

    1/21/2014 8:25:32 PM |

    Good work Smile cheers!

  • Leisha

    2/6/2014 11:02:56 AM |

    Useful stuff a joy to have found it

  • @realmandydee

    2/14/2014 3:33:35 AM |

    will you mind if I tweet your post?

    • Jon-Paul Flood

      2/14/2014 12:24:37 PM |

      please do.

  • Teddy

    4/2/2014 10:57:48 AM |

    I loved your article post. Fantastic.

Comments are closed