Click here to Skip to main content
15,886,724 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 375.3K   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.Web;
using System.Configuration;
using System.Web.Security;
using MessageBoard.Properties;

namespace MessageBoard
{
    /// <summary>
    /// This class is used by the Asp.Net Object Data Source
    /// </summary>
    public static class MessageSource
    {
        private static IMessageProvider _actualMessageProvider = CreateMessageProvider();

        private static IMessageProvider CreateMessageProvider()
        {
            string typeName = ConfigurationManager.AppSettings["MessageBoard-MessageProviderType"];
            Type type = Type.GetType(typeName, true);

            return (IMessageProvider)Activator.CreateInstance(type);
        }
                
        public static IEnumerable<Message> GetRecentMessages(int start, int count)
        {
            return GetRecentMessages(0, start, count);
        }

        public static IEnumerable<Message> GetRecentMessages(int idSince, int start, int count)
        {
            return _actualMessageProvider.GetRecentMessages(idSince, start, count);
        }

        public static int GetMessageCount()
        {
            return _actualMessageProvider.GetMessageCount();
        }

        public static void AddMessage(string subject, string text)
        {
            if (String.IsNullOrEmpty(subject))
                throw new ArgumentException(Resources.SubjectEmpty, "subject");

            if (String.IsNullOrEmpty(text))
                throw new ArgumentException(Resources.TextEmpty, "text");            
            
            //Get the current membership user
            MembershipUser user = Membership.GetUser();
            string postedById = String.Empty;
            string postedBy;

            if (user == null)
            {
                postedBy = Resources.Anonymous;
            }
            else
            {
                postedById = user.ProviderUserKey.ToString();
                postedBy = user.UserName;
            }

            _actualMessageProvider.AddMessage(subject, text, postedBy, postedById, DateTime.Now.ToUniversalTime());
        }

        /// <summary>
        /// Finds a message given an Id and returns a collection of 1 element
        /// </summary>
        /// <param name="id">The id of the message</param>
        /// <returns>A collection of 1 element. A collection is returned so that 
        /// end users can easily data bind
        /// </returns>
        public static IEnumerable<Message> GetMessageById(int id)
        {
            return _actualMessageProvider.GetMessageById(id);
        }
    }
}

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