1

ドメイン中心のアーキテクチャを使用してasp.netアプリケーションを作成しています。依存関係の注入が原因でアプリケーション層に問題が発生しました。Demeter.Application.Interfaces.IDatabaseServiceを解決できません。Demeter.Application.Events.Queries.QueryEvent.GetEventsListQuery 。依存関係注入を修正するのに助けてくれる人がいますか?IDatabaseService依存性注入に失敗しましたか?

のSystem.InvalidOperationException: は 'Demeter.Application.Events.Queries.QueryEvent.GetEventsListQuery' をアクティブにしようとしたときにタイプ 'Demeter.Application.Interfaces.IDatabaseService' のサービスを解決することができません。

namespace Demeter.Application.Events.Queries.QueryEvent 
{ 
    using System.Collections.Generic; 
    using Commands.CreateEvent; 
    using Demeter.Application.Interfaces; 
    using AutoMapper; 
    using Domain; 


    public class GetEventsListQuery : IGetEventsListQuery 
    { 
     public List<ListEventModel> Execute() 
     { 
      var events = this.databaseService.SelectEventsForList(); 

      //// Use AutoMapper to convert events (IEnumerable<Event>) to (List<ListEventModel>) 
      //IMapper mapperConfig = this.mapperConfig.CreateMapper(); 

      //return Mapper.Map<IEnumerable<Event>, List<ListEventModel>>(events); 
      return null; 
     } 

     public GetEventsListQuery(IDatabaseService databaseService) 
     { 
      this.databaseService = databaseService; 
      //TO-DO: Move this to mapper congigfration function 
      //this.mapperConfig = new MapperConfiguration(cfg => { 
      // cfg.CreateMap<Event, ListEventModel>(); 
      //}); 
     } 

     private readonly IDatabaseService databaseService; 
     private readonly MapperConfiguration mapperConfig; 

    } 
} 

namespace Demeter.Application.Interfaces 
{ 
    using System.Collections.Generic; 
    using Domain; 

    public interface IDatabaseService 
    { 
     void InsertEvent(Event @event); 
     void UpdateEvent(Event @event); 
     void DeleteEvent(long recordId); 
     IEnumerable<Event> SelectEventsForList(); 
    } 
} 

startup.csフォームサービス

public class Startup 
{ 
    public Startup(IHostingEnvironment env) 
    { 
     var builder = new ConfigurationBuilder() 
      .SetBasePath(env.ContentRootPath) 
      .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 
      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 
      .AddEnvironmentVariables(); 

     this.Configuration = builder.Build(); 


    } 

    public IConfigurationRoot Configuration { get; } 

    // This method gets called by the runtime. Use this method to add services to the container. 
    public void ConfigureServices(IServiceCollection services) 
    { 

     // Add framework services. 
     services.AddScoped(provider => 
     { 
      var connectionString = new SqlConnection(Configuration["ConnectionStrings:DevConnection"]); 
      return connectionString; 
     }); 
     // Register the Swagger generator, defining one or more Swagger documents 
     services.AddSwaggerGen(c => 
     { 
      c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); 
     }); 
     services.AddMvc(); 

     services.AddTransient<IGetEventsListQuery, GetEventsListQuery>(); 


    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     loggerFactory.AddConsole(this.Configuration.GetSection("Logging")); 
     loggerFactory.AddDebug(); 


     app.UseMvc(); 

     // Enable middleware to serve generated Swagger as a JSON endpoint. 
     app.UseSwagger(); 

     // Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint. 
     app.UseSwaggerUI(c => 
     { 
      c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); 
     }); 
    } 
} 

}

は、これはあなたがのための具体的なタイプを登録していない持続性

namespace Demeter.Persistance.Services 
{ 
    using System; 
    using System.Collections.Generic; 
    using Application.Interfaces; 
    using Domain; 
    using System.Data; 
    using System.Data.SqlClient; 
    using Dapper; 

    public class DatabaseService : IDatabaseService 
    { 
     public void InsertEvent(Event @event) 
     { 
      throw new NotImplementedException(); 
     } 

     public void UpdateEvent(Event @event) 
     { 
      throw new NotImplementedException(); 
     } 

     public void DeleteEvent(long recordId) 
     { 
      throw new NotImplementedException(); 
     } 

     public IEnumerable<Event> SelectEventsForList() 
     { 
      using (IDbConnection dbConnection = Connection) 
      {  
       return dbConnection.Query<Event>("SELECT * FROM Event"); 
      } 

     } 

     public IDbConnection Connection 
     { 
      get 
      { 
       return new SqlConnection(connectionString); 
      } 
     } 

     public DatabaseService(string connectionString) 
     { 
      this.connectionString = connectionString; 
     } 

     private readonly string connectionString; 

    } 
} 
+1

起動時に 'IDatabaseService'を登録しましたか? – DavidG

+0

DI初期化コードはどこですか? @DavidGが言ったように、あなたはどこかで 'IDatabaseService'の具象型を登録しましたか? –

+0

私はそう考えていません。これはasp.net COREを使用したドメイン中心のアーキテクチャの私の最初の取り組みです。スタートアップでIDatabaseServiceを登録する方法を尋ねることはできますか? – NinjaDeveloper

答えて

2

IDatabaseServiceをASP.NET Coreの依存性注入エンジンに登録する必要があります。

これはStartup.csファイルのConfigureServicesメソッド内で実行されます。

あなたのDatabaseServiceの実装を見ると、接続文字列に依存するようですが、メソッドの中には、すでにSqlConnectionがあります。

あなたは、コンストラクタで直接接続を使用して、いくつかの変更を加える必要があるDIを使用して、すべての仕事をするために:

public void ConfigureServices(IServiceCollection services) 
{ 
    // Add framework services. 
    services.AddScoped<IDbConnection>(provider => new SqlConnection(Configuration["ConnectionStrings:DevConnection"])); 
    // Register the Swagger generator, defining one or more Swagger documents 
    services.AddSwaggerGen(c => 
    { 
     c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); 
    }); 
    services.AddMvc(); 

    services.AddTransient<IGetEventsListQuery, GetEventsListQuery>(); 

    // Register your database service 
    services.AddScoped<IDatabaseService, DatabaseService>(); 
} 

public class DatabaseService : IDatabaseService 
{ 
    public void InsertEvent(Event @event) 
    { 
     throw new NotImplementedException(); 
    } 

    public void UpdateEvent(Event @event) 
    { 
     throw new NotImplementedException(); 
    } 

    public void DeleteEvent(long recordId) 
    { 
     throw new NotImplementedException(); 
    } 

    public IEnumerable<Event> SelectEventsForList() 
    { 
     _dbConnection.Query<Event>("SELECT * FROM Event"); 
    } 

    public DatabaseService(IDbConnection dbConnection) 
    { 
     _dbConnection = dbConnection; 
    } 

    private readonly IDbConnection _dbConnection; 

} 

例では、直接のを持っているように見えるので、私は「スコープ」としての登録を追加しましたデータベース接続への依存(スコープとして宣言されている)

これにより、要求ごとに単一のIDatabaseServiceが作成され、使用されることも保証されます。

2

IDatabaseServiceの実装ですあなたのIDatabaseService中面。このような行を追加します。

services.AddTransient<IDatabaseService, DatabaseService>(); 

をそのせずに、DIフレームワークはGetEventsListQueryクラスのコンストラクタに注入するのか知りません。

.NETコアで依存性注入が行われる方法については、the docsを読むことをお勧めします。

関連する問題