Tim Van Wassenhove home

Most people add an implicit conversion to their builder API which gives them the advantage that they don’t have to call Build explicitely. I have decided that i do not want to have this implicit conversion for a couple of reasons:

Here is an example to clarify that last reason: Consider an OrderBuilder which requires the user to provide a product and then a quantity:

interface ITakeProduct { ITakeQuantity WithProduct(int productId); }
interface ITakeQuantity { IBuildProduct WithQuantity(int quantity); }
interface IBuildProduct { Product Build(); }

With the existence of an implicit conversion operator a developer can create an invalid order as following:

Order order = new OrderBuilder();

Conclusion: I am not convinced that Fluent Builders should support implicit conversion.