2017-09-26 51 views
2

所以現在我有一個項目,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的解決方案。

我該如何分割它呢?

回答

3

你應該在你的BL項目的根目錄創建BlStartupExtensions.cs,與當前行:

public static class BlStartupExtensions 
{ 
    public static void ConfigureBlServices(this IServiceCollection services) 
    { 
     //register what you need 
    } 
} 

然後在它註冊您的Startup.cs

services.ConfigureBlServices(); 

隨着DAL,解決方案是絕對的相同。

2

你可以在你的相關項目如下面創建擴展。

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; 
    } 
} 

然後你就可以添加依賴如下。

public void ConfigureServices(IServiceCollection services) 
{   
    services.AddDALDependencies(Configuration); 
}