How to migrate transaction handling from CSLA 4.8 to 7.0 #3613
Replies: 1 comment 7 replies
-
As a general rule, using dependency injection (DI), you either inject the database connection into your root-level operation method. Starting in CSLA 7 we've embraced DI to conform to the standard .NET way of doing things. You can either inject a connection to the root, and start/end the transaction there (which is probably normal), or you can inject a transaction into every dataportal method, and commit/rollback that transaction in the root. In your example, in your dataportal operation method(s) you are injecting the DAL, which is good. In the DAL, you should inject the database connection so you can use it. In your root level DAL method you should start/end the transaction. Pseudocode: public class MyDal : IMyDal
{
public MyDal(DataConnection connection) {}
public MyRootDto Insert(MyRootDto dto)
{
_connection.StartTransaction();
// map DTO properties to entity properties here
_connection.CommitTransaction();
}
} The idea is that this would be no different from what you'd do any any non-CSLA code when building data access layers in modern .NET. |
Beta Was this translation helpful? Give feedback.
-
Hello everyone,
I am migrating an application from Csla 4.8 to 7.0. My application uses Oracle as the database, and all operations are performed through stored procedures invoked from a DAL (Data Access Layer) project, without using Entity Framework.
In the Business Logic Layer, I handle transactions from the Root object using the TransactionManager and ApplicationContext classes.
The following code is a summarized example of how an Insert is performed from the business layer, invoking the DAL:
protected override void DataPortal_Insert()
{
using (var ctxConn = TransactionManager<OracleConnection, OracleTransaction>.GetManager(MyConnectString, false, MyConnId))
{
Csla.ApplicationContext.LocalContext[MyConnId] = "TransacOK";
}
On the DAL side, when the method is invoked, transaction handling is performed as follows:
// Creates a transaction if it doesn't exist (Root Object) or retrieves the existing open connection (Child Object)
ctxConn = TransactionManager<OracleConnection, OracleTransaction>.GetManager(MyConnectString, false, MyConnId);
OracleCommand cmd = new OracleCommand(procedureName, ctxConn.Connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("p_id", OracleDbType.Decimal).Value = myObjectDto.Id;
// ...
cmd.Transaction = ctxConn.Transaction;
cmd.ExecuteScalar();
In version 7, the TransactionManager class is deprecated, and the ApplicationContext class is no longer static. It must be instantiated. I haven't been able to find an example that demonstrates how to manage transactions. I am injecting it into the Insert method as follows, but I don't know how to share the connection between parent and child classes:
[Insert]
private void Insert(MyRootDto myRootDto, [Inject] IMyRootDal dal)
{
// Implementation
}
Can someone help me solve this, preferably with an example or point me to where I can find one?
Thank you in advance.
Esteban
Beta Was this translation helpful? Give feedback.
All reactions