Changes

Internal:Coding Practices

2,047 bytes added, 10:40, 10 July 2015
Created page with "=== Check the integrity of values and especially objects before accessing them! === Before accessing a property of an object '''ALWAYS''' check whether or not the object exist..."
=== Check the integrity of values and especially objects before accessing them! ===
Before accessing a property of an object '''ALWAYS''' check whether or not the object exists
For example, to get the login name use
<syntaxhighlight lang="csharp">
// Get UBIK session from environment
UBIK.Kernel.UBIKSession session = this.Environment.CurrentSession();

string userName = null;

// Make sure that the session and the session.Login object is valid
if ((session != null) && (session.Login != null))
{
userName = this.Environment.CurrentSession().Login.Name;
}
</syntaxhighlight>



=== Convert values between data types within C#? ===

=== Customizing the displayed strings on the client ===
The recommended method to use is <code>[[Internal:DisplayStringItem_(Method)|DisplayStringItem]]</code>.

=== Certain actions need to be done on creation of a new object. Where should the code snippet be placed? ===
Use the event <code>OnPrepareForSave</code> to implement actions carried out before the object is saved.

=== Start a workflow by code, triggered by an action from the client. Where should the code snippet be placed? ===
If possible, use the <code>set</code> method of the property to trigger the workflow. It is also possible to prevent setting the value at all.

Using an event like <code>OnPropertyValueChanged</code> is not recommended, as the value is already written to the property and the version number of the objects is increased already. If the property is only a trigger, this would be too much overhead.

=== How to sort objects by there creation timestamp? ===
'''Scenario:''' Objects should be sorted by its creation timestamp<br />
'''Prerequisites:''' classification CLS_SORTEDITEM

<syntaxhighlight lang="csharp">
DateTime cts = obj.CreationTimeStamp; // obj can be this, ...
TimeSpan t = cts - new DateTime(1, 1, 1); // calculate a timespan
int days = (int)t.TotalDays+1; // for casting to an integer
</syntaxhighlight>

[[Category:Best Practices (internal)|Coding Practices]]
10,686
edits