Message 1 of 5
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Dear All,
This is to know if there are any drawbacks for this approach. While working with WPF windows, we usually use External Events and Handlers to run transactions. Here I have another approach and found working so far so good.
I have a singleton class, called TransactionRunner, which is initialized within the IExternalCommand object. The current document is then set as the value of its 'Document' property. Here is the class;
public sealed class TransactionRunner
{
private static TransactionRunner _instance;
private static readonly object lockobject = new object();
public Document Document { get; set; }
public string TransactionName { get; set; } = "Transaction Runner";
private TransactionRunner() { }
public static TransactionRunner Instance
{
get
{
lock (lockobject) // thread safety
{
if (_instance == null)
{
_instance = new TransactionRunner();
}
return _instance;
}
}
}
public void Execute(Action action, Action callback = null)
{
if (Document == null)
{
throw new System.Exception("Document is null");
}
using (Transaction t = new Transaction(Document, TransactionName))
{
t.Start();
action();
t.Commit();
}
callback?.Invoke();
}
}
In my Command;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
uidoc = commandData.Application.ActiveUIDocument;
doc = uidoc.Document;
TransactionRunner transactionRunner = TransactionRunner.Instance;
transactionRunner.Document = doc;
// Other calls....
return Result.Succeeded;
}
Now I can call it like this from anywhere in the project;
TransactionRunner.TransactionName = "Some Transaction Name";
TransactionRunner.Instance.Execute(() => {
// Make changes to the document
}, _callbackToUpdateSomething);
Does the approach have any potential issues...?
Solved! Go to Solution.