59

It's been discussed before on Stack Overflow that we should prefer attributes to marker interfaces (interfaces without any members). Interface Design article on MSDN asserts this recommendation too:

Avoid using marker interfaces (interfaces with no members).

Custom attributes provide a way to mark a type. For more information about custom attributes, see Writing Custom Attributes. Custom attributes are preferred when you can defer checking for the attribute until the code is executing. If your scenario requires compile-time checking, you cannot comply with this guideline.

There's even an FxCop rule to enforce this recommendation:

Avoid empty interfaces

Interfaces define members that provide a behavior or usage contract. The functionality described by the interface can be adopted by any type, regardless of where the type appears in the inheritance hierarchy. A type implements an interface by providing implementations for the interface's members. An empty interface does not define any members, and as such, does not define a contract that can be implemented.

If your design includes empty interfaces that types are expected to implement, you are probably using an interface as a marker, or a way of identifying a group of types. If this identification will occur at runtime, the correct way to accomplish this is to use a custom attribute. Use the presence or absence of the attribute, or the attribute's properties, to identify the target types. If the identification must occur at compile time, then using an empty interface is acceptable.

The article states only one reason that you might ignore the warning: when you need compile time identification for types. (This is consistent with the Interface Design article).

It is safe to exclude a warning from this rule if the interface is used to identify a set of types at compile-time.

Here comes the actual question: Microsoft didn't conform to their own recommendation in the design of the Framework Class Library (at least in a couple cases): IRequiresSessionState interface and IReadOnlySessionState interface. These interfaces are used by the ASP.NET framework to check whether it should enable session state for a specific handler or not. Obviously, it's not used for compile time identification of types. Why they didn't do that? I can think of two potential reasons:

  1. Micro-optimization: Checking whether an object implements an interface (obj is IReadOnlySessionState) is faster than using reflection to check for an attribute (type.IsDefined(typeof(SessionStateAttribute), true)). The difference is negligible most of the time but it might actually matter for a performance-critical code path in the ASP.NET runtime. However, there are workarounds they could have used like caching the result for each handler type. The interesting thing is that ASMX Web services (which are subject to similar performance characteristics) actually use the EnableSession property of the WebMethod attribute for this purpose.

  2. Implementing interfaces are potentially more likely to be supported than decorating types with attributes by third-party .NET languages. Since ASP.NET is designed to be language agnostic, and ASP.NET generates code for types (possibly in a third-party language with the help of CodeDom) that implement the said interfaces based on the EnableSessionState attribute of the <%@ Page %> directive, it might make more sense to use interfaces instead of attributes.

What are the persuasive reasons to use marker interfaces instead of attributes?

Is this simply a (premature?) optimization or a tiny mistake in the framework design? (Do they think reflection is a "big monster with red eyes"?) Thoughts?

4
  • 3
    At the risk of sounding too snarky, what has Microsoft ever done to make you expect consistency from them? They've provided a lot of useful tools and software over the years, but the one thing I've never seen from them is consistent behavior, or adherence to their own guidelines or rules.
    – jalf
    Jan 18, 2010 at 14:21
  • 5
    @jalf: I agree they are notorious for not adhering to their own guidelines but to be fair, they have done a good job in .NET. .NET Framework exhibits a huge amount of consistency and it's generally very well designed. Jan 18, 2010 at 14:25
  • 1
    generally very well designed, yes, and unusually consistent, but not perfectly so. There are plenty of silly little design mistakes that are obvious in retrospect. But as Mark's answer says, these issues probably weren't as obvious while the BCL was being designed -- and so neither were the guideline recommendations.
    – jalf
    Jan 18, 2010 at 14:27
  • 5
    @jalf: sometimes the design pre-dates the guideline recommendation.
    – SergioL
    Jan 18, 2010 at 20:43

6 Answers 6

16

I generally avoid "marker interfaces" because they don't allow you to unmark a derived type. But that aside, here are some of the specific cases that I have seen where marker interfaces would be preferable to built-in meta-data support:

  1. Run-time performance sensitive situations.
  2. Compatibility with languages that don't support annotation or attributes.
  3. Any context where the interested code may not have access to the metadata.
  4. Support for generic constraints and generic variance (typically of collections).
1
  • 2
    I would suggest that the fact that marker interfaces cannot be "unmarked" in derived types is often a compelling reason to use them. If an object passed to a method must have some characteristic which is defined via marker interface, then theMethod<T>(T it) where T:IHasAttribute will enforce that at compile time. By contrast, even one knows that Fred has an attribute, theMethod(Fred it) could be passed an object that didn't have that attribute, failing at run-time. That having been said, I think most marker-ish interfaces should inherit at least one other interface.
    – supercat
    May 22, 2013 at 20:05
12

For a generic type you might want to use the same generic parameter in a marker interface. This is not achievable by an attribute:

interface MyInterface<T> {}

class MyClass<T, U> : MyInterface<U> {}

class OtherClass<T, U> : MyInterface<IDictionary<U, T>> {}

