« 4 things about domain objects

June 13, 2007 • ☕️ 1 min read

First, please do not mock a domain object. Stub it. Thanks.

Second, do not write an interface for it. Neither if you want a Null Object implementation.

Third do not be obsessed with Null Objects, sometimes they are good, not always. If you end with a

if objectInstanceType == ObjectType.NULL in your code perhaps something is wrong in your design, a null object is to avoid that stuff and to give a “null behaviour”.

Forth. Do not use the ActiveRecord pattern, seriously, you’re gonna couple the infrastructure layer with the domain layer. It’s not a pattern IMHO, kind of antiproton, try considering a repository.

Fifth. For the point one maybe you will like a builder pattern in order to stub objects. Here an example:

class PersonBuilder
{
private string name = “testname”;
private int age;
public PersonBuilder Name(string value)
{
name = value;
return this;
}
}
public PersonBuilder Age(int value)
{
age = value;
return this;
}
public Person ToPerson()
{
return new Person(name, age);
}

Syntax then is nice, I really like it.

Person expected = new PersonBuilder().Age(29).Name(“antonio”).ToPerson();

and it encourages to write not mutable object like this Person:

class Person
{
private readonly int age;
private readonly string name;
public Person(string name, int age){…}
public int age
{
get { return age; }
}
}

Looks like I’ve lost my code formatter… :-(