private AdViewModel GetAdModel(string subverse, bool useExisting = false) { var linkToAdPurchase = String.Format("[Want to advertize on {1}?]({0})", Url.Action("Advertize", "Home"), VoatSettings.Instance.SiteName); //Default add var adToDisplay = new AdViewModel { Name = $"Advertize on {VoatSettings.Instance.SiteName}", DestinationUrl = Url.Action("Advertize", "Home"), Description = linkToAdPurchase, GraphicUrl = Url.Content("~/images/voat-ad-placeholder.png") }; try { using (var db = new VoatOutOfRepositoryDataContextAccessor()) { var ad = (from x in db.Ad where ((subverse != null && subverse.ToLower() == subverse.ToLower() || (x.Subverse == null))) && (x.EndDate >= Repository.CurrentDate && x.StartDate <= Repository.CurrentDate) && x.IsActive orderby x.Subverse descending select x).FirstOrDefault(); if (ad != null) { adToDisplay = new AdViewModel { Name = ad.Name, DestinationUrl = ad.DestinationUrl, Description = String.Format("{0}\n\n\n\n{1}", ad.Description, linkToAdPurchase), GraphicUrl = ad.GraphicUrl }; } else if (useExisting) { { var ads = CacheHandler.Instance.Register(CachingKey.AdCache(), new Func <IList <Ad> >(() => { using (var dbcontext = new VoatOutOfRepositoryDataContextAccessor()) { var adCache = (from x in dbcontext.Ad where x.Subverse == null && x.IsActive select x).ToList(); return(adCache); } }), TimeSpan.FromMinutes(60)); if (ads != null && ads.Count > 0) { //pick random index Random m = new Random(); var index = m.Next(0, ads.Count - 1); ad = ads[index]; adToDisplay = new AdViewModel { Name = ad.Name, DestinationUrl = ad.DestinationUrl, Description = String.Format("{0}\n\n\n\n{1}", ad.Description, linkToAdPurchase), GraphicUrl = ad.GraphicUrl }; } } } } } catch (Exception ex) { /*no-op - ensure that ads don't throw exceptions */ } return(adToDisplay); }