1

我不确定标题是否正确描述了我的问题。如果有人能够通过阅读以下描述来更好地描述我的问题,请通过将标题编辑为更有意义的内容来帮助我。从单独项目中的类访问方法/属性而不添加对包含类的项目的引用(相同的解决方案)

我想学习与实体框架和Ninject的asp.net MVC。 我正在看看GitHub上的NuGet Gallery应用程序,并试图在我的项目中实现几个部分。

我跟着这个问题提供的答案[How do architect an ASP.Net MVC app with EF?]和设计我的项目与以下分层结构。

MyDemoApp

  • MyDemoApp.Domain(包含POCO类)
  • MyDomain.Service(包含对域名的引用,EF,它仅包含接口)
  • MyDemoApp.Data(包含引用到EF, (包含对ApplicationModel,Data,Domain,Service,Ninject的引用)
  • MyDemoApp.Appli cationModel(包含对数据,域,服务的引用。它实现了从服务项目中的类)

MyDemoApp.Web没有业务逻辑,并像个谦逊对象,如this answer

提到我在MyDemoApp.Service项目接口IConfiguration正在由配置来实现类位于MyDemoApp.Web,我试图读取连接字符串。我需要将此连接字符串传递给位于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 
     } 
    } 
} 

回答

1

我不太明白你面临的问题。我假设你需要从数据程序集中访问Web程序集中的类,但是数据程序集已经引用了Web程序集。

你可以只将配置接口注入到你的工厂构造函数中,然后用它来获取连接字符串吗?

public class EntitiesContextFactory : IDbContextFactory<EntitiesContext> 
{ 
    public EntitiesContextFactory(IConfiguration configuration){ 
     this.configuration = configuration; 
    } 
    IConfiguration configuration; 

    public EntitiesContext Create() 
    { 
     return new EntitiesContext(configuration.ConnectionString); 
    } 
} 

虽然我可能会误解你的问题。

0

就个人而言,我认为你应该充分利用ConfigurationManager中在EntitiesContextFactory。

这看起来像:

​​

这个类是不可知的,是否是一个app.config或正在提供配置web.config中。

它规定的是,运行dll的应用程序必须使用包含该连接字符串的(app/web).config进行配置。你的应用程序可以在启动时进行测试,因为它知道它有依赖于数据库连接的功能。

+0

我在我的web.config中有2个连接字符串,我也有一个配置类中的属性,它允许我获得不同的连接字符串。例如用于开发的连接字符串,或者可能是基于web.config中设置的环境的生产。你可以建议任何方法,让我连接字符串使用ninject(构造函数注入)实际上没有硬编码连接字符串名称在EntitiesContext – 2013-05-01 09:15:33

+0

个人我只有1个连接字符串,然后我使用配置转换切换连接字符串。节省忘记改变它,因为你在构建过程中设置它并保留它。 – Slicksim 2013-05-01 11:51:53

+0

我会等待一段时间来自其他成员的意见。如果我没有得到任何其他答案,那么我会按照您的建议去做,并将其标记为已接受的答案。 – 2013-05-01 12:11:22

相关问题