从新文件实现依赖注入
Implementing Dependency Injection from a new file
我一直在研究 ASP.NET Core 5 MVC 项目,它按预期工作,我现在遇到的唯一问题是 Startup.cs 文件的大小,我我经常使用 Microsoft.Extensions.DependencyInjection,它非常好!但正如我所提到的,它变得非常拥挤,那些“services.AddTransient、Scoped 或 Singleton”,有没有办法创建我自己的 class 添加这些服务并从 Startup.cs?.
调用它
到目前为止,我一直在尝试使用“注入”方法制作一个静态 class,该方法将 return 一个 IServiceCollection,但它不起作用,我一直在搜索 google 举一些例子,但看起来这不是一个“东西”。
让我分享一些示例代码:
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using Models;
namespace MyFirstAzureWebApp
{
public class Injections
{
private static readonly IServiceCollection _services;
public static IServiceCollection Inject()
{
_services.AddTransient<IValidator<Customer>, CustomerValidator>();
_services.AddTransient<IValidator<Requirement, RequirementValidator>();
_services.AddApplicationInsightsTelemetry(Configuration["APPINSIGHTS_CONNECTIONSTRING"]);
_services
.AddFluentEmail("noreply@myownmail.com")
.AddRazorRenderer()
.AddSmtpSender("smtp.myownmail.com",445);
return _services;
}
}
}
并且在 Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews()
.AddFluentValidation(opt =>
{
opt.DisableDataAnnotationsValidation = true;
opt.ValidatorOptions.LanguageManager.Culture = new CultureInfo("es");
});
//Dependency Injetions call
services = Injections.Inject();
}
我希望这些信息足以解决我的问题。
非常感谢!
这是很常见的事情(因为是的 - 它确实很拥挤!)。
一种方法是在 IServiceCollection 上编写扩展方法,将功能位组合在一起。
例如,您可以创建一个名为 DatabaseServices.cs
的文件,其中添加 entity framework 或 Dapper 或其他内容。
// DatabaseServices.cs
public static class DatabaseServices
{
public static IServiceCollection AddDatabases(this IServiceCollection services)
{
// Set up Entity Framework
services.AddDbContext<MyContext>(/* configure EF */);
// Do other stuff related to databases.
// Return the service collection to allow chaining of calls.
return services
}
}
然后在 Startup.cs
你可以这样做:
// Startup.cs
services.AddDatabases();
创建其他文件以添加日志记录、配置、服务、HTTP 客户端等
是的,你绝对可以做到。我一直使用它来保持我的代码干净整洁。您可以使用扩展方法轻松做到这一点:
public class Injections
{
public static IServiceCollection RegisterServices(this IServiceCollection services) => services
.AddTransient<IValidator<Customer>, CustomerValidator>()
.AddTransient<IValidator<Requirement, RequirementValidator>();
public static IServiceCollection AddOtherServices(this IServiceCollection services, IConfiguration configuration) => services
.AddApplicationInsightsTelemetry(configuration["APPINSIGHTS_CONNECTIONSTRING"])
.AddFluentEmail("noreply@myownmail.com")
.AddRazorRenderer()
.AddSmtpSender("smtp.myownmail.com",445);
}
然后在你的Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews()
.AddFluentValidation(opt =>
{
opt.DisableDataAnnotationsValidation = true;
opt.ValidatorOptions.LanguageManager.Culture = new CultureInfo("es");
});
//Dependency Injetions call
services.RegisterServices();
services.AddOtherServices(Configuration);
}
我一直在研究 ASP.NET Core 5 MVC 项目,它按预期工作,我现在遇到的唯一问题是 Startup.cs 文件的大小,我我经常使用 Microsoft.Extensions.DependencyInjection,它非常好!但正如我所提到的,它变得非常拥挤,那些“services.AddTransient、Scoped 或 Singleton”,有没有办法创建我自己的 class 添加这些服务并从 Startup.cs?.
调用它到目前为止,我一直在尝试使用“注入”方法制作一个静态 class,该方法将 return 一个 IServiceCollection,但它不起作用,我一直在搜索 google 举一些例子,但看起来这不是一个“东西”。
让我分享一些示例代码:
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using Models;
namespace MyFirstAzureWebApp
{
public class Injections
{
private static readonly IServiceCollection _services;
public static IServiceCollection Inject()
{
_services.AddTransient<IValidator<Customer>, CustomerValidator>();
_services.AddTransient<IValidator<Requirement, RequirementValidator>();
_services.AddApplicationInsightsTelemetry(Configuration["APPINSIGHTS_CONNECTIONSTRING"]);
_services
.AddFluentEmail("noreply@myownmail.com")
.AddRazorRenderer()
.AddSmtpSender("smtp.myownmail.com",445);
return _services;
}
}
}
并且在 Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews()
.AddFluentValidation(opt =>
{
opt.DisableDataAnnotationsValidation = true;
opt.ValidatorOptions.LanguageManager.Culture = new CultureInfo("es");
});
//Dependency Injetions call
services = Injections.Inject();
}
我希望这些信息足以解决我的问题。
非常感谢!
这是很常见的事情(因为是的 - 它确实很拥挤!)。
一种方法是在 IServiceCollection 上编写扩展方法,将功能位组合在一起。
例如,您可以创建一个名为 DatabaseServices.cs
的文件,其中添加 entity framework 或 Dapper 或其他内容。
// DatabaseServices.cs
public static class DatabaseServices
{
public static IServiceCollection AddDatabases(this IServiceCollection services)
{
// Set up Entity Framework
services.AddDbContext<MyContext>(/* configure EF */);
// Do other stuff related to databases.
// Return the service collection to allow chaining of calls.
return services
}
}
然后在 Startup.cs
你可以这样做:
// Startup.cs
services.AddDatabases();
创建其他文件以添加日志记录、配置、服务、HTTP 客户端等
是的,你绝对可以做到。我一直使用它来保持我的代码干净整洁。您可以使用扩展方法轻松做到这一点:
public class Injections
{
public static IServiceCollection RegisterServices(this IServiceCollection services) => services
.AddTransient<IValidator<Customer>, CustomerValidator>()
.AddTransient<IValidator<Requirement, RequirementValidator>();
public static IServiceCollection AddOtherServices(this IServiceCollection services, IConfiguration configuration) => services
.AddApplicationInsightsTelemetry(configuration["APPINSIGHTS_CONNECTIONSTRING"])
.AddFluentEmail("noreply@myownmail.com")
.AddRazorRenderer()
.AddSmtpSender("smtp.myownmail.com",445);
}
然后在你的Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews()
.AddFluentValidation(opt =>
{
opt.DisableDataAnnotationsValidation = true;
opt.ValidatorOptions.LanguageManager.Culture = new CultureInfo("es");
});
//Dependency Injetions call
services.RegisterServices();
services.AddOtherServices(Configuration);
}