Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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 ). 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?

like image 210
TanguyB Avatar asked Sep 26 '17 07:09

TanguyB


2 Answers

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.

like image 100
Yurii N. Avatar answered Nov 10 '22 04:11

Yurii N.


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);
}
like image 36
DSR Avatar answered Nov 10 '22 06:11

DSR