How to implement UnitOfWork on Csla 8 #4108
Unanswered
ecaldentey
asked this question in
Questions
Replies: 1 comment 10 replies
-
What I am hearing, is that you have multiple object graphs, each with its own root object, and you need to update these object graphs within a single unit of work? The easiest way to implement the UoW pattern is typically as a command object that inherits from public class MyUnitOfWork : CommandBase<MyUnitOfWork>
{
// implement one property for each root object
public static readonly PropertyInfo<MyRoot1> MyRoot1Property = RegisterProperty<MyRoot1>(nameof(MyRoot1));
public MyRoot1 MyRoot1
{
get => ReadProperty(MyRoot1Property);
set => LoadProperty(MyRoot1Property, value);
}
[Create]
[RunLoca]
private void Create()
{}
[Execute]
private async Task Execute([Inject] ISqlTransaction transaction, [Inject] IDataPortal<MyRoot1>)
{
transaction.Start()
MyRoot1 = await MyRoot1.SaveAsync();
// repeat for other object graphs
transaction.Commit();
}
}
// calling code with an IDataPortal<MyUnitOfWork> instance
var uow = await commandPortal.CreateAsync();
uow.MyRoot1 = myRoot1;
// set other object graph properties
uow = await commandPortal.ExecuteAsync(uow);
// you now have all the updated object graphs |
Beta Was this translation helpful? Give feedback.
10 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi Rocky,
Could you guide me on how to implement a UnitOfWork in CSLA 8?
I need to perform a series of operations on different CSLA objects within a single transaction, such as editing some records in a BusinessListBase, updating a root BusinessBase with children, etc. In other words, multiple operations across different classes, and if any operation fails, revert everything.
Currently, I can do this separately, one class at a time, but if something fails, I have to delete what has been done class by class, which is insecure and inefficient.
In fact, the updates of the BusinessListBase are a kind of UnitOfWork since this class opens the database connection, starts the transaction, then passes control to the child classes that are root BusinessBase with children. But as they know they are part of a BusinessListBase, the items in the list that are root objects have their IsChild property set to true. They skip opening the database connection because they know the BusinessListBase did it, and the same with the transaction. If something happens with any object in the list, the BusinessListBase takes care of the rollback, and if everything is fine, the BusinessListBase commits and informs the UI. In my opinion, it is a good example of UnitOfWork.
What I want to do is something similar but with a UnitOfWork object that contains all the objects involved in the transaction and that is responsible for making the database connection and opening the transaction. If any of the different objects contained in the UnitOfWork report an error, the UnitOfWork should handle the rollback and report the error to the UI; otherwise, it should commit.
Can you guide me on how to do this in CSLA 8 or indicate where I can find an example? I have reviewed the CSLA projects and forums but haven't found clear information regarding what I need to do.
Thank you
Beta Was this translation helpful? Give feedback.
All reactions