Let's get straight. I have interface and class like this:
public interface IDataBase
{
DataTable GetSomeTableData();
}
My class:
public class DataBase : IDataBase
{
private readonly string _connectionString;
public DataBase(string connectionString)
{
this._connectionString = connectionString;
}
public DataTable GetSomeTableData()
{
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
// some select
}
}
}
I'm using Autofac to inject that class:
var builder = new ContainerBuilder();
builder.RegisterType<DataBase>().As<IDataBase>).WithParameter("connectionString", "my connection string");
var container = builder.Build();
var database = container.Resolve<IDataBase>();
var tableData1 = database.GetSomeTableData();
// change connection string ?????????????????
var tableData2 = database.GetSomeTableData();
I need to get table data from one DB and another DB. How can I change connection string after have registered class? You may give another exapmle..