I am currently writing a Fluent API (pretty new to this pattern). I am unsure what is the best practice around making sure that dependant data is set via previous method chaining prior to the final execution.
Given the below API.
public class Processor : IFluentProcessor
{
private string _connectionName = String.Empty;
private string _whereClause = String.Empty;
public IFluentProcessor ConnectionName(string connectionName)
{
_connectionName = connectionName;
return this;
}
public IFluentProcessor FilterClause(string whereClause)
{
_whereClause = whereClause;
return this;
}
public bool Execute(out string errorMessage)
{
errorMessage = String.Empty;
try
{
//Ideally i would like to make sure that these variables have been set prior to this execution
var client = new dbContext(_connectionName);
var items = client.Where(_whereClause).Select(m => m).ToList();
foreach (var item in items)
{
//process the items here.
}
return true;
}
catch (Exception ex)
{
errorMessage = ex.Message;
return false;
}
}
}
public interface IFluentProcessor
{
IFluentWorker ConnectionName(string connectionName);
IFluentProcessor FilterClause(string whereClause);
bool Execute(out string errorMessage);
}
Is there a way of insuring that the 'configuration' methods have been previously chained prior to calling the execute method. Rather than just validating the items within the Execute method.