Beispiel #1
0
        /// <summary>
        /// Load a persisted <see cref="IModel"/> instance from disk.
        /// </summary>
        /// <typeparam name="T"> The type of the model. </typeparam>
        /// <param name="path"> The path to the persisted model file on disk. </param>
        /// <param name="ignoreCache">
        /// True if the internal cache should be ignored, forcing a reload of the model from disk, false otherwise.
        /// </param>
        /// <returns> The persisted <see cref="IModel"/> instance. </returns>
        public async Task <T> LoadAsync <T>(string path, bool ignoreCache = false)
            where T : IModel
        {
            if (ignoreCache)
            {
                var(id, data) = await ModelReader.LoadFromAsync(path);

                if (ModelFactories.TryGetValue(id, out var modelFactory))
                {
                    var model = await modelFactory.CreateAsnyc(data);

                    return((T)model);
                }
            }
            else
            {
                if (Cache.TryGetValue(path, out var model))
                {
                    return((T)model);
                }

                await SyncLock.WaitAsync();

                try
                {
                    if (Cache.TryGetValue(path, out model))
                    {
                        return((T)model);
                    }

                    var(id, data) = await ModelReader.LoadFromAsync(path);

                    if (ModelFactories.TryGetValue(id, out var modelFactory))
                    {
                        model = await modelFactory.CreateAsnyc(data);

                        Cache.Add(path, model);
                        return((T)model);
                    }
                }
                finally
                {
                    SyncLock.Release();
                }
            }

            // ToDo: ExceptionHandling
            return(default(T));
        }