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

StackOverflow https://stackoverflow.com/questions/16314229

Pregunta

I'm not sure if the title correctly describes my problem. If someone could better describe my problem by reading the following description, please help me by editing the title to something more meaningful.

I'm trying to learn asp.net MVC with Entity Framework and Ninject. I was having a look at NuGet Gallery application on GitHub and tried to implement a few parts in my project.

I followed the answer provided in this question [How do architect an ASP.Net MVC app with EF?] and designed my project with the following layered structure.

MyDemoApp

  • MyDemoApp.Domain (Contains POCO Classes)
  • MyDomain.Service (Contains references to Domain,EF. It contains only Interfaces)
  • MyDemoApp.Data (Contains references to EF, Domain, Service. It contains classes dealing with Entity Context and Repository)
  • MyDemoApp.Web (Contains references to ApplicationModel,Data,Domain,Service,Ninject)
  • MyDemoApp.ApplicationModel (Contains references to Data, Domain, Serivce. It implements the classes from Service project)

MyDemoApp.Web has no business logic and is acting like Humble Object, as mentioned in this answer

I have a Interface IConfiguration in MyDemoApp.Service project which is being implemented by Configuration class located in MyDemoApp.Web where I'm trying to read the connection string. I need to pass this connection string to the object of EntityContext being created in EntityContextFactory located in MydemoApp.Data If I add a project reference of MyDemoApp.web to MyDemoApp.Data then Visual Studio Prompts me saying that it would cause a circular reference

In the following code return new EntitiesContext(""); How should I pass a parameter over here that would get the connection string that my bindings.cs gets ?

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
        }
    }
}
¿Fue útil?

Solución

I don't quite get what problem you are facing. I assume that you need to access a class in Web assembly from Data assembly, but Data assembly already referencing Web assembly.

Can you just inject the configuration interface to your factory constructor, and use that to get the connection string?

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

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

I may misunderstand your question though.

Otros consejos

Personally, I think you should leverage the ConfigurationManager in your EntitiesContextFactory.

This would look like:

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

This class is agnostic of whether it is an app.config or web.config that is providing the configuration.

All it stipulates is that application that is hosting the dlls for running, must be configured with an (app/web).config that contains that connection string. your app can test for this at startup since it knows it has a dependency on a database connection for it to work.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top