Beispiel #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMemoryCache(options => {
                options.SizeLimit = this.Configuration.GetValue <long?>("MemCache:SizeLimit", null);
            });

            ILyricsGenerator lyricsGenerator =
                this.Configuration.GetValue <string>("Generator", null) == "dummy"
                ? new DummyLyrics()
                : this.CreateGradientLyrics();

            CheckGeneratorSanity(lyricsGenerator);
            services.AddSingleton(lyricsGenerator);

            services.Configure <IdentityOptions>(options => {
                options.Password.RequiredUniqueChars    = 4;
                options.Password.RequiredLength         = 5;
                options.Password.RequireDigit           = false;
                options.Password.RequireLowercase       = options.Password.RequireUppercase = false;
                options.Password.RequireNonAlphanumeric = false;
            });

            services.Configure <CookiePolicyOptions>(options => {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext <ApplicationDbContext>(this.ConfigureDbContext);
            services.AddDefaultIdentity <SongsUser>()
            .AddDefaultUI()
            .AddEntityFrameworkStores <ApplicationDbContext>();

            services.AddScoped <ISongDatabase, CachedSongDatabase>();

            services.AddSingleton(sp => {
                var scopedProvider = sp.CreateScope().ServiceProvider;
                return(SongVoteCache.Load(scopedProvider.GetService <ApplicationDbContext>().Votes));
            });
            services.AddSingleton(sp => {
                var scopedProvider = sp.CreateScope().ServiceProvider;
                return(new PregeneratedSongProvider(
                           scopedProvider.GetService <ISongDatabase>(),
                           scopedProvider.GetService <ApplicationDbContext>().Songs,
                           sp.GetService <ILogger <PregeneratedSongProvider> >(),
                           sp.GetService <IHostApplicationLifetime>().ApplicationStopping));
            });
            services.AddSingleton <IRandomSongProvider>(sp => new RandomSongProviderCombinator(
                                                            new WeightedRandom <IRandomSongProvider>(
                                                                new Dictionary <IRandomSongProvider, int> {
                [sp.GetRequiredService <PregeneratedSongProvider>()] = 3,
                [new TopSongWeightedProvider(sp.GetRequiredService <SongVoteCache>())] = 1,
            }
                                                                ), logger: sp.GetService <ILogger <RandomSongProviderCombinator> >()));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
        }
        public static SongVoteCache Load(DbSet <SongVote> votes)
        {
            var result = new SongVoteCache();

            foreach (var vote in votes.AsNoTracking())
            {
                if (vote.Upvote)
                {
                    result.AddUpvotes(vote.SongID, 1);
                }
                else
                {
                    result.AddDownvotes(vote.SongID, 1);
                }
            }
            return(result);
        }
 public TopSongWeightedProvider(SongVoteCache voteCache)
 {
     this.voteCache = voteCache ?? throw new ArgumentNullException(nameof(voteCache));
 }