Asp Net MVC pass connection string to access layer

Asked

Viewed 392 times

0

I am with a project that is divided in layers, BLL DAL and UI, in the UI is my MVC project, however arose the need that in the login screen the user choose the connection string, how can I pass this string from the MVC project to the DAL and continue using it every time I instantiate Dbcontext ?

  • Are you familiar with the Pattern Singleton design? Do you know how to leave your Singleton thread safes classes (ensures uniqueness in different threads)? if you know everything just implement if you do not know comments here that I create an example for you!

  • Thank you @Felipeasunción for the comment, if you can post an example I thank you, thank you.

  • 1

    I added an answer

3 answers

2


Follows a solution using the Singleton Design Pattern

using System;

// Exemplo singleton        
public sealed class ConnectionStringManager
{
    #region Singleton design pattern properties
    private static readonly Lazy<ConnectionStringManager> lazy =
        new Lazy<ConnectionStringManager>(() => new ConnectionStringManager());
    public static ConnectionStringManager Instance { get { return lazy.Value; } }
    private ConnectionStringManager(){ }
    #endregion

    public string CurrentConnectionString { get; set; }

}

// Exemplo de console application
public class Program
{
    public static void Main()
    {
        // Setando a connection string
        ConnectionStringManager.Instance.CurrentConnectionString = "connectionstring selecionado pelo cliente";
        // Voce vai poder acessar a propriedade novamente utilizando 
        // "ConnectionStringManager.Instance.CurrentConnectionString"
        // em qualquer outra classe do sistema.
        Console.WriteLine(ConnectionStringManager.Instance.CurrentConnectionString);
    }
}

If you understand a little English I recommend reading the article: http://csharpindepth.com/Articles/General/Singleton.aspx

2

Another alternative to doing this is also using a hack to change the read-only parameter ConfigurationManager.ConnectionStrings, this solution found on the website http://david.gardiner.net.au/, being like this:

var settings = ConfigurationManager.ConnectionStrings["nomeDaMinhaConexao"];
var fi = typeof(ConfigurationElement).GetField("_bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(settings, false);
settings.ConnectionString = "minhaStringDeConexao";

I put the connection string and was giving the error Keyword not supported: “data source”, to fix it just change where you have &quot; in the connection string for single quotes.

Remembering that it is a hack, however I did several tests here and the application is working normally.

1

Just add the Connectionstrings to your web.config.

List only the names of the connections on the login screen.

Then you can persist the name selected in a Session, and in the other layers you refer to the library System.Web and retrieves the selected name.

And tbm in its other layers, you reference the library System.Configuration and recovers the Connection string with ConfigurationManager.ConnectionStrings["nome"].

But all this is bad practice.

If you want to point to different environments, do it with Web.Config Transformations.

  • Referencing the system.web in the data infrastructure and using the session to store such information is a really bad practice, you could propose other means to solve its problem.

  • Your idea of using singletons is an output. Another would be to inject the name of the selected connstr into the data access layer constructs.

  • Yes, you can propose that solution to our friend?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.