This kind of interface might be useful to associate a type with another one.

Another good use for a marker interface is when you want to create a kind of mixin:

interface MyMixin {}

static class MyMixinMethods {
  public static void Method(this MyMixin self) {}
}

class MyClass : MyMixin {
}

The acyclic visitor pattern also uses them. The term "degenerate interface" is sometimes used as well.

UPDATE:

I don't know if this one counts, but I've used them to mark classes for a post-compiler to work on.

13
  • 2
    The chances of being able to effectively implement a Mixin on ANY type that could possible implement MyMixin without any members in the interface is exceedingly low. Jun 24, 2011 at 22:02
  • And I don't understand why the "permanence" of a relationship has anything to with whether or not a marker interface is appropriate for it. Where would you use the IFood interface that an attribute would not be more appropriate? Jun 24, 2011 at 22:03
  • @David Nelson: I agree, it's not really different from using an attribute. But the mixin usage is interesting, I didn't get your comment on it.
    – Jordão
    Jun 24, 2011 at 22:26
  • 1
    @DavidNelson: The set of things which can be done with an object is not determined entirely by its members. Indeed, sometimes it may be useful for an object to promise not to do something. Even though Microsoft fails in its naming to distinguish between things which are readable, read-only, and immutable, marker interfaces to identify the latter two could have been helpful if they'd been standardized. An immutable object's contents may be encapsulated by encapsulating a reference to the immutable object, and a reference to a legitimately read-only object may be safely exposed to code...
    – supercat
    Dec 26, 2014 at 20:56
  • 1
    ...which could not be trusted to refrain from modifying a mutable object. Being able to use or expose objects directly without having to copy their contents or encapsulate them in read-only wrappers would be helpful if a standard means existed to determine which objects could be safely used or exposed in that fashion. Unfortunately, no standard means of identification exists.
    – supercat
    Dec 26, 2014 at 20:59
7

Microsoft didn't strictly follow the guidelines when they made .NET 1.0, because the guidelines evolved together with the framework, and some of the rules they didn't learn until it was too late to change the API.

IIRC, the examples you mention belong to BCL 1.0, so that would explain it.

This is explained in Framework Design Guidelines.


That said, the book also remarks that "[A]ttribute testing is a lot more costly than type checking" (in a sidebar by Rico Mariani).

It goes on to say that sometimes you need the marker interface for compile time checking, which isn't possible with an attribute. However, I find the example given in the book (p. 88) unconvincing, so I will not repeat it here.

5
  • Yes, they are seen breaking their own guidelines a lot of times, but there are usually reasons behind it. The question is, why they didn't follow this specific guideline? Are there any real advantages or simply a design mistake? Jan 18, 2010 at 14:09
  • 2
    I've always assumed that it was just a mistake. I don't have the book with me right now, but IIRC it specifically discusses examples where they used Marker Interfaces because 'they didn't know better'... Jan 18, 2010 at 14:16
  • Now that I have had a chance to look it up in the book, I've added some more information to my answer. Jan 18, 2010 at 18:25
  • Yes, performance is the first point I mentioned (micro-optimization). I'm not convinced by it since ASMX uses it and it's possible to cache it. Compile-time checking is a convincing reason to use interfaces (as noted in the guideline and FxCop rule) but it doesn't apply to this specific case. Did they write anywhere that they say this was a mistake? Jan 18, 2010 at 18:29
  • Well, I didn't reread the entire book, but I looked up the pages pointed to by the index, and they didn't say anything about a mistake in that area. I probably mixed up the examples in my memory. Jan 18, 2010 at 18:47
5

From performance point of view:

Marker attributes will be slower than marker interfaces because of reflection. If you don't cache reflection then calling GetCustomAttributes all the time can be a performance bottleneck. I benchmarked this before and using marker interfaces win in terms of performance even when using cached reflection.

This only applies when you use it in the code that's frequently called.

BenchmarkDotNet=v0.10.14, OS=Windows 10.0.16299.371 (1709/FallCreatorsUpdate/Redstone3)
Intel Core i5-2400 CPU 3.10GHz (Sandy Bridge), 1 CPU, 4 logical and 4 physical cores
Frequency=3020482 Hz, Resolution=331.0730 ns, Timer=TSC
.NET Core SDK=2.1.300-rc1-008673
  [Host] : .NET Core 2.0.7 (CoreCLR 4.6.26328.01, CoreFX 4.6.26403.03), 64bit RyuJIT
  Core   : .NET Core 2.0.7 (CoreCLR 4.6.26328.01, CoreFX 4.6.26403.03), 64bit RyuJIT

Job=Core  Runtime=Core

                     Method |          Mean |      Error |     StdDev | Rank |
--------------------------- |--------------:|-----------:|-----------:|-----:|
                     CastIs |     0.0000 ns |  0.0000 ns |  0.0000 ns |    1 |
                     CastAs |     0.0039 ns |  0.0059 ns |  0.0052 ns |    2 |
            CustomAttribute | 2,466.7302 ns | 18.5357 ns | 17.3383 ns |    4 |
 CustomAttributeWithCaching |    25.2832 ns |  0.5055 ns |  0.4729 ns |    3 |

