C# Fluent API - 如何确保相关数据正确链接
C# Fluent API - How to insure dependant data is chained correctly
我目前正在写一个 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);
}
有没有一种方法可以确保 'configuration' 方法在调用 execute 方法之前已预先链接。而不仅仅是验证 Execute 方法中的项目。
为了严格排序,可以拆分接口(原谅我命名时缺乏灵感,你懂的):
public interface IFluentProcessor1
{
IFluentProcessor2 ConnectionName(string connectionName);
}
public interface IFluentProcessor2
{
IFluentProcessor3 FilterClause(string whereClause);
}
public interface IFluentProcessor3
{
bool Execute(out string errorMessage);
}
将其与私有构造函数 + 工厂方法相结合以实例化流畅的接口:
public class Processor : IFluentProcessor1, IFluentProcessor2, IFluentProcessor3
{
private string _connectionName = String.Empty;
private string _whereClause = String.Empty;
private Processor() {}
public static IFluentProcessor1 Create() { return new Processor(); }
// ...
}
这确实修复了调用方法的顺序,它不允许将调用切换到 FilterClause
和 ConnectionName
:
string errorMessage;
var result = Processor.Create()
.ConnectionName("my connection")
.FilterClause("my filter")
.Execute(out errorMessage);
我目前正在写一个 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);
}
有没有一种方法可以确保 'configuration' 方法在调用 execute 方法之前已预先链接。而不仅仅是验证 Execute 方法中的项目。
为了严格排序,可以拆分接口(原谅我命名时缺乏灵感,你懂的):
public interface IFluentProcessor1
{
IFluentProcessor2 ConnectionName(string connectionName);
}
public interface IFluentProcessor2
{
IFluentProcessor3 FilterClause(string whereClause);
}
public interface IFluentProcessor3
{
bool Execute(out string errorMessage);
}
将其与私有构造函数 + 工厂方法相结合以实例化流畅的接口:
public class Processor : IFluentProcessor1, IFluentProcessor2, IFluentProcessor3
{
private string _connectionName = String.Empty;
private string _whereClause = String.Empty;
private Processor() {}
public static IFluentProcessor1 Create() { return new Processor(); }
// ...
}
这确实修复了调用方法的顺序,它不允许将调用切换到 FilterClause
和 ConnectionName
:
string errorMessage;
var result = Processor.Create()
.ConnectionName("my connection")
.FilterClause("my filter")
.Execute(out errorMessage);