【发布时间】:2022-12-02 00:08:37
【问题描述】:
What is a better way pass a bunch of params to the factory. Not injecting them all to Class where factory is calling and passing them out? I have abstract class with three implementation. And for each implementation I have two different http services and one db service. I.e.
public class class1Implementation: someAbstractClass
{
private readonly IHttpOneFirstService _httpOneFirstService;
private readonly IHttpOneSecondService _httpOneSecondService;
private readonly IDbOneService _dbOneService;
}
public class class2Implementation: someAbstractClass
{
private readonly IHttpTwoFirstService _httpTwoFirstService;
private readonly IHttpTwoSecondService _httpTwoSecondService;
private readonly IDbTwoService _dbTwoService;
}
public class class3Implementation: someAbstractClass
{
private readonly IHttpThreeFirstService _httpThreeFirstService;
private readonly IHttpThreeSecondService _httpThreeSecondService;
private readonly IDbThreeService _dbThreeService;
}
public class CustomWorker : BackgroundService
{
private readonly IHttpOneFirstService _httpOneFirstService;
private readonly IHttpOneSecondService _httpOneSecondService;
private readonly IHttpTwoFirstService _httpTwoFirstService;
private readonly IHttpTwoSecondService _httpTwoSecondService;
private readonly IHttpThreeFirstService _httpThreeFirstService;
private readonly IHttpThreeSecondService _httpThreeSecondService;
private readonly IDbOneService _dbOneService;
private readonly IDbTwoeService _dbTwoService;
private readonly IDbThreeService _dbThreeService;
private someAbstractClass myFactory(int condition)
{
someAbstractClass impl = condition switch
{
0 => new class1Implementation(_httpOneFirstService, _httpOneSecondService, _dbOneService),
1 => new class2Implementation(_httpTwoFirstService, _httpTwoSecondService, _dbTwoService),
_ => new class3Implementation(_httpThreeFirstService, _httpThreeSecondService, _dbThreeService),*/
};
return impl;
}
}
【问题讨论】:
-
Have you considered using a dependency injection container? That might give you an easier way to create your objects.
标签: c#