コード例 #1
0
ファイル: BibleService.cs プロジェクト: theglorych/Bible2PPT
        private void LinkSource(BibleBase target, BibleSource source)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            target.Source   = source;
            target.SourceId = source.Id;
        }
コード例 #2
0
ファイル: BibleService.cs プロジェクト: theglorych/Bible2PPT
        public async Task <List <Bible> > GetBiblesAsync(BibleSource source)
        {
            if (source == null)
            {
                return(new List <Bible>());
            }

            // 캐시가 있으면 사용
            var bibles = GetCached();

            if (bibles.Any())
            {
                return(bibles);
            }
            // 캐시가 없으면 온라인에서 가져와서 저장
            bibles = await source.GetBiblesOnlineAsync().ConfigureAwait(false);

            bibles.ForEach(bible => LinkSource(bible, source));
            Cache();
            return(bibles);

            List <Bible> GetCached()
            {
                using var scope = ScopeFactory.CreateScope();
                var db = scope.ServiceProvider.GetRequiredService <BibleContext>();

                return(db.Bibles.Where(i => i.SourceId == source.Id).ToList());
            }

            void Cache()
            {
                using var scope = ScopeFactory.CreateScope();
                var db = scope.ServiceProvider.GetRequiredService <BibleContext>();

                db.Bibles.AddRange(bibles);
                db.SaveChanges();
            }
        }