Coding Monkey
Coding Monkey

Reputation: 1000

How to verifying a Class has all properties configured before being used?

I'm wondering what the best approach is to verify that a class has all the required details before it can be used.

For example say I have a Book class and I want to pass in a set of properties about the book to the Book constructor.

Book book = new Book(bookProperties);

What I want to make sure that BookProperties is complete, i.e. has all the information.

Let's say in this example I have the following:

One way is that I could create a default constructor that only accepts all 3 items:

BookProperties bookProperties = new BookProperties("2001: A Space Odyssey",
                                                   "Arthur C. Clarke",
                                                   1968);

Now this is ok, if we only have three values, but say my class has 10 or more properties that need to be initialized by the user before the Book class can be created.

One thing I was thinking was having a method in the BookProperties called isValid. Then in the constructor of the Book class I would see if bookProperties.isValid and assert if the return is false.

Is this a good idea or am I going about this all wrong?

Upvotes: 1

Views: 205

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1063318

If it needs 10 values, then it isn't unheard of to pass 10 values into the constructor - but what you describe sounds more like an entity object. For convenience, it is common to just use the property setters. With C# 3.0 you can use initializer syntax to do this conveniently and clearly:

Book book = new Book {
    Title = "2001: A Space Odyssey",
    Author = "Arthur C. Clarke",
    PublishedYear = 1968
};

You can use a BookProperties object (as some kind of builder) - but in that case, why not make construction a method on the builder? Then the builder does the validation before creating a Book (presumably via an internal constructor) - and throws an exception if there is a problem. I'm assuming the use of BookProperties is (for example) to allow an immutable Book - but there is no need for the builder to be immutable:

var bookProperties = new BookProperties(); // builder?
bookProperties.Title = "2001: A Space Odyssey";
...
Book book = bookProperties.CreateBook();

Personally, I'd use just the Book approach (topmost above) in most cases - this approach plays well with common .NET serialization engines and binding frameworks (both of which like parameterless constructors and get/set properties), for example. If you want validation, then perhaps just add a Validate() method (or IsValue {get;} property) to Book. Or implement IDataErrorInfo.

Upvotes: 3

Henk Holterman
Henk Holterman

Reputation: 273419

If those properties are really essential they should be in the constructor. You can of course use overloads to omit properties that have an acceptable default.

10 or even 20 parameters may not be pretty but it is better than the alternatives.

Upvotes: 1

Related Questions