Ho il seguente codice che potrebbe essere chiamato tramite più richieste web nello stesso secondo. Come tale, non voglio che la seconda + richiesta colpisca il database, ma attendo fino a quando il primo non lo fa.Questo codice C# dovrebbe essere refactored per utilizzare invece la classe Lazy <T>?
Devo refactor questo per utilizzare invece la classe Lazy<T>
parola chiave
? Se 10 chiamate a una parte di codice Lazy<T>
si verificano allo stesso tempo, 9 di queste chiamate attendono il completamento della prima?
public class ThemeService : IThemeService
{
private static readonly object SyncLock = new object();
private static IList<Theme> _themes;
private readonly IRepository<Theme> _themeRepository;
<snip snip snip>
#region Implementation of IThemeService
public IList<Theme> Find()
{
if (_themes == null)
{
lock (SyncLock)
{
if (_themes == null)
{
// Load all the themes from the Db.
_themes = _themeRepository.Find().ToList();
}
}
}
return _themes;
}
<sip snip snip>
#endregion
}
'Lazy' non è una parola chiave. –
BoltClock
Allora come si chiama? –
Si chiama semplicemente un tipo. Più specificamente, è una classe. – BoltClock