Blazor CSLA 7.0 SQL Transactions Does anyone have a samples of how to implement them #3717
-
Hello all, I was wondering if anyone might have a sample of how to implement SQL transcations. I have been reading some post (#2979) but im not sure Im following. Maybe it cant even be done like i have listed below. I dont want to create a all new Dal object just to use a Scope transaction. Currently I am using Transient. Im not sure if that is needed. The code below is how I think it should be done on my Blazor razor page.
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
Any transactions must be managed by the data portal or by your data portal operation method in the root object. There are never transactional behaviors at the UI layers. When using a remote data portal, all data access occurs on the app server, so the interface and interface control layers aren't even on the same physical machine. When using a local data portal, the data portal creates a DI scope to isolate the logical "server-side" operations from the logical "client-side" operations, so it should be practically impossible for the interface and interface control layers to impact behaviors of the logical server (even though it is all running on the same physical machine). You should be able to mark the data portal operation method of the root object with the CSLA transactional attribute and have ADO.NET automatically manage the transaction/rollback for you. That's the simple solution that usually works. [Update]
[Transactional(TransactionalTypes.TransactionScope)]
private void Update([Inject] IProjectDal dal) This tells the data portal to start/commit/rollback a transaction for all ADO.NET operations within the object graph on the logical server. In this case, the DAL presumably injects a database connection - and that connection will automatically enlist in the transaction. private readonly PTrackerContext db;
public ProjectDal(PTrackerContext context)
{
db = context;
} If you have data access that needs to be transactional, and other data access that isn't transactional (like an event log), then you may need to take matters into your own hands. In that case you should use named services via DI to get access to the database connection for your business data, and a different database connection for your event log. In this case, you'll probably use DI mechanisms to ensure that the business data connection has a transaction attached. |
Beta Was this translation helpful? Give feedback.
-
Thank you both of you @Chicagoan2016 and @rockfordlhotka for responding. I have another question: Is there a way to add two Child list for a business object? Or do I need to add a flag to child business object to differentiate between child_1 and Child_2. Both Child_1 and Child_2 have different business propeties. Also the transaction will need be verified for (Parent, Child_1, and Child_2) to ensure that the transaction can be commited or rolledback. I was thinking of creating a BusinessListBase but that can only reference one child list. Any ideas? |
Beta Was this translation helpful? Give feedback.
-
@jhuerta3 I think what you need is very similar to ProjectEdit.cs (parent object) with child list similar to 'Resources' property which is of type ProjectResources. |
Beta Was this translation helpful? Give feedback.
@jhuerta3 I think what you need is very similar to ProjectEdit.cs (parent object) with child list similar to 'Resources' property which is of type ProjectResources.
The code is available in the ProjectTracker sample.
I would still recommend reading Using Csla 4.0 books, I have been working with Csla for some years and I keep those books on my machine.