如果我使用 ServiceProvider 接口来解析我在 .NET Core 中的属性进行依赖注入会不会很糟糕
假设我有以下内容
private readonly IRecipeRepository _recipeRepository;
private readonly IMediaResource _resourceUpload;
private readonly IAWSMedia _awsMedia;
然后这样做
public RecipeService(IServiceProvider service)
{
_recipeRepository = service.GetService<IRecipeRepository>();
_resourceUpload = service.GetService<IMediaResource>();
_awsMedia = service.GetService<IAWSMedia>();
}
而不是这个
public RecipeService(IRecipeRepository recipeRepo, IMediaResource media, IAWSMedia awsMedia )
{
_recipeRepository = recipeRepo;
_resourceUpload = media;
_awsMedia = awsMedia ;
}
喵喵时光机
相关分类