I am trying to separate my logic for better readability and re-useability. I am trying to build a better application.
I will show you a sample of a working controller and model and show where I am trying to go.
This is where i am before the conversion.
private IOAuthCredentials credentials = new SessionStateCredentials();
private MvcAuthorizer auth;
private TwitterContext twitterCtx;
public ActionResult Twitter()
{
if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
{
credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
credentials.OAuthToken = ConfigurationManager.AppSettings["twitterOAuthToken"];
credentials.AccessToken = ConfigurationManager.AppSettings["twitterAccessToken"];
}
auth = new MvcAuthorizer
{
Credentials = credentials
};
auth.CompleteAuthorization(Request.Url);
if (!auth.IsAuthorized)
{
Uri specialUri = new Uri(Request.Url.ToString());
return auth.BeginAuthorization(specialUri);
}
twitterCtx = new TwitterContext(auth);
List<TweetViewModel> friendTweets = (from tweet in twitterCtx.Status
where tweet.Type == StatusType.Friends
select new TweetViewModel
{
ImageUrl = tweet.User.ProfileImageUrl,
ScreenName = tweet.User.Identifier.ScreenName,
Tweet = tweet.Text
})
.ToList();
return View(friendTweets);
here is the class
public class TweetViewModel
{
/// <summary>
/// User's avatar
/// </summary>
public string ImageUrl { get; set; }
/// <summary>
/// User's Twitter name
/// </summary>
public string ScreenName { get; set; }
/// <summary>
/// Text containing user's tweet
/// </summary>
public string Tweet { get; set; }
}
I made a datacontext folder and put a dataclass in
here is the class
public class TwitterFriend
{
private MvcAuthorizer auth;
public List<TweetViewModel> GetFriends()
{
// private MvcAuthorizer auth;
using (var twitterCtx = new TwitterContext(auth))
{
var friendTweets = (from tweet in twitterCtx.Status
where tweet.Type == StatusType.Friends
select new TweetViewModel
{
ImageUrl = tweet.User.ProfileImageUrl,
ScreenName = tweet.User.Identifier.ScreenName,
Tweet = tweet.Text
})
.ToList();
return friendTweets;
}
}
Then I tried to make a List method to instantiate the list (does not work)
public List<TweetViewModel> GetFriendTweets()
{
List<TweetViewModel> friends = (List<TweetViewModel>)(new TwitterFriend());
// friends.ToList();
return friends.ToList();
}
}
Then I would put the pull the list from the method
Getfriends():
Sorry if I pasted alot of stuff, I am trying to design and make a proper app where I dont have to redo my logic all over because I knew I could hit these pitfalls.
Can i get some help fixing this. I dont think this is a complicated scenario.
The amended answer for others if they need help is below! Thank you!!
Just the dataclass
public List<TweetViewModel> GetFriends()
// public List<Status> GetFriends()
{
using (var twitterCtx = new TwitterContext(_auth))
{
// List<Type>
List<Status> friendTweets = (from tweet in twitterCtx.Status
where tweet.Type == StatusType.Friends
select tweet).ToList();
List<TweetViewModel> friends = new List<TweetViewModel>();
foreach (Status item in friendTweets)
{
TweetViewModel search2 = new TweetViewModel();
{
search2.ImageUrl = item.User.ProfileImageUrl;
search2.ScreenName = item.User.Identifier.ScreenName;
search2.Tweet = item.Text;
}
friends.Add(search2);
}
return friends.ToList();
Add a constructor to Twitter friend class where you inject the MVCAuthorizer.
public class TwitterFriendService{
public List GetFriends()
{
var friendTweets = (from tweet in twitterCtx.Status
where tweet.Type == StatusType.Friends
select new TweetViewModel
{
ImageUrl = tweet.User.ProfileImageUrl,
ScreenName = tweet.User.Identifier.ScreenName,
Tweet = tweet.Text
})
.ToList();
}
and then on the controller:
}
Hope this helps