Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use lazy initialization to postpone the database call until you actually need the information (if ever), and then store the value for re-use.</p> <pre><code>Private _isPowerUser As New Lazy(Of Boolean)(AddressOf GetIsPowerUser) Public Function IsPowerUser() As Boolean Return _isPowerUser.Value End Function Private Function GetIsPowerUser() As Boolean ' Retrieve the information from the database and return Return True Or False End Function </code></pre> <p>In this sample, <code>_isPowerUser</code> is a backing field that uses lazy initialization. It is first initialized when someone calls <code>IsPowerUser</code>. Under the covers, <code>Lazy(Of T)</code> then calls the delegate <code>GetIsPowerUser()</code> that does all the heavy lifting to retrieve the value from the database.</p> <p><code>Lazy(Of T)</code> only needs to call <code>GetIsPowerUser()</code> once. In other words: any consecutive calls to <code>IsPowerUser()</code> do not trigger <code>GetIsPowerUser()</code>.</p> <p><strong>EDIT</strong> (1/2)</p> <p><code>Lazy(Of T)</code> was not introduced until .NET 4, so here is a code sample that works for .NET 2 and above:</p> <pre><code>Private _isPowerUser As Boolean? Private lazyLock As New Object Public Function IsPowerUser() As Boolean If Not _isPowerUser.HasValue Then SyncLock lazyLock If Not _isPowerUser.HasValue Then _isPowerUser = GetIsPowerUser() End If End SyncLock End If Return _isPowerUser.Value End Function Private Function GetIsPowerUser() As Boolean ' Retrieve the information from the database and return Return True Or False End Function </code></pre> <p><strong>EDIT</strong> (2/2)</p> <p>I changed my code sample to work in a multi-user environment:</p> <pre><code>Private _powerUsers As New Dictionary(Of Long, Boolean) Private ReadOnly LazyLock As New Object Public Function IsPowerUser(userId As Long) As Boolean If Not _powerUsers.ContainsKey(userId) Then SyncLock LazyLock If Not _powerUsers.ContainsKey(userId) Then _powerUsers.Add(userId, GetIsPowerUser(userId)) End If End SyncLock End If Return _powerUsers.Item(userId) End Function Private Function GetIsPowerUser(UId As Long) As Boolean ' Retrieve the information from the database and return Return True Or False End Function </code></pre> <p>Usage example:</p> <pre><code> Dim Steven As Long = 454151 If IsPowerUser(Steven) Then Console.WriteLine("Steven is a power user.") Else Console.WriteLine("Steven is a normal user.") End If </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload