Does your company or project have certain conventions that you are required to follow? Maybe adding a copyright header to each file?  Or using the correct layer super-type for all your controllers, context, etc?  I work on a *lot* of projects these days, and I often forget to follow some of these conventions in my day-to-day coding.  Fortunately, I have test cases that automatically check these conventions, and I get immediate feedback when I fail to follow them.

[more]

Copyright Headers

Here’s a simple NUnit spec you can use to make sure all the C# source files in your project have the correct copyright header:

    [TestFixture]
    public class AllFilesShouldHaveCopyrightHeader
    {
        private const string CopyrightHeader = "// Copyright (c) Heroic Apps. All rights reserved.";

        private string[] _filesWithoutHeaders;

        public AllFilesShouldHaveCopyrightHeader()
        {
            //You're in /bin/debug
            var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
            //Traverse up to the solution root... PFP FTW!
            var solutionRoot = currentDirectory.Parent.Parent.Parent;

            _filesWithoutHeaders = solutionRoot.GetFiles("*.cs", SearchOption.AllDirectories)
                .Where(f => !f.FullName.Contains(@"obj\Debug")) //Skip temporary files
                .Where(f => File.ReadLines(f.FullName).FirstOrDefault() != CopyrightHeader)
                .Select(f => f.FullName)
                .ToArray();
        }

        [Test]
        public void all_csharp_source_code_files_should_contain_the_appropriate_copyright_header()
        {
            if (_filesWithoutHeaders.Any())
            {
                Assert.Fail("The following files are missing the required copyright header: \r\n\t" +
                            string.Join("\r\n\t", _filesWithoutHeaders));
            }
        }
    }

Correct Base Class

You can do something similar to check that your layer types (such as Entity Framework contexts or MVC controllers) derive from the correct base type.  Here’s an example, this time using SpecsFor:

    public class EFContextConventions : SpecsFor<object>
    {
        private Type[] _contextTypes;

        protected override void When()
        {
            _contextTypes = Assembly.GetExecutingAssembly().GetReferencedAssemblies()
                .Where(n => n.Name.StartsWith("YourAssemblyPrefix"))
                .Select(n => Assembly.Load(n))
                .SelectMany(a => a.GetExportedTypes())
                .Where(x => x.CanBeCastTo<DbContext>() && !x.IsAbstract)
                .ToArray();
        }

        [Test]
        public void all_context_classes_should_derive_from_correct_base()
        {
            var badTypes = _contextTypes.Where(x => !IsDerivedFromCorrectContextBase(x))
                .ToArray();

            if (badTypes.Any())
            {
                var message = "The following context classes should be changed to derive from " + typeof(HeroicContext<>).FullName + ":\r\n\t" +
                    string.Join("\r\n\t", badTypes.Select(x => x.FullName));

                Assert.Fail(message);
            }
        }

        private static bool IsDerivedFromCorrectContextBase(Type x)
        {
            return x.BaseType != null &&
                   x.BaseType.IsGenericType &&
                   x.BaseType.GetGenericTypeDefinition() == typeof (HeroicContext<>);
        }
    }

There are other ways to enforce conventions, but I’ve found that creating test cases for them is simple and effective.  Whatever you do to enforce conventions, make sure it’s as automated as possible!