ベスト プラクティスは、プロジェクトに IoC を含め、構成オブジェクトをコントローラーに挿入することだと思います。
コントローラのコード例:
public class YourController : Controller
{
private IConfigService _configService;
//inject your configuration object here.
public YourController(IConfigService configService){
// A guard clause to ensure we have a config object or there is something wrong
if (configService == null){
throw new ArgumentNullException("configService");
}
_configService = configService;
}
}
この構成オブジェクトのシングルトン スコープを指定するように IoC を構成できます。このパターンをすべてのコントローラーに適用する必要がある場合は、基本コントローラー クラスを作成してコードを再利用できます。
あなたの IConfigService
public interface IConfigService
{
string ConfiguredPlan{ get; }
}
あなたの ConfigService:
public class ConfigService : IConfigService
{
private string _ConfiguredPlan = null;
public string ConfiguredPlan
{
get
{
if (_ConfiguredPlan == null){
//load configured plan from DB
}
return _ConfiguredPlan;
}
}
}
- このクラスは簡単に拡張して、接続文字列、デフォルト タイムアウトなどの構成をさらに含めることができます。
- コントローラー クラスにインターフェイスを渡します。単体テスト中にこのオブジェクトを簡単にモックできます。