次のシナリオを最適に処理する方法を見つけようとしています。
RequestContext
次のような外部サービスに依存するクラスを想定します。
public class RequestContext : IRequestContext
{
private readonly ServiceFactory<IWeatherService> _weatherService;
public RequestContext(ServiceFactory<IWeatherService> weatherService, UserLocation location, string query)
{
_weatherService = weatherService;
...
最終的にインスタンス化するクラスでは、どのような種類の依存関係が必要RequestContext
ですか?可能性はありますがServiceFactory<IWeatherService>
、それは正しくないようです。または、次のように作成することもできますIRequestContextFactory
。
public class RequestContextFactory : IRequestContextFactory
{
private readonly ServiceFactory<IWeatherService> _weatherService;
public RequestContextFactory(ServiceFactory<IWeatherService> weatherService)
{
_weatherService = weatherService;
}
public RequestContext Create(UserLocation location, string query)
{
return new RequestContext(_weatherService, location, query);
}
}
次に、IRequestContextFactory
スルーコンストラクターインジェクションを渡します。
これは良い方法のように思えますが、このアプローチの問題は、発見可能性を妨げると思うことです(開発者はファクトリについて知って実装する必要がありますが、実際には明らかではありません)。
私が見逃しているより良い/より発見可能な方法はありますか?