The way I do it is to always use the API, unless I don't have any other choice.
But only load the elements you need, when you need them. If you load them too soon, they might already have changed, or worse deleted, when you deal with them.
There are a few tips however to make the process a bit smoother.
- iterating collections is super slow. Avoid that at all costs. If you need, say all requirements with status "Active", then an SQL query to get those requirements. There is Repository.GetElementSet, and Repository.SQLQuery which you can use to get either the complete set of elements, or a set of ID's that you can use to load the elements (or diagrams, or attributes, or...)
- If you are not sure the API element you have in memory still represents the latest state, then reload it by getting it again based on it's id.
- Use the undocumented Repository.Execute only when there is no other option. You can pass an SQL update query to that method and it will get executed. There is no feedback.
All of my C# addins are open source and available on github:
https://github.com/GeertBellekensGeert