AutoFac 2forDIを使用するアプリケーションを構築しています。静的なIoCHelper(Service Locator)の使用は避けるべきだと読んでいます。
IoCHelper.cs
public static class IoCHelper
{
private static AutofacDependencyResolver _resolver;
public static void InitializeWith(AutofacDependencyResolver resolver)
{
_resolver = resolver;
}
public static T Resolve<T>()
{
return _resolver.Resolve<T>();
}
}
前の質問への回答から、自動生成されたファクトリを使用して、UnitOfWorkでIoCHelperを使用する必要性を減らす方法を見つけました。この道を進み続けると、IoCHelperを完全に排除できるかどうか興味があります。
シナリオは次のとおりです。
構成実装のラッパーとして機能する静的Settingsクラスがあります。Settingsクラスは他のクラスの大部分への依存関係であるため、ラッパーを使用すると、アプリケーション全体に設定クラスを挿入する必要がなくなります。
Settings.cs
public static class Settings
{
public static IAppSettings AppSettings
{
get
{
return IoCHelper.Resolve<IAppSettings>();
}
}
}
public interface IAppSettings
{
string Setting1 { get; }
string Setting2 { get; }
}
public class AppSettings : IAppSettings
{
public string Setting1
{
get
{
return GetSettings().AppSettings["setting1"];
}
}
public string Setting2
{
get
{
return GetSettings().AppSettings["setting2"];
}
}
protected static IConfigurationSettings GetSettings()
{
return IoCHelper.Resolve<IConfigurationSettings>();
}
}
サービスロケーターを使用せずに、またすべてのクラスにAppSettingsを挿入する必要なしに、これを処理する方法はありますか?以下にリストされているのは、コンストラクターインジェクションの代わりにServiceLocatorに頼り続ける3つの領域です。
- AppSettings
- ロギング
- キャッシング