/// <inheritdoc /> /// <remarks> /// Initializes the service by caching all instances in the memory. /// Virtual for testing testing purposes. /// </remarks> public virtual async Task InitializeAsync() { await _initializationSlim.WaitAsync().ConfigureAwait(false); try { if (_isCached) { return; } // Allocate place var safeBytes = await GetAllSafeBytesAsync().ConfigureAwait(false); var dictionary = safeBytes.ToDictionary(safeByte => safeByte.Id); var settings = new InitialSafeObjectSettings ( protectionMode: SafeObjectProtectionMode .JustState, /* TODO: Ignore _innerDictionaryProtectionMode because code protection is broken for dictionary as of v0.1 */ initialValue: dictionary, /* set our dictionary */ isReadOnly: true, /* the dictionary will not be modifiable after initialization */ alertChannel: InjectionAlertChannel .ThrowException //**TODO : FIx here by implementing Ialerts for the class**/ ); _safeBytesDictionary = _safeObjectFactory.Get <Dictionary <int, ISafeByte> >(settings); _isCached = true; } finally { _initializationSlim.Release(); } }
/// <summary> /// Initializes the service by caching all instances in the memory. /// </summary> /// <remarks> /// Virtual for testing testing purposes. /// </remarks> public virtual void Initialize() { if (_isCached) { return; } var dictionary = new Dictionary <int, ISafeByte>(); //Allocate place var safeBytes = GetAllSafeBytes(); foreach (var safeByte in safeBytes) //faster than ToDictionary { dictionary.Add(safeByte.Id, safeByte); } var settings = new InitialSafeObjectSettings ( protectionMode: SafeObjectProtectionMode.JustState, /* code protection is broken for dictionary as of v0.1 */ initialValue: dictionary, /* set our dictionary */ isReadOnly: true, /* the dictionary will not be modifiable after initialization */ alertChannel: InjectionAlertChannel.ThrowException //**TODO : FIx here by implementing Ialerts for the class**/ ); _safeBytesDictionary = _safeObjectFactory.Get <Dictionary <int, ISafeByte> >(settings); _isCached = true; }
/// <summary> /// Changes the protection mode of the inner dictionary and its values. /// </summary> protected sealed override void ChangeProtectionMode( IProtectionChangeContext <SafeContainerProtectionMode> context) { Dictionary <string, IInstanceProvider> newDictionary = null; if (_typeInstancesSafe != null) { var innerInstanceProtection = GetInnerInstanceProtectionMode(context.NewValue); newDictionary = _typeInstancesSafe.Object; newDictionary.Values.ForEach(instance => instance.SetProtectionMode(innerInstanceProtection)); _typeInstancesSafe.Dispose(); } //re-create inner safe object _typeInstancesSafe = CreateInnerDictionary(context.NewValue, AlertChannel, newDictionary); }