Click here to Skip to main content
15,881,173 members
Articles / Web Development / CSS

Building a Web Message Board using Visual Studio 2008, Part I - The Basic Message Board

Rate me:
Please Sign up or sign in to vote.
4.90/5 (83 votes)
30 Dec 2007CPOL47 min read 374.9K   3.7K   333  
This article builds a web based message board and uses several new technologies introduced with Visual Studio 2008 such as LINQ, WCF Web Programming, WCF Syndication, ASP.NET ListView, ASP.NET DataPager etc.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace MessageBoard.DataAccess.Linq
{
    public class LinqMessageProvider : IMessageProvider
    {
        #region IMessageSource Members

        public IEnumerable<Message> GetRecentMessages(int lastId, int start, int count)
        {
            using (MessageBoardDataContext context = CreateDataContext())
            {
                var messages = from m in context.Messages
                               where m.Id > lastId
                               orderby m.DatePosted descending
                               select m.Freeze();
                
                var messagesInRange = messages.Skip(start).Take(count);
                
                return messagesInRange.ToList();
            }
         }

        public int GetMessageCount()
        {
            using(MessageBoardDataContext context = CreateDataContext())
                return context.Messages.Count();
        }

        private static MessageBoardDataContext CreateDataContext()
        {
            MessageBoardDataContext context = new MessageBoardDataContext();
            context.ObjectTrackingEnabled = false;

            return context;
        }

        public int AddMessage(string subject, string text, string postedBy, string postedById, DateTime datePosted)
        {
            using (MessageBoardDataContext context = CreateDataContext())
            {
                context.ObjectTrackingEnabled = true;

                Message message = new Message();
                message.Subject = subject;
                message.Text = text;
                message.PostedBy = postedBy;
                message.PostedById = postedById;
                message.DatePosted = datePosted;
                context.Messages.InsertOnSubmit(message);

                context.SubmitChanges();

                //After calling submit changes the Id is automatically updated
                return message.Id;
            }
        }

        public IEnumerable<Message> GetMessageById(int id)
        {
            using (MessageBoardDataContext context = CreateDataContext())
            {
                return (from m in context.Messages
                       where m.Id == id
                       select m).ToList();
            }
        }
        
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions