Cloud Service
How to SetBasePath in ConfigurationBuilder in Core 2.0 ?

How to SetBasePath in ConfigurationBuilder in Core 2.0 ?

ASP.NET Core this a cross-platform & open-source web framework of Microsoft is a good option to build modern web or internet applications that can be deployed to the cloud server.

In .NET Core, configuration has been updated. previously its System.Configuration model that relied on XML-based configuration files. And now introducing a number of new configuration components offering more flexibility and better extensibility.

Here I am explaining 'How to set the base path in ConfigurationBuilder in Core 2.0.'

Install the packages

.NET Core configuration components are published under Microsoft.Extensions.Configuration.*packages on NuGet. In my code sample I have used below libraries.

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.Json (support for JSON configuration files)

https://www.nuget.org/packages/Microsoft.Extensions.Configuration/

https://www.nuget.org/packages/Microsoft.Extensions.Configuration.Json/

In this dot net core 2.0 test scenario we have a data access layer and it is a separate class library.I am trying to get the connection string from configuration file and get data from database.

ConnectionFactory


using System.Data;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace DataAccess.Infrastructure
{
 public class ConnectionFactory : IConnectionFactory
 {
    public ConnectionFactory()
    {
        var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
        Configuration = builder.Build();
    }


    public IConfigurationRoot Configuration { get; }
    public IDbConnection GetConnection
    {
        get
        {
            var connectionString = Configuration.GetConnectionString("DefaultConnection");
            var conn = new SqlConnection(connectionString);
            conn.Open();
            return conn;
        }
    }

    #region IDisposable Support
    private bool disposedValue = false; // To detect redundant calls

    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                // TODO: dispose managed state (managed objects).
            }


            // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
            // TODO: set large fields to null.

            disposedValue = true;
        }
    }

    // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
    // ~ConnectionFactory() {
    //   // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
    //   Dispose(false);
    // }

    // This code added to correctly implement the disposable pattern.
    public void Dispose()
    {
        // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
        Dispose(true);
        // TODO: uncomment the following line if the finalizer is overridden above.
        // GC.SuppressFinalize(this);
    }
    #endregion
} }

IConnectionFactory


using System;
using System.Data;

namespace DataAccess.Infrastructure
{
	public interface IConnectionFactory : IDisposable
	{
		IDbConnection GetConnection { get; }
	}
}

BaseRepository



using DataAccess.data.Infrastructure;
namespace DataAccess.data.Repository
{
	public abstract class BaseRepository
	{
		protected readonly IConnectionFactory connectionFactory;
		public BaseRepository(IConnectionFactory connectionFactory)
		{
			this.connectionFactory = connectionFactory;
		}
	}
}

ClassRepository


namespace DataAccess.data.Repository
{
	public partial class BannerRepository : BaseRepository, IBannerRepository
	{
		public BannerRepository(IConnectionFactory connectionFactory) : base(connectionFactory) { }
 
		public async Task Get(System.Int32? bannerID)
		{
			using (var connection = connectionFactory.GetConnection)
			{
			 
                           /* your sql command code here  */
				
			}
		}

          }
}
String to Date and Time object conversion – Android
String to Date and Time object conversion – Android
How to SetBasePath in ConfigurationBuilder in Core 2.0 ?
How to SetBasePath in ConfigurationBuilder in Core 2.0 ?

Related Posts