async everything

This commit is contained in:
Tommy Parnell
2015-10-05 20:45:47 -04:00
parent a1199fe9fe
commit c9009b700f
10 changed files with 80 additions and 31 deletions

View File

@@ -2,16 +2,16 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DotNetMashup.Web.Factories;
using DotNetMashup.Web.Repositories;
using Microsoft.AspNet.Mvc;
namespace DotNetMashup.Web.Controllers
{
public class HomeController : Controller
{
private readonly BlogPostFactory factory;
private readonly BlogPostRepository factory;
public HomeController(BlogPostFactory factory)
public HomeController(BlogPostRepository factory)
{
this.factory = factory;
}

View File

@@ -12,7 +12,9 @@ namespace DotNetMashup.Web.Extensions
/// Truncates a string containing HTML to a number of text characters, keeping whole words.
/// The result contains HTML and any tags left open are closed.
/// </summary>
/// <param name="s"></param>
/// <param name="html">todo: describe html parameter on TruncateHtml</param>
/// <param name="maxCharacters">todo: describe maxCharacters parameter on TruncateHtml</param>
/// <param name="trailingText">todo: describe trailingText parameter on TruncateHtml</param>
/// <returns></returns>
public static string TruncateHtml(this string html, int maxCharacters, string trailingText)
{
@@ -90,7 +92,8 @@ namespace DotNetMashup.Web.Extensions
/// Truncates a string containing HTML to a number of text characters, keeping whole words.
/// The result contains HTML and any tags left open are closed.
/// </summary>
/// <param name="s"></param>
/// <param name="html">todo: describe html parameter on TruncateHtml</param>
/// <param name="maxCharacters">todo: describe maxCharacters parameter on TruncateHtml</param>
/// <returns></returns>
public static string TruncateHtml(this string html, int maxCharacters)
{
@@ -100,7 +103,7 @@ namespace DotNetMashup.Web.Extensions
/// <summary>
/// Strips all HTML tags from a string
/// </summary>
/// <param name="s"></param>
/// <param name="html">todo: describe html parameter on StripHtml</param>
/// <returns></returns>
public static string StripHtml(this string html)
{
@@ -115,7 +118,6 @@ namespace DotNetMashup.Web.Extensions
/// </summary>
/// <param name="text"></param>
/// <param name="maxCharacters"></param>
/// <param name="trailingText"></param>
/// <returns></returns>
public static string Truncate(this string text, int maxCharacters)
{
@@ -142,7 +144,6 @@ namespace DotNetMashup.Web.Extensions
/// </summary>
/// <param name="text"></param>
/// <param name="maxCharacters"></param>
/// <param name="trailingText"></param>
/// <returns></returns>
public static string TruncateWords(this string text, int maxCharacters)
{

View File

@@ -5,7 +5,7 @@ using System.Threading.Tasks;
namespace DotNetMashup.Web.Model
{
public interface IFeedData
public interface IMetaData
{
string FeedUrl { get; set; }
string Author { get; set; }

View File

@@ -1,7 +1,7 @@
namespace DotNetMashup.Web.Model
{
//stolen idea from: https://github.com/NancyFx/Nancy.Blog/blob/master/src/Nancy.Blog/Model/MetaData.cs
public class MetaData : IFeedData
public class MetaData : IMetaData
{
public string FeedUrl { get; set; }
public string Author { get; set; }

View File

@@ -10,9 +10,9 @@ using DotNetMashup.Web.Global;
using DotNetMashup.Web.Model;
using Microsoft.Framework.Caching.Memory;
namespace DotNetMashup.Web.Factories
namespace DotNetMashup.Web.Repositories
{
public class BlogPostFactory : IFactory
public class BlogPostRepository : IRepository
{
private readonly ISiteSetting setting;
@@ -20,7 +20,7 @@ namespace DotNetMashup.Web.Factories
private readonly IEnumerable<MetaData> _data;
private const string cacheKey = "blogposts";
public BlogPostFactory(IEnumerable<MetaData> data, IMemoryCache cache, ISiteSetting setting)
public BlogPostRepository(IEnumerable<MetaData> data, IMemoryCache cache, ISiteSetting setting)
{
this._data = data;
this.cache = cache;
@@ -35,11 +35,11 @@ namespace DotNetMashup.Web.Factories
}
}
public IEnumerable<IExternalData> GetData()
public async Task<IEnumerable<IExternalData>> GetData()
{
var cachedata = cache.Get<IEnumerable<IExternalData>>(cacheKey);
if(cachedata != null) return cachedata;
var syndicationFeeds = GetSyndicationFeeds(_data);
var syndicationFeeds = await GetSyndicationFeeds(_data);
var data = syndicationFeeds
.SelectMany(pair => pair.Value.Items, (pair, item) => new { Id = pair.Key, Item = item })
@@ -105,35 +105,40 @@ namespace DotNetMashup.Web.Factories
return data;
}
private static IEnumerable<KeyValuePair<string, SyndicationFeed>> GetSyndicationFeeds(IEnumerable<MetaData> metadataEntries)
private async static Task<IEnumerable<KeyValuePair<string, SyndicationFeed>>> GetSyndicationFeeds(IEnumerable<IMetaData> metadataEntries)
{
var syndicationFeeds = new List<KeyValuePair<string, SyndicationFeed>>();
foreach(var metadata in metadataEntries)
{
GetFeed(metadata.FeedUrl, metadata.Id, syndicationFeeds);
syndicationFeeds.AddRange(await GetFeed(metadata.FeedUrl, metadata.Id, syndicationFeeds));
}
return syndicationFeeds;
}
private static void GetFeed(string url, string id, List<KeyValuePair<string, SyndicationFeed>> syndicationFeeds)
private async static Task<List<KeyValuePair<string, SyndicationFeed>>> GetFeed(string url, string id, List<KeyValuePair<string, SyndicationFeed>> syndicationFeeds)
{
var feeds = new List<KeyValuePair<string, SyndicationFeed>>();
try
{
SyndicationFeed feed = null;
using(var reader = XmlReader.Create(url))
{
feed = SyndicationFeed.Load(reader);
}
await Task.Run(() => {
using(var reader = XmlReader.Create(url))
{
feed = SyndicationFeed.Load(reader);
}
});
if(feed != null)
{
syndicationFeeds.Add(new KeyValuePair<string, SyndicationFeed>(id, feed));
feeds.Add(new KeyValuePair<string, SyndicationFeed>(id, feed));
if(feed.Links.Any(x => x.RelationshipType == "next"))
{
foreach(var pagingLink in feed.Links.Where(x => x.RelationshipType == "next"))
{
GetFeed(pagingLink.Uri.AbsoluteUri, id, syndicationFeeds);
feeds.AddRange(await GetFeed(pagingLink.Uri.AbsoluteUri, id, syndicationFeeds));
}
}
}
@@ -146,6 +151,8 @@ namespace DotNetMashup.Web.Factories
{
//Unable to load RSS feed
}
return feeds;
}
}
}

View File

@@ -4,12 +4,12 @@ using System.Linq;
using System.Threading.Tasks;
using DotNetMashup.Web.Model;
namespace DotNetMashup.Web.Factories
namespace DotNetMashup.Web.Repositories
{
public interface IFactory
public interface IRepository
{
string FactoryName { get; }
IEnumerable<IExternalData> GetData();
Task<IEnumerable<IExternalData>> GetData();
}
}

View File

@@ -0,0 +1,26 @@
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Threading.Tasks;
//using DotNetMashup.Web.Model;
//using Tweetinvi;
//namespace DotNetMashup.Web.Repositories
//{
// public class TwitterRepository : IRepository
// {
// public string FactoryName
// {
// get
// {
// return "Twitter";
// }
// }
// public Task<IEnumerable<IExternalData>> GetData()
// {
// throw new NotImplementedException();
// var matchingTweets = Search.SearchTweets();
// }
// }
//}

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.IO;
using DotNetMashup.Web.Factories;
using DotNetMashup.Web.Repositories;
using DotNetMashup.Web.Global;
using DotNetMashup.Web.Model;
using Microsoft.AspNet.Builder;
@@ -42,7 +42,7 @@ namespace DotNetMashup.Web
return new MemoryCache(new MemoryCacheOptions());
});
services.AddInstance(_feedData);
services.AddSingleton<BlogPostFactory>();
services.AddSingleton<BlogPostRepository>();
// Add MVC services to the services container.
services.AddMvc();

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DotNetMashup.Web.Model;
namespace DotNetMashup.Web.ViewModel
{
public class MashupViewModel
{
public string Header { get; set; }
public IEnumerable<IExternalData> posts { get; set; }
}
}

View File

@@ -15,7 +15,8 @@
"Microsoft.Framework.Logging.Console": "1.0.0-beta7",
"Microsoft.Framework.Logging.Debug": "1.0.0-beta7",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-beta7",
"Newtonsoft.Json": "7.0.1"
"Newtonsoft.Json": "7.0.1",
"TweetinviAPI": "0.9.9.7"
},
"commands": {
@@ -23,7 +24,7 @@
},
"frameworks": {
"dnx451": {
"dnx46": {
"frameworkAssemblies": {
"System.ServiceModel": "4.0.0.0"
}