using System;
namespace DTT.Utils.Workflow
{
///
/// Wraps a value that is not guaranteed to be created.
///
/// The type of value to create.
public class Unguaranteed where T : class
{
///
/// The unguaranteed value.
///
private readonly T _value;
///
/// The unguaranteed value.
///
public T Value
{
get
{
OnValueAccess();
return _value;
}
}
///
/// Whether the value was created or not.
///
public bool IsValueCreated => _value != null;
///
/// Called when the value is being accessed.
///
protected virtual void OnValueAccess()
{
if (!IsValueCreated)
throw new InvalidOperationException("Value was not created.");
}
///
/// Initializes the value using a constructor for the value.
///
/// The constructor.
public Unguaranteed(Func constructor)
{
if (constructor == null)
throw new ArgumentNullException(nameof(constructor));
_value = constructor();
}
}
}