我有一个采用函数的方法(为了便于阅读而剥离):
private TestEntityContainer CreateTestEntityContainer(string rootName,
Func<InstallationSummary, DateTime> forceInstallationTimeFunc,
bool forceInstallEnvironmentShouldMatch, bool freezeAllInstallations, int numberOfInstallationSummariesToCreate)
{
// Other code exists above here. Note that we use two variables, appServer and appWithGroup,
// created earlier in this method, here:
var mostRecentInstallationSummary = InstallationSummaryLogic.GetMostRecentByServerAppAndGroup(appServer, appWithGroup);
var forceInstallation = new ForceInstallation();
// This is where the func is invoked. We need other things, created above, for this to work.
forceInstallation.ForceInstallationTime = forceInstallationTimeFunc.Invoke(mostRecentInstallationSummary);
// Do more things with forceInstallation here
}
下面是两个示例调用者,一个使用范围变量:
var container = CreateTestEntityContainer("UseCase12", x => x.InstallationStart.AddSeconds(1), true, false, 5);
而一个没有:
var container = CreateTestEntityContainer("UseCase10", x => DateTime.Now.AddDays(-1), false, false, 0);
这似乎是一个黑客。有没有更好的方法来解决这个问题,消费者不需要使用在许多情况下不必要的函数?