0

I'm currently creating a fluent interface and I'm not 100% sure what's the best way to implement optional branches. A little example:

// Normal service registration
.AddService(myService)
// Service registration with additional parameters
.AddService(myOtherService).WithParameter(ServiceParam.Timeout, 100)
.AddService(myThirdService)

Now, I'm not really sure which return types AddService() and WithParameter() should have. The base interface provides AddService which must returns something supporting WithParameter AND AddService. Would you recommend to have the following structure (pseudo-code):

interface FluentStart
{
    AddService
}

interface FluentServiceConfiguration
{
    WithParameter
}

and finally, AddService would return an interface of:

interface FluentStartORFluentServiceConfiguration
    : FluentStart
    , FluentServiceConfiguration

? Is there any other (better) way to do that?

D.R.
  • 20,268
  • 21
  • 102
  • 205

1 Answers1

0

You can use lambda expression:

interface FluentStart
{
    AddService( service );
    AddService( service, Action<FluentServiceConfiguration> config );
}

// Normal service registration
.AddService(myService)
// Service registration with additional parameters
.AddService(myOtherService, x => x.WithParameter(ServiceParam.Timeout, 100))
.AddService(myThirdService)
Ivo
  • 8,172
  • 5
  • 27
  • 42