タイトルが私の問題を正しく説明しているかどうかわかりません。誰かが次の説明を読んで私の問題をよりよく説明できる場合は、タイトルをより意味のあるものに編集してください。
Entity FrameworkとNinjectでasp.net MVCを学ぼうとしています。GitHub で NuGet ギャラリー アプリケーションを見ていて、プロジェクトにいくつかの部分を実装しようとしました。
この質問[EF を使用して ASP.Net MVC アプリを設計するにはどうすればよいですか?]で提供される回答に従い、次の階層構造でプロジェクトを設計しました。
MyDemoApp
- MyDemoApp.Domain (POCO クラスを含む)
- MyDomain.Service (ドメイン、EF への参照が含まれます。インターフェイスのみが含まれます)
- MyDemoApp.Data (EF、ドメイン、サービスへの参照が含まれています。エンティティ コンテキストとリポジトリを扱うクラスが含まれています)
- MyDemoApp.Web (ApplicationModel、Data、Domain、Service、Ninject への参照を含む)
- MyDemoApp.ApplicationModel (Data、Domain、Service への参照が含まれます。Service プロジェクトのクラスを実装します)
MyDemoApp.Web
この回答で述べたように、ビジネス ロジックはなく、Humble Object のように動作しています。
接続文字列を読み取ろうとしている MyDemoApp.Web にある Configuration クラスによって実装されている MyDemoApp.Service プロジェクトに Interface IConfiguration があります。この接続文字列を、MydemoApp.Data にある EntityContextFactory で作成されている EntityContext のオブジェクトに渡す必要があります。MyDemoApp.web のプロジェクト参照を MyDemoApp.Data に追加すると、Visual Studio から循環参照が発生するというメッセージが表示されます。
次のコードreturn new EntitiesContext("");
では、 bindings.cs が取得する接続文字列を取得するパラメーターをここに渡すにはどうすればよいですか?
namespace MyDemoApp.Data
{
public class EntitiesContextFactory : IDbContextFactory<EntitiesContext>
{
public EntitiesContext Create()
{
//TO-DO : Get the Connnectionstring
return new EntitiesContext(""); //Need to pass connection string by calling property from Configuration class in MyDemoApp.Web project
}
}
public class EntitiesContext:DbContext,IEntitiesContext
{
public EntitiesContext(string connectionString) : base(connectionString)
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Provide mapping like foreign key
}
}
}
}
Configuration.cs
:
namespace MydemoApp.Web
{
public class Configuration : IConfiguration
{
public string ConnectionString
{
get
{
return ConfigurationManager.ConnectionStrings['dev'].ConnectionString;
}
}
}
}
Bindings.cs
:
namespace MydemoApp.Web.Bindings
{
public class MyModule : NinjectModule
{
public override void Load()
{
Bind<IConfiguration>().To<Configuration>();
var configuration = new Configuration(); //Gives me Connectionstring
Bind<IEntitiesContext>().ToMethod(context => new EntitiesContext(configuration.ConnectionString)); // This part would help me pass the connection string to the constructor
}
}
}