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

net core data access layer
clean architecture net core
asp.net core architecture
getting started with net core clean architecture
clean architecture c#
connection string in appsettings json with username and password

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.

N-Tier Architecture in ASP.NET Core - Udara Bibile, 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  .Net Core 3-tier architecture passing connection string from presentation layer to data layer issue 0 How do I get a connection string in a .net core standard class library from the configuration file in a .net core 2.0 web app?

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.

Best way to store and access Connection String for a 3-tier web app , Part 4: Build layered architecture for separation of concern, 3-Tier Architecture is typical N-Tier design where application is Then data access layer would connect to database, execute required Typically presentation layer is added as controllers. public Task<Author> GetByName(string name) => Business Layer is mainly working as the bridge between Data Layer and Presentation Layer. All the Data passes through the Business Layer before passing to the presentation Layer. Business Layer is the sum of Business Logic Layer, Data Access Layer and Value Object and other components used to add business logic.

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.

Create and Implement 3-Tier Architecture in ASP.Net, The web pages form the presentation layer. The business layer ( BLL ) and the Data Access layer ( DAL ) are class libraries . They each have What is the best way to handle connection strings for a 3-tier web application? Gary Frank NET to access the database and return generic lists of objects. If these  Building a multi layer API with .NET Core 3 using Repository and Unit of Work pattern, Entity Framework, AutoMapper and Fluent Validation.

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);
    }
}

ASP.NET Core: A Multi-Layer Data Service Application Migrated , Create and Implement 3-Tier Architecture in ASP.Net. Saineshwar Bageri the Business Layer to connect the data and perform required actions, Though a web site could talk to the data access layer directly, Application Layer or Presentation Layer (our web form and UI Part) NET Core Application. Service layer; Domain layer; Data layer; In my Web API I'm loading data from an external Exchange Webservices. i would like to provide in my own Web API for my clients. For this I implemented a repository at my data layer which gets the data from the external webservice as objects from Class Appointment.

Step by step 3 tier architecture in asp.net using c# example, NET Core version 2.x and 3.0) and also describing the various issues Primary Key Identity Insert Issue; Data Context and Connection String; Custom Repositories Image 3. When migrating to the ASP.NET Core, those projects must WebApi with the EF6 passes the connection string to the data context  3 Tier Architecture in ASP.NET with Example using C# : Three tier architecture means dividing your project into three different layers that is Presentation Layer (User Interface Layer), Application Layer or Business Access Layer (Logic Code Layer) and Data Access Layer (Layer which connects to database).

How to Implement 3 Tier Architecture Concepts in asp.net with Real , Three tier architecture means dividing our project into three layers that is presentation layer then business layer passes those records to the presentation layer. Step 3 : Create a connection to Data Provider Now lets call that sql connection string in database layer file. Step 5 : Create Business Layer This article explains how to create and implement a 3-tier architecture for our project in ASP.Net. A layer is a reusable portion of code that performs a specific function. In the .NET environment, a layer is usually set up as a project that represents this specific function. This specific layer is in charge of working with other layers to

Advanced Architecture for ASP.NET Core Web API, In 3 tier application Presentation layer can't talk with Data Access Layer layer --​>After that this data pass data to Data Access Layer -->After that data Note:-If you are facing problem to add .mdf file on your project then read below links:- If you don't use Sql Data Source then you will have to add connection string(sql  Layered Architecture in ASP.NET Core Applications. One of the viewers of my YouTube channel asked me an interesting question. He mentioned in a typical layered architecture, he sees ASP.NET MVC building blocks (Controller, View, and Model) as part of the presentation layer.

Comments
  • Please do not post screenshots of your code, copy/paste your source code as text instead.
  • I don't think if it's a good practice to pass connection string from PL to DL.
  • Are you not using Entity Framework? If not, I recommend you look at it. At the very least though, go and learn about dependency injection.
  • @DavidG no I don't want to use Entity framework. And by Dependency injection you mean create an appsettings.json in the datalayer?
  • If I had to go back to using ADO.Net directly, I would hurt someone. There's really almost no need to avoid using it (or another ORM). Anyway.... no, that's not what DI is at all, you absolutely need to go do your research.
  • The DbConnection class it is in the dataLayer which is a class library. the presenation layer don't now about the dataLayer
  • @Whiplash References in .Net Core are transitive meaning that if ProjectA has a reference to ProjectB and ProjectB references ProjectC, then ProjectA will be able to see everything in ProjectC.