关于asp.net mvc:从单独项目中的类访问方法/属性,而无需添加对包含该类的项目的引用(相同的解决方案)

Accessing Methods/Properties from a class in a separate project without adding reference to the project containing the class (Same solution)

我不确定标题是否正确描述了我的问题。如果有人可以通过阅读以下说明更好地描述我的问题,请通过将标题编辑为更有意义的内容来帮助我。

我正在尝试使用Entity Framework和Ninject学习asp.net MVC。
我正在查看GitHub上的NuGet Gallery应用程序,并尝试在我的项目中实现一些部分。

我遵循了此问题提供的答案[如何使用EF设计ASP.Net MVC应用程序?],并使用以下分层结构设计了我的项目。

MyDemoApp

  • MyDemoApp.Domain(包含POCO类)
  • MyDomain.Service(包含对Domain,EF的引用。它仅包含接口)
  • MyDemoApp.Data(包含对EF,域,服务的引用。它包含处理实体上下文和存储库的类)
  • MyDemoApp.Web(包含对ApplicationModel,Data,Domain,Service,Ninject的引用)
  • MyDemoApp.ApplicationModel(包含对Data,Domain,Serivce的引用。它实现Service项目中的类)

MyDemoApp.Web没有业务逻辑,并且表现得像谦虚对象,如此答案中所述

我在MyDemoApp.Service项目中有一个接口IConfiguration,该项目由位于MyDemoApp.Web中的Configuration类正在实现,我正在其中尝试读取连接字符串。我需要将此连接字符串传递给位于MydemoApp.Data中的EntityContextFactory中创建的EntityContext对象。
如果我将MyDemoApp.web的项目引用添加到MyDemoApp.Data,则Visual Studio提示我说这将导致循环引用

在以下代码中return new EntitiesContext("");我应该如何在此处传递一个参数,该参数将获取我的bindings.cs获取的连接字符串?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

1
2
3
4
5
6
7
8
9
10
11
12
13
namespace MydemoApp.Web
{
    public class Configuration : IConfiguration
    {
        public string ConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings['dev'].ConnectionString;
            }
        }
    }
}

Bindings.cs

1
2
3
4
5
6
7
8
9
10
11
12
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
        }
    }
}

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

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

1
2
3
4
5
6
7
8
9
10
11
12
public class EntitiesContextFactory : IDbContextFactory<EntitiesContext>
{
    public EntitiesContextFactory(IConfiguration configuration){
        this.configuration = configuration;
    }
    IConfiguration configuration;

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

但是我可能会误解您的问题。


我个人认为,您应该在EntitiesContextFactory中利用ConfigurationManager。

这看起来像:

1
return new EntitiesContext(System.Configuration.ConfigurationManager.ConnectionStrings["{connectionstringname}"].ConnectionString)

此类与提供配置的app.config或web.config无关。

它所规定的就是托管运行dll的应用程序必须使用包含该连接字符串的(app / web).config进行配置。您的应用可以在启动时对此进行测试,因为它知道它依赖于数据库连接才能正常工作。