所以現在我有一個項目,A N層架構的工作(3層:API,BL,DAL)。我擔心的是我服務的所有注射在我Startup.cs文件發生。.NET 2.0核心分離Startup.cs服務注入
是否有可能將其移動到正確的解決方案?
E.G. 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());
});
}
理想情況下, 「BL」 部分應該移動到我的BL解決方案,和DAL & MongoDB的一部分,我的DAL的解決方案。
我該如何分割它呢?