Reflection post for coders (part 2)

I’m getting happier and happier for reflection :slight_smile:

I have hated the message in the “undo” pulldown for years, what does “Dynamo-51297CB5 Script” say!? Nothing at all, in fact, it is super annoying :frowning:
First of all “Script”… come on, it is nearly never a script that causes a transaction, it is with 99.95% a node which needs a transaction in a graph… again it is super annoying :frowning:

So for all programmers especially those who code using C# and build ZeroTouch nodes, If you what to help your users by informing them which node has caused a transaction or just want to inform your users in a better way what is going on like I show below… then use reflection :slight_smile:

What I have done is coding a transaction method calling the Dynamo TransactionManager and then using reflection :slight_smile:

internal static void TransactionEnsure(Document document, string transactionName)
{
    TransactionManager.Instance.EnsureInTransaction(document);

    // Set a proper name for the transaction
    var name = string.Format("Dynamo node: {0}", transactionName);
    typeof(TransactionWrapper).GetField("TransactionName").SetValue(null, name);
}

internal static void TransactionClose()
{
    TransactionManager.Instance.ForceCloseTransaction();
}

Hereafter when I need a transaction I can do it as following… and yes I don’t want to write the name manually, so I also use reflection to get the class and the method :slight_smile:

var transactionName = string.Format("{0}.{1}",
    MethodBase.GetCurrentMethod().DeclaringType.Name,
    MethodBase.GetCurrentMethod().Name);

Services.TransactionEnsure(document, transactionName);
...whatever is needing a transaction...
Services.TransactionClose();

The result is in my view exactly what we need to give better information to users :slight_smile:

What do you think @solamour, @john_pierson

However, I would prefer that the ootb TransactionManager would take a string that could be used for naming the transaction to inform the user.

The “fusion” post for coders (part 1)

7 Likes

Oooh, this is awesome! I have done some of the naming of the undos in the past using the Revit.DB transactions, but that is more cumbersome.

This is great because it is using the Dynamo transaction manager which has consolidation, etc.

Nice work erfajo :+1:

Yes using the Revit API transaction comes with some issues… If you want to go that way you need to kill all other transactions handled by the Dynamo TransactionManager for every transaction, but that is not even enough, you also need to turn your nodes into accepting only complete lists to ensure you have all data at once.

This was why I visited the old friend “reflection”… and yup that is my new best friend when battling with things annoying me which is in the hand of the Dynamo team :slight_smile:
However, I do hope that the team acknowledges some of the changes needed instead of being forced to bypass them using reflection.

So I am glad you find this useful :slight_smile:

.
Just for the record, I also have a…

internal static void TransactionDone()
{
    TransactionManager.Instance.TransactionTaskDone();
}
2 Likes