/// <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;
        }
Beispiel #2
0
        /// <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();
            }
        }