Open
Description
Background and motivation
Lazy<T>
currency always caches exceptions When LazyThreadSafetyMode
is ExecutionAndPublication
or None
and factory method is used. This often is not desired behavious as factory method often can throw exception caused by transient error.
API Proposal
Add overload with additional bool or enum parameter, which allows to explicitly indicate exception caching behaviour:
public Lazy(Func<T> valueFactory, LazyThreadSafetyMode mode, bool cacheException)
API Usage
public class ExampleClass
{
public DatabaseConnection DatabaseConnection => _databaseConnectionLazy .Value;
private DatabaseConnection GetConnection(){}
private Lazy<DatabaseConnection> _databaseConnectionLazy = new Lazy<DatabaseConnection>
(GetConnection, LazyThreadSafetyMode.ExecutionAndPublication, false);
}
Alternative Designs
No response
Risks
No response