So right now I got a project working with a N Tier architecture ( 3 layers: API, BL, DAL ). My concern is that all the injection of my services happen in my Startup.cs file.
Is it possible to move them to the correct solution ?
E.G. Startup.cs ConfigureServices method
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.
How do I split it up?
You should create BlStartupExtensions.cs
in the root of your BL project, with the current lines:
public static class BlStartupExtensions
{
public static void ConfigureBlServices(this IServiceCollection services)
{
//register what you need
}
}
And then register it in your Startup.cs
:
services.ConfigureBlServices();
With DAL, solution is absolutely the same.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With