A factory is a creation abstraction that reconstructs domain objects from raw input while keeping instantiation logic out of application services. It may be a separate class, a static domain method, or an ORM mapping. Separating this utility logic leaves the service simpler and makes the reconstruction code isolated and testable. section 7.2
Reconstructing a user
UserFactory.Create receives an object array, requires at least three elements, casts the identifier, email, and user type, then returns a User. UserController can therefore load data, call UserFactory.Create, coordinate the domain operation, and persist the result without knowing reconstruction details. section 7.2
Use a factory when:
- Raw database data must become a domain object. section 7.2
- Construction is complex enough to distract an application service but lacks direct domain significance. section 7.2
- Input shape requires explicit validation before instantiation. section 7.2
Listing 7.3. User factory
public class UserFactory
{
public static User Create(object[] data)
{
Precondition.Requires(data.Length >= 3);
int id = (int)data[0];
string email = (string)data[1];
UserType type = (UserType)data[2];
return new User(id, email, type);
}
}
Inspect the concrete UserFactory implementation, including input validation, casts, and object creation. section 7.2










