3

I want to create fluent interface for some of my classes that I am building as part of a framework. I have created the methods and I am able to successfully chain methods. Now I want to ensure that I can handle the improper sequence of method calls.

The thing I am doing is something like CreateWorkflow -> OpenConfiguration -> ChangeUserName In the above scenario it wouldn't make sense if ChangeUserName was called first because it is dependent on OpenConfiguration.

I am confused whether I am correct in creating a Fluent chain of methods for this scenario or not and how to make the sequence work. To me this scenario seems to be very suitable for creating a fluent API.

Afraz Ali
  • 2,672
  • 2
  • 27
  • 47
  • It seems that we can achieve what I want using interfaces as mentioned here http://stackoverflow.com/questions/17800706/fluent-interfaces-implementation but this seems like an overkill for me. Is there any other better solution for this problem? – Afraz Ali Jul 29 '14 at 15:31
  • You're going to have to clarify what "better" means. A few interfaces doesn't seem all that scary to work with. – 48klocs Jul 29 '14 at 15:37
  • If your goal is compile-time checking (which is a totally reasonable goal here), it seems to me that you're stuck either using different interfaces for the same core class or having each chained method return a different class entirely. The type system is what OO languages provide for enforcing these kinds of compile-time constraints. – joshtkling Jul 29 '14 at 17:09
  • @joshtkling Yes you are right, I am looking for a way for compile time checking, Ideally I want the user to have only valid options in intellisense. – Afraz Ali Jul 30 '14 at 01:49
  • @48klocs I am thinking of how many classes or interfaces I will end up with, I don't think they will be small in number if I take the route of using interfaces and classes. I was thinking maybe there are some best practices / approach for doing this? – Afraz Ali Jul 30 '14 at 01:51
  • @AfrazAli the approach you linked to is a your best bet if you want to go this route. If the number of classes worries you, reconsider your approach. – 48klocs Jul 30 '14 at 20:53

2 Answers2

7

Here is the sample code that enforces method chain in specific order. I've used the example from here and fixed a minor issue in the original code. Here is the running code in dotnet fiddler

public interface IName
{
    IAge WithName(string name);
}

public interface IAge
{
    IPersist WithAge(int age);
}

public interface IPersist
{
    void Save();
}

public class Person : IName, IAge, IPersist
{
    public string Name { get; private set; }
    public int Age { get; private set; }


    public IAge WithName(string name)
    {
        Name = name;
        return this;
    }

    public IPersist WithAge(int age)
    {
        Age = age;
        return this;
    }

    public void Save()
    {
        // save changes here
    }
}
Community
  • 1
  • 1
Venkatesh Muniyandi
  • 5,132
  • 2
  • 37
  • 40
0

The real key is if you require a specific sequence for a fluent API to work you API needs improvement. Maybe you should consider something a little different. If ChangeUserName needs OpenConfiguration the consumer of the API shouldn't care. Either internalize the dependency so the API becomes:

CreateWorkflow -> ChangeUserName

or if the consumer already has the Configuration Object you could use a Dependency Injection approach and make the API something like:

CreateWorkflow(IConfigurationManager) -> ChangeUserName

or

CreateWorkflow -> ChangeUserName(IConfigurationManager)

I show 2 approaches here as I am not sure what the scope of dependency is on your configuration class. By either internalizing the need or adding a required parameter onto the signature of one of the methods you should be able to eliminate the fixed sequence issue. Other than a clear "Start" and "Finish" to your API.

Hope this helps.

CertifiedCrazy
  • 935
  • 5
  • 14