Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7679101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:47:57+00:00 2026-05-31T17:47:57+00:00

I am trying to separate my logic for better readability and re-useability. I am

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:47:58+00:00Added an answer on May 31, 2026 at 5:47 pm

    Add a constructor to Twitter friend class where you inject the MVCAuthorizer.

    public class TwitterFriendService{

    private readonly MvcAuthorizer _auth;
    public TwitterFriend(MvcAuthorizer auth){
    
    _auth = auth;
    }
    

    public List GetFriends()
    {

        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;
     }
    

    }

    and then on the controller:

    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);
          }
    
    
    
        TwitterFriendService twitterFriendService = new TwitterFriendService(auth);
        List<TweetViewModel> friendTweets = twitterFriendService.GetFriends();
    
           return View(friendTweets);
    

    }

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to separate some asp logic out into a separate page. For
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
In my rails3.1 application, I'm trying to apply the following logic in one of
I'm trying to separate my presentation and logic as much as I can, but
I'm trying to show information about user in my Zend Framework application. I'm using
I´m trying to separate a gridViewColumn into two rows. Im using default style for
I trying to set separate style for particular widgets, like for one button with
trying to implement a dialog-box style behaviour using a separate div section with all
I'm trying to write a library to separate all the disk activity out into
I'm trying to get crawl to work on two separate farms I have but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.