Click here to Skip to main content
15,886,737 members
Articles / Web Development / XHTML

An ASP.NET Data Layer Base Class for LINQ Disconnected Mode

Rate me:
Please Sign up or sign in to vote.
4.73/5 (21 votes)
14 Mar 2009CPOL6 min read 81K   1.1K   68  
Quickly and easily implement your LINQ Data Layer with this abstract class
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;

namespace Vandermotten.Diagnostics
{
    /// <summary>
    /// Implements a <see cref="TextWriter"/> for writing information to the debugger log.
    /// </summary>
    /// <seealso cref="Debugger.Log"/>
    public class DebuggerWriter : TextWriter
    {
        private static UnicodeEncoding encoding;
        private readonly string category;
        private readonly int level;
        private bool isOpen;

        /// <summary>
        /// Initializes a new instance of the <see cref="DebuggerWriter"/> class.
        /// </summary>
        public DebuggerWriter()
            : this(0, Debugger.DefaultCategory)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DebuggerWriter"/> class with the specified level and category.
        /// </summary>
        /// <param name="level">A description of the importance of the messages.</param>
        /// <param name="category">The category of the messages.</param>
        public DebuggerWriter(int level, string category)
            : this(level, category, CultureInfo.CurrentCulture)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DebuggerWriter"/> class with the specified level, category and format Repository.
        /// </summary>
        /// <param name="level">A description of the importance of the messages.</param>
        /// <param name="category">The category of the messages.</param>
        /// <param name="formatRepository">An <see cref="IFormatRepository"/> object that controls formatting.</param>
        public DebuggerWriter(int level, string category, IFormatProvider formatRepository)
            : base(formatRepository)
        {
            this.level = level;
            this.category = category;
            isOpen = true;
        }

        public override Encoding Encoding
        {
            get
            {
                if (encoding == null)
                {
                    encoding = new UnicodeEncoding(false, false);
                }
                return encoding;
            }
        }

        public int Level
        {
            get { return level; }
        }

        public string Category
        {
            get { return category; }
        }

        protected override void Dispose(bool disposing)
        {
            isOpen = false;
            base.Dispose(disposing);
        }

        public override void Write(char value)
        {
            if (!isOpen)
            {
                throw new ObjectDisposedException(null);
            }
            Debugger.Log(level, category, value.ToString());
        }

        public override void Write(string value)
        {
            if (!isOpen)
            {
                throw new ObjectDisposedException(null);
            }
            if (value != null)
            {
                Debugger.Log(level, category, value);
            }
        }

        public override void Write(char[] buffer, int index, int count)
        {
            if (!isOpen)
            {
                throw new ObjectDisposedException(null);
            }
            if (buffer == null || index < 0 || count < 0 || buffer.Length - index < count)
            {
                base.Write(buffer, index, count); // delegate throw exception to base class
            }
            Debugger.Log(level, category, new string(buffer, index, count));
        }
    }
}

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
Chief Technology Officer Lobstersoft
Germany Germany
Adrian Grigore holds a German diploma in Computer sciences from the University of Würzburg, Germany.

He founded Lobstersoft early during his studies and has since then released several commercially successful game titles. All of these games were created using Adrian's custom build game engine.

In addition to object-oriented software design in C+ and C#, Adrian's main areas of expertise are ASP.NET, Perl and XML/XSLT.

Apart from leading Lobstersoft, Adrian also works as a freelance software development consultant.

Comments and Discussions