简体   繁体   中英

.Net Core 3-tier architecture passing connection string from presentation layer to data layer issue

I have a Web API with 3 tier architecture as shown below:

PresentationLayer (in the presentation layer i have appsetting.json where the connection string is)

BusinessLayer (Class library)

DataLayer (class library)

When I was still using .Net Framework I used to do in the dataLayer this code shown in the link below to get the connection string from the web.config of the presentation layer:

在此处输入图片说明

Now days I am experimenting with .Net Core 2.1 and i have build the same class in the data layer but it is not working. how can I pass the connection string from the presentation layer to the data layer most efficient way. Is there a similar way how i used to pass the connection string when i was working with .Net framework.

Can you help me please.

how can I pass the connection string from the presentation layer to the data layer most efficient way. Is there a similar way how i used to pass the connection string when i was working with .Net framework.

In Asp.Net Core, it is much easier to passing connection to DbConnection with dependence Injection.

  • Change DbConnection to accept connection string

     public class DbConnection { public DbConnection(string connectionString) { _sqlConnection = new SqlConnection(connectionString); } 
  • Register DbConnection in PresentationLayer

     services.AddTransient<DbConnection>(serviceProvider => new DbConnection(Configuration.GetConnectionString("DefaultConnection"))); 

Create AppSettings class

public static class AppSettings
{
    public static string ConnectionString { get; set; }
}

In Startup.cs

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
    BuildAppSettingsProvider();
}

public IConfiguration Configuration { get; }

private void BuildAppSettingsProvider()
{
    AppSettings.ConnectionString = Configuration["ConnectionString"];
}

now you can access AppSettings in your presentation layer.

100% works.

When you have multiple layers you most likely have an IOC Container in use. Create an interface in your datalayer and do the implementation in your view layer and then register and get it with your IOC Container. This is the most clean way to do it if you cant pass it directly in your SqlConnection.

There are (min) 3 options. I think the best one is to use an IoC container. Maybe you prefer to add an appconfig file to DB layer and access to it for layer specific settings. Or, you can use the default injection mechanism to pass the IConfiguration to Business Layer, and then pass it to DataLayer via ctors.

For example

/* ----- Startup.cs ----- */
public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IxxxService, xxxBusinessService>();
}

/* ----- UI Layer ----- */
public xxxController(IxxxService xxxBusinessService)
{
   this.xxxBusinessService = xxxBusinessService;
}

/* ----- BUSINESS LAYER ----- */
/*
UI/controller knows Business service and IConfiguration objects, and default 
injector automatically creates/passes configuration object via ctor to Business layer.
*/
public xxxService(IConfiguration configuration)
{
    this.xxxRepository = new xxxRepository(configuration);
}

/* ----- DATA LAYER ----- */
public class xxxRepository: BaseRepository, IxxxRepository
{
    public xxxRepository(IConfiguration configuration)
        : base(configuration)
    {

    }
}       

public class BaseRepository{

    protected xxxDbContext context;

    public BaseRepository(IConfiguration configuration)
    {   
        var optionsBuilder = new DbContextOptionsBuilder<xxxDbContext>();

        optionsBuilder.UseSqlServer(configuration.GetSection("ConnectionString")["DefaultConnection"]);

        this.context = new xxxDbContext(optionsBuilder.Options);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM