Linq / mvc4中查询逻辑的构建与分离

时间:2012-03-21 22:15:02

标签: linq twitter linq-to-objects datacontext asp.net-mvc-4

我试图将我的逻辑分开以获得更好的可读性和可重用性。我正在努力构建一个更好的应用程序。

我将向您展示一个工作控制器和模型的示例,并显示我想要去的地方。

这是我在转换之前的位置。

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

这是班级

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

我创建了一个datacontext文件夹并将数据类放在

这是班级

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

然后我尝试使用List方法来实例化列表(不起作用)

    public List<TweetViewModel> GetFriendTweets()
    {
        List<TweetViewModel> friends = (List<TweetViewModel>)(new TwitterFriend());
        // friends.ToList();

        return friends.ToList();
    }
}

然后我会从方法中拉出列表

Getfriends():

很抱歉,如果我粘贴了很多东西,我正在尝试设计并制作一个合适的应用程序,我不需要重新编写逻辑,因为我知道我可以解决这些陷阱。

我可以帮忙解决这个问题。我不认为这是一个复杂的场景。

如果他们需要帮助,其他人的修正答案如下!谢谢!!

只是数据类

      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 个答案:

答案 0 :(得分:0)

将一个构造函数添加到您注入MVC​​Authorizer的Twitter好友类中。

公共类TwitterFriendService {

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

_auth = auth;
}

公开列表GetFriends()     {

    using (var twitterCtx = new TwitterContext(_auth))
    {

var friendTweets =(来自twitterCtx.Status中的推文                                          其中tweet.Type == StatusType.Friends                                          选择新的TweetViewModel                                          {                                              ImageUrl = tweet.User.ProfileImageUrl,                                              ScreenName = tweet.User.Identifier.ScreenName,                                              Tweet = tweet.Text                                          })     .ToList();

    return friendTweets;
 }

}

然后在控制器上:

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

}

希望这有帮助