It's not significant difference though.

namespace BenchmarkStuff
{
    [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
    public class CustomAttribute : Attribute
    {

    }

    public interface ITest
    {

    }

    [Custom]
    public class Test : ITest
    {

    }

    [CoreJob]
    [RPlotExporter, RankColumn]
    public class CastVsCustomAttributes
    {
        private Test testObj;
        private Dictionary<Type, bool> hasCustomAttr;

        [GlobalSetup]
        public void Setup()
        {
            testObj = new Test();
            hasCustomAttr = new Dictionary<Type, bool>();
        }

        [Benchmark]
        public void CastIs()
        {
            if (testObj is ITest)
            {

            }
        }

        [Benchmark]
        public void CastAs()
        {
            var itest = testObj as ITest;
            if (itest != null)
            {

            }
        }

        [Benchmark]
        public void CustomAttribute()
        {
            var customAttribute = (CustomAttribute)testObj.GetType().GetCustomAttributes(typeof(CustomAttribute), false).SingleOrDefault();
            if (customAttribute != null)
            {

            }
        }

        [Benchmark]
        public void CustomAttributeWithCaching()
        {
            var type = testObj.GetType();
            bool hasAttr = false;
            if (!hasCustomAttr.TryGetValue(type, out hasAttr))
            {
                hasCustomAttr[type] = type.CustomAttributes.SingleOrDefault(attr => attr.AttributeType == typeof(CustomAttribute)) != null;
            }
            if (hasAttr)
            {

            }
        }
    }

    public static class Program
    {
        public static void Main(string[] args)
        {
            var summary = BenchmarkRunner.Run<CastVsCustomAttributes>();
        }
    }
}
4

I'm strongly pro Marker Interfaces. I never liked Attributes. I see them as some kind of meta information for classes and members intended for example for debuggers to look at. Similar to Exceptions, they should not influence normal processing logic, in my most humble opinion.

5
  • 1
    Can you elaborate why you think like that in your answer? I mean why do you think attributes and exceptions should not "influence normal processing logic"? (What do you mean by "normal processing logic"? exceptions certainly do influence flow of the program and are not just there for debuggers.) Jan 18, 2010 at 18:13
  • 1
    Okay, I'll try to elaborate a bit. First, .NET Attributes (or Java Annotations) are a relatively new invention, I think there isn't even a standard representation in UML for them. I know they are widely used for AOP and postprocessing now (PostSharp et al), but that only tries to make up for a certain lack in features or too much restrictiveness of the programming language. Jan 18, 2010 at 20:00
  • 1
    By "normal processing logic" I mean the regular "control flow" in an application or system, which is not "exception"al. Some people throw wild Exceptions when they want to return something that doesn't fit into the simple return datatype they chose, when they better just should have modeled things differently. It's a similar thing with Attributes. Take for example the [MaxLength(x)] Attributes for property meta data, or the [DefaultValue("foo")] one who is my special friend because he is not localizable. Jan 18, 2010 at 20:01
  • 1
    These things would belong into dedicated Type meta data classes and not into attributes. What if I want to initialize these things by a config file or from a database? I had to fiddle around with the TypeDescriptor and PropertyDescriptor stuff to make things work, and it felt very dirty. All in all, almost everything that can be expressed with Attributes can also be modeled differently, be it by the means of better support classes or a support for configuration. Also for post processing for example I would find a Marker Interface approach cleaner. Jan 18, 2010 at 20:01
  • 2
    Then again, anything that can be done with classes can be modeled differently. The point is they can make expressing some stuff significantly easier. Jan 18, 2010 at 20:23
3

From a coding point of view, I think I prefer the Marker Interface syntax because of the built in keywords as and is. Attribute marking requires a little bit more code.

[MarkedByAttribute]
public class MarkedClass : IMarkByInterface
{
}

public class MarkedByAttributeAttribute : Attribute
{
}

public interface IMarkByInterface
{
}

public static class AttributeExtension
{
    public static bool HasAttibute<T>(this object obj)
    {
        var hasAttribute = Attribute.GetCustomAttribute(obj.GetType(), typeof(T));
        return hasAttribute != null;
    }
}

And some tests to use the code:

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class ClassMarkingTests
{
    private MarkedClass _markedClass;

    [TestInitialize]
    public void Init()
    {
        _markedClass = new MarkedClass();
    }

    [TestMethod]
    public void TestClassAttributeMarking()
    {
        var hasMarkerAttribute = _markedClass.HasAttibute<MarkedByAttributeAttribute>();
        Assert.IsTrue(hasMarkerAttribute);
    }

    [TestMethod]
    public void TestClassInterfaceMarking()
    {
        var hasMarkerInterface = _markedClass as IMarkByInterface;
        Assert.IsTrue(hasMarkerInterface != null);            
    }
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.