【发布时间】:2016-01-25 12:21:53
【问题描述】:
我目前正在编写一个 Fluent API(对这种模式来说非常新)。我不确定在最终执行之前确保通过先前的方法链接设置依赖数据的最佳做法是什么。
鉴于以下 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);
}
有没有办法确保“配置”方法在调用执行方法之前已经被链接。而不仅仅是验证 Execute 方法中的项目。
【问题讨论】:
-
这个related answer 除了您接受的答案之外也有帮助。