Exemplo n.º 1
0
        // If 1) reload flag is set to true and 2) OnLoadException handler is not set,
        // all exceptions raised during OnReload() will be ignored.
        private async Task LoadAsync(bool reload, bool preFetch = false)
        {
            // Wait for any already running load operations to complete before starting another.
            // This can happen if the framework calls Load before the pre-fetch completes.
            await Lock.WaitAsync().ConfigureAwait(false);

            try
            {
                // Check if the Data has been prefetched. If this is a reload,
                // ignore pre-fetched data. We only prefetch during initialization.
                if (DataHasBeenPreFetched && !reload)
                {
                    return;
                }

                var newData = await SystemsManagerProcessor.GetDataAsync().ConfigureAwait(false) ?? new Dictionary <string, string>();

                if (!Data.EquivalentTo(newData))
                {
                    Data = newData;

                    OnReload();
                }
            }
            catch (Exception ex)
            {
                if (Source.Optional)
                {
                    return;
                }

                var ignoreException = reload;
                if (Source.OnLoadException != null)
                {
                    var exceptionContext = new SystemsManagerExceptionContext
                    {
                        Provider  = this,
                        Exception = ex,
                        Reload    = reload
                    };
                    Source.OnLoadException(exceptionContext);
                    ignoreException = exceptionContext.Ignore;
                }

                if (!ignoreException)
                {
                    throw;
                }
            }
            finally
            {
                // Always reset the flag and release the lock
                DataHasBeenPreFetched = preFetch;
                Lock.Release();
            }
        }
        // If 1) reload flag is set to true and 2) OnLoadException handler is not set,
        // all exceptions raised during OnReload() will be ignored.
        private async Task LoadAsync(bool reload)
        {
            try
            {
                var newData = await SystemsManagerProcessor.GetDataAsync().ConfigureAwait(false) ?? new Dictionary <string, string>();

                if (!Data.EquivalentTo(newData))
                {
                    Data = newData;

                    OnReload();
                }
            }
            catch (Exception ex)
            {
                if (Source.Optional)
                {
                    return;
                }

                var ignoreException = reload;
                if (Source.OnLoadException != null)
                {
                    var exceptionContext = new SystemsManagerExceptionContext
                    {
                        Provider  = this,
                        Exception = ex,
                        Reload    = reload
                    };
                    Source.OnLoadException(exceptionContext);
                    ignoreException = exceptionContext.Ignore;
                }

                if (!ignoreException)
                {
                    throw;
                }
            }
        }