Afraz Ali
Afraz Ali

Reputation: 2752

How to ensure the sequence of methods in fluent API?

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.

Upvotes: 3

Views: 2440

Answers (3)

Rahim Moghaddam
Rahim Moghaddam

Reputation: 1

1.Create Your Sequence In Your DatabaseContext

=> Enter The Code In OnModelCreating

builder
.HasSequence<int>("SequenceName", schema: "SequenceSchema")
.StartsAt(100)
.IncrementsBy(1)
.HasMax(int.MaxValue);

builder.Entity<TableName>()
.Property(a => a.PropertyName)
.HasDefaultValueSql("NEXT VALUE FOR SequenceSchema.SequenceName");

Upvotes: 0

CertifiedCrazy
CertifiedCrazy

Reputation: 935

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.

Upvotes: 0

Venkatesh Muniyandi
Venkatesh Muniyandi

Reputation: 5630

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
    }
}

Upvotes: 7

Related Questions