Ejemplo n.º 1
0
        public List<TwitterEntity> FindAll(ActionState actionState)
        {
            List<TwitterEntity> twitterEntityList;
            MongoServer server = null;
            MongoDatabase database = null;
            TwitterEntity entity;

            twitterEntityList = new List<TwitterEntity>();
            entity = null;
            try
            {
                server = MongoServer.Create(ConfigurationManager.AppSettings[CommonConstants.ConnictionString]);
                database = server.GetDatabase(CommonConstants.DatabaseName);
                MongoCollection<BsonDocument> tweet = database.GetCollection<BsonDocument>(TwitterConstants.TweetsCollection);
                foreach (BsonDocument tweetEntity in tweet.FindAll())
                {
                    entity = new TwitterEntity();
                    entity.ID = tweetEntity[TwitterConstants.ID].ToString();
                    entity.Name = tweetEntity[TwitterConstants.Name].AsString;
                    entity.ScreenName = tweetEntity[TwitterConstants.ScreenName].AsString;
                    entity.CreatedDate = tweetEntity[TwitterConstants.CreatedDate].AsDateTime;
                    entity.Tweet = tweetEntity[TwitterConstants.Tweets].AsString;
                    entity.UserID = tweetEntity[TwitterConstants.UserID].AsString;
                    twitterEntityList.Add(entity);
                }
                actionState.SetSuccess();
            }
            catch (Exception ex)
            {
                actionState.SetFail(ActionStateEnum.Exception, ex.Message);
            }
            finally
            {
                server.Disconnect();
                server = null;
                database = null;
            }
            return twitterEntityList;
        }
Ejemplo n.º 2
0
        public void Insert(TwitterEntity entity, ActionState actionState)
        {
            MongoServer server = null;
            MongoDatabase database = null;
            try
            {
                server = MongoServer.Create(ConfigurationManager.AppSettings[CommonConstants.ConnictionString]);
                database = server.GetDatabase(CommonConstants.DatabaseName);
                var dataCollection = database.CollectionExists(TwitterConstants.TweetsCollection);
                if (Convert.ToBoolean(dataCollection) == false)
                {
                    database.CreateCollection(TwitterConstants.TweetsCollection);
                    MongoCollection<BsonDocument> tweet = database.GetCollection<BsonDocument>(TwitterConstants.TweetsCollection);
                    BsonDocument tweetEntity = new BsonDocument {
                { TwitterConstants.Name, entity.Name },
                { TwitterConstants.ScreenName, entity.ScreenName },
                { TwitterConstants.Tweets, entity.Tweet },
                { TwitterConstants.UserID, entity.UserID },
                { TwitterConstants.CreatedDate, entity.CreatedDate }
                };
                    tweet.Insert(tweetEntity);
                    actionState.SetSuccess();
                }
                else
                {
                    MongoCollection<BsonDocument> tweet = database.GetCollection<BsonDocument>(TwitterConstants.TweetsCollection);
                    BsonDocument tweetEntity = new BsonDocument {
                { TwitterConstants.Name, entity.Name },
                { TwitterConstants.ScreenName, entity.ScreenName },
                { TwitterConstants.Tweets, entity.Tweet },
                { TwitterConstants.UserID, entity.UserID },
                { TwitterConstants.CreatedDate, entity.CreatedDate }
                };
                    tweet.Insert(tweetEntity);
                    actionState.SetSuccess();

                }
            }
            catch (Exception ex)
            {
                actionState.SetFail(ActionStateEnum.Exception, ex.Message);
            }
            finally
            {
                server.Disconnect();
                server = null;
                database = null;

            }
        }