Upgrade to CSLA 6: ConnectionManager problem

421 Views Asked by At

we are trying to upgrade to CSLA 6. now, we are getting a message: "ConnectionManager is obsolete, use dependency injection ... use ApplicationContext.LocalContext"

for this code:

 using (var ctx = ConnectionManager<OracleConnection>.GetManager("dbEndpoint", true))

We've tried this code snippet but all connections is NULL. Could you please help us to correctly get Connection?

   var services = new ServiceCollection();
   services.AddCsla();
    
   var provider = services.BuildServiceProvider();
    
   DataPortalFactory = provider.GetRequiredService<IDataPortalFactory>();
   var appContext = provider.GetRequiredService<Csla.ApplicationContext>();
    
    
var conn1 = appContext.LocalContext.GetValueOrNull("dbEndpoint");
var conn2 = appContext.LocalContext.GetValueOrNull("__db:default-dbEndpoint");
    
var conn3 = appContext.LocalContext["dbEndpoint"];
var conn4 = appContext.LocalContext["__db:default-dbEndpoint"];

another experiment:

....
                var CONNECTION_ORACLE = new OracleConnection(ConfigurationManager.ConnectionStrings["dbEndpoint"].ConnectionString);

                services.AddScoped<IDbConnection>(o => CONNECTION_ORACLE);
....
var provider = services.BuildServiceProvider();
...
var connectionResolved = provider.GetRequiredService<IDbConnection>();

                appContext.LocalContext.Add("dbEndpoint", connectionResolved);

then connection is not null;

and inside of Factory is successfully resolved by DI:

public DocFactory(ApplicationContext appContext, IDbConnection connection) : base(
    appContext)
{
    _connection = connection;
}

then

[Fetch]
public Doc_Fetch(DocCriteria criteria)
{
    bool cancel = false;
    OnFetching(criteria, ref cancel);
    if (cancel) return null;

    Doc item = null;

    OracleConnection connection = _connection as OracleConnection;

connection is Closed (but NOT null!!). it's possible to open it but if close it, somebody else consuming it will face with a problem or child objects also will face problem with closed connection. so, making ConnectionManager as Obsolete may be not so obvious way to go. But ConnectionManager was very useful for counting open connection, supporting transactions etc Could you please provide a workaround for it. more attempts:

var connectionString = 
ConfigurationManager.ConnectionStrings["dbEndpoint"].ConnectionString;
..
 appContext.ClientContext.Add("DBConnectionString", connectionString );
...
Factory
           using (var connection = new OracleConnection(ApplicationContext.ClientContext["DBConnectionString"].ToString()))

            {
                connection.Open();
1

There are 1 best solutions below

4
Rockford Lhotka On

Your DAL should require that a database connection be injected.

public class MyDal : IDisposable
{
  public MyDal(OracleConnection connection)
  {
    Connection = connection;
  }

  private OracleConnection Connection { get; set; }

  public MyData GetData()
  {
    // use Connection to get the data
    return data;
  }

  public void Dispose()
  {
    Connection.Dispose();
  }
}

Then in the app server startup code, register your DAL type(s) and also register your connection type.

  services.AddScoped(typeof(OracleConnection), () =>
    {
      // initialize the connection here
      return connection;
    });
  services.AddScoped<MyDal>();

Then, in your data portal operation method (such as create, fetch, etc.), inject your DAL:

  [Fetch]
  private void Fetch([Inject] MyDal dal)
  {
    var data = dal.GetData();
  }