I am writing an object that must always have certain values. Most notably, it must always have a value for Name property.
public class User
{
public string Name { get; set; }
public User(string name)
{
Name = name;
}
}
Now, there are a couple of business rules that I need to implement in this class. One of which is that the Name property must be a unique name. So, I would think that the initializer for this object would look something this:
public User(string name, IQueryable<User> allUsers)
{
var matches = allUsers.Where(q => q.Name == name).ToList();
if(matches.Any())
{
// abort object initialization
}
Name = name;
}
But I’m not sure how I would abort the object initialization. In fact, is that even possible?
Is there a way to abort an object initialization (ie: set object to null) or is there a better way of accomplishing this?
Aborting initialization of an object is done by throwing an exception in the constructor, and is recommended to reject invalid input.
The business logic you wish to define in the constructor doesn’t fit there. Constructors should be lightweight, and instantiation only. Querying some data source is too expensive for a constructor. Because of this, you should use the factory pattern instead. With the factory pattern, a caller might expect there to be some heavy lifting involved with object creation.
You can see that the factory pattern fits better, and because it’s a method a caller might expect there to be some work going on by invoking it. Whereas with a constructor one would expect it to be lightweight.
If you wanted to go the constructor route, then you would want to try some other method of enforcing your business rules such as when the actual insert into the data source is attempted.