関数を取るメソッド(読みやすくするために削除)があります:
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
}
以下に 2 つの呼び出し元の例を示します。1 つは範囲変数を使用します。
var container = CreateTestEntityContainer("UseCase12", x => x.InstallationStart.AddSeconds(1), true, false, 5);
そして、そうでないもの:
var container = CreateTestEntityContainer("UseCase10", x => DateTime.Now.AddDays(-1), false, false, 0);
それはハックのようです。多くの場合、消費者が不要な関数を使用する必要がない場合に、これを解決するより良い方法はありますか?