How to build a sequence in the method calls of a fluent interface
Posted
by
silverfighter
on Stack Overflow
See other posts from Stack Overflow
or by silverfighter
Published on 2011-02-24T15:20:26Z
Indexed on
2011/02/24
15:25 UTC
Read the original article
Hit count: 290
Hi I would like to build a fluent interface for creating am object some sort of factory or builder.
I understand that I have to "return this" in order to make the methods chainable.
public class CarBuilder
{
public CarBuilder()
{
car = new Car();
}
private Car car;
public CarBuilder AddEngine(IEngineBuilder engine)
{
car.Engine = engine.Engine();
return this;
}
public CarBuilder AddWheels(IWheelsBuilder wheels)
{
car.Wheels = wheels.Wheels();
return this;
}
public CarBuilder AddFrame(IFrameBuilder frame)
{
car.Frame = frame.Frame();
return this;
}
public Car BuildCar()
{
return car;
}
}
with this I could build a car like that:
Car c = builder.AddFrame(fBuilder).AddWheels(wBuilder).AddEngine(eBuilder).BuildCar();
But what I need is a special sequence or workflow: I can only build the wheels on top of the frame and when the wheels exist then I'll be able to build up the engine. So instead of offering every method of the car builder I want to be able to add only the frame to the builder and then only the wheels to the frame and then the engine on top of that...
And how would it be or what would be a good implementation if the EngineBuilder itself has a fluent api like eBuilder.Cylinders(12).WithPistons()....
Would it be possible to have something like this
Car c = builder.AddFrame(fBuilder).AddWheels(wBuilder).AddEngine(x=>x.WithCylinders(12).WithPistons()).BuildCar();
So in sum how to structure the flow of the fluent interface and how to nest fluent Interfaces?
© Stack Overflow or respective owner