[英].NET Core 2.0 Separating Startup.cs Services Injection
So right now I got a project working with a N Tier architecture ( 3 layers: API, BL, DAL ). 所以现在我得到了一个使用N层架构的项目(3层:API,BL,DAL)。 My concern is that all the injection of my services happen in my Startup.cs file.
我担心的是我的所有服务注入都发生在我的Startup.cs文件中。
Is it possible to move them to the correct solution ? 是否有可能将它们转移到正确的解决方案?
EG Startup.cs ConfigureServices method EG Startup.cs ConfigureServices方法
public void ConfigureServices(IServiceCollection services)
{
//MVC
services.AddMvc();
//Swagger
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Title = "2Commit Blogpost API",
Version = "v1"
});
});
services.ConfigureSwaggerGen(options =>
{
options.CustomSchemaIds(x => x.FullName);
});
//Mediatr
services.AddScoped<IMediator, Mediator>();
services.AddTransient<SingleInstanceFactory>(sp => sp.GetService);
services.AddTransient<MultiInstanceFactory>(sp => sp.GetServices);
services.AddMediatorHandlers(typeof(Startup).Assembly);
//MongoDB
services.Configure<MongoSettings>(s =>
{
s.Database = Configuration.GetSection("MongoConnection:Database").Value;
});
services.AddSingleton<IMongoClient, MongoClient>(client => new MongoClient(Configuration.GetSection("MongoConnection:ConnectionString").Value));
//BL
services.AddTransient<IUserService, UserService>();
services.AddTransient<IAccountService, AccountService>();
//DAL
services.AddTransient<IRepository, MongoRepository>();
//Authentication
services.AddAuthentication()
.AddJwtBearer(jwt =>
{
var signingKey =
new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration.GetSection("Secret:Key").Value));
jwt.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = signingKey,
ValidateIssuer = true,
ValidIssuer = "2CIssuer",
ValidateAudience = true,
ValidAudience = "2CAudience",
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero
};
});
//Authorization
services.AddAuthorization(auth =>
{
auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme).RequireAuthenticatedUser().Build());
});
}
Ideally, the "BL" part should move to my BL Solution, and the DAL & MongoDB part to my DAL solution. 理想情况下,“BL”部分应移至我的BL解决方案,DAL和MongoDB部分移至我的DAL解决方案。
How do I split it up? 我该如何拆分?
You should create BlStartupExtensions.cs
in the root of your BL project, with the current lines: 您应该使用当前行在BL项目的根目录中创建
BlStartupExtensions.cs
:
public static class BlStartupExtensions
{
public static void ConfigureBlServices(this IServiceCollection services)
{
//register what you need
}
}
And then register it in your Startup.cs
: 然后在
Startup.cs
注册它:
services.ConfigureBlServices();
With DAL, solution is absolutely the same. 使用DAL,解决方案绝对相同。
You could create extension in your relevant project like below. 您可以在相关项目中创建扩展,如下所示。
public static class DalServiceCollectionExtensions
{
public static IServiceCollection AddDALDependencies(this IServiceCollection services, IConfigurationRoot configuration)
{
services.Configure<MongoSettings>(s =>
{
s.Database = configuration.GetSection("MongoConnection:Database").Value;
});
services.AddSingleton<IMongoClient, MongoClient>(
client => new MongoClient(configuration.GetSection("MongoConnection:ConnectionString").Value));
return services;
}
}
Then you can add dependencies as below. 然后,您可以添加依赖项,如下所示。
public void ConfigureServices(IServiceCollection services)
{
services.AddDALDependencies(Configuration);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.