Attach an HTTP Request to an Existing Long-Running Transaction
When you are building a multi-page application, it might be useful to create an object in a transaction without committing it.
If you want to provide the user with a URL to an uncommitted object, you will need to attach the view-model of the request handler to the existing long-running transaction.
The following button handler creates a Person object and redirects to its page, without committing the object:
1
public void Handle(Input.CreatePerson action)
2
{
3
var person = new Person()
4
{
5
FirstName = "Uncommitted Guy"
6
};
7
8
RedirectUrl = "/people/persons/" + person.GetObjectID();
9
}
Copied!
When the request handler for "/people/persons/{?}" catches the request after the redirection, it needs to search for the object by its ID in the existing long-running transaction:
1
Handle.GET("/people/persons/{?}", (string id) =>
2
{
3
MasterPage master = GetMasterPageFromSession();
4
5
Action runPartialInScope = () =>
6
{
7
master.CurrentPage = Self.GET("/people/partials/persons/" + id);
8
};
9
10
if (master.CurrentPage.Transaction != null)
11
{
12
// Attach to a long running transaction of the existing CurrentPage
13
master.CurrentPage.Transaction.Scope(runPartialInScope);
14
}
15
else
16
{
17
// If there was no existing CurrentPage, create a new transaction
18
Db.Scope(runPartialInScope);
19
}
20
21
return master;
22
});
Copied!

Read more

Last modified 3yr ago
Copy link
Contents
Read more