/// <summary> /// Initializes a new instance of the <see cref="SafeObject{TObject}" /> class using custom settings. /// </summary> /// <param name="settings">The settings.</param> /// <exception cref="ArgumentNullException"><paramref name="settings" /> is <see langword="null" /> </exception> public SafeObject(IInitialSafeObjectSettings settings) : this(settings, new InjectionDetector()) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } }
/// <summary> /// <p>Initializes a new instance of the <see cref="SafeObject{TObject}" /> class.</p> /// </summary> /// <param name="settings">The settings.</param> /// <param name="injectionDetector">The injection protector.</param> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="settings" /> is not null and <see cref="InitialSafeObjectSettings.InitialValue" /> is not a type /// of <typeparamref name="TObject" />. /// </exception> /// <exception cref="ArgumentNullException"> /// <p><paramref name="injectionDetector" /> is <see langword="null" />.</p> /// <p> /// Initial object value from <paramref name="settings" /> is null or cannot be casted to /// <typeparamref name="TObject" /> /// </p> /// </exception> internal SafeObject(IInitialSafeObjectSettings settings, IInjectionDetector injectionDetector) : base(settings.ProtectionMode) { _injectionDetector = injectionDetector ?? throw new ArgumentNullException(nameof(injectionDetector)); _injectionDetector.AlertChannel = settings.AlertChannel; SetInitialValue(settings.InitialValue, ref _object); ChangeProtectionMode(new ProtectionChangeContext <SafeObjectProtectionMode>(settings.ProtectionMode)); IsReadOnly = false; IsReadOnly = settings.IsReadOnly; }
public ISafeObject <T> Get <T>(IInitialSafeObjectSettings settings) where T : class, new() { var mock = new Mock <ISafeObject <T> >(); var obj = settings.InitialValue == null ? new T() : (T)settings.InitialValue; mock.Setup(m => m.AlertChannel).Returns(settings.AlertChannel); mock.Setup(m => m.CurrentProtectionMode).Returns(settings.ProtectionMode); mock.Setup(m => m.ApplyChanges(It.IsAny <Action <T> >())).Callback( (Action <T> action) => action.Invoke(obj)); mock.SetupGet(m => m.Object).Returns(obj); return(mock.Object); }
public ISafeObject <T> Get <T>(IInitialSafeObjectSettings settings) where T : class, new() { var mock = new Mock <ISafeObject <T> >(); var isReadOnly = false; var obj = settings.InitialValue == null ? new T() : (T)settings.InitialValue; mock.Setup(m => m.AlertChannel).Returns(settings.AlertChannel); mock.Setup(m => m.IsReadOnly).Returns(() => isReadOnly); mock.Setup(m => m.MakeReadOnly()).Callback(() => isReadOnly = true); mock.Setup(m => m.CurrentProtectionMode).Returns(settings.ProtectionMode); mock.Setup(m => m.ApplyChanges(It.IsAny <Action <T> >())).Callback( (Action <T> action) => { if (isReadOnly) { throw new WriteAccessDeniedException($"{nameof(ISafeObject<T>.MakeReadOnly)} is called"); } action.Invoke(obj); }); mock.SetupGet(m => m.Object).Returns(obj); return(mock.Object); }
public ISafeObject <T> Get <T>(IInitialSafeObjectSettings settings) where T : class, new() { return(new SafeObject <T>(settings)); }