Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to present the concept of mixins and their implementation in .NET.

The way they work in .NET is you add extension methods on an interface and decorate classes with said interface.

What would be the best examples for this pattern (other than linq)?

share|improve this question

closed as primarily opinion-based by Martijn Pieters, GlenH7, amon, MichaelT, Dynamic May 11 '14 at 15:38

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

Just to clarify are you looking for the abstract base class sample or is it something else? Term mixin seems to be unfamiliar at least in .NET. – oleksii Feb 19 '12 at 10:34
No, not abstract base classes. Mixins allow for a form of multiple inheritance. Say you have a bat. It's a specialization of the mammal class. But it can also fly. A mixin would allow you to implement the flight logic in one single place, and decorate all the classes you want (e.g. bats, owles, bugs, etc.) – linkerro Feb 19 '12 at 12:37
A mixin is a class that is parameterized by its superclass. I don't really see what extension methods have to do with that. Can you explain? – Jörg W Mittag Feb 19 '12 at 14:51
There are various ways of implementing the mixin pattern, and it takes different forms in different programming languages (the pattern being very loosely defined). Basically it's a way of adding utility code to a class by sidestepping "normal" inheritance, since you need that for something else entirely. – linkerro Feb 20 '12 at 7:09
up vote 4 down vote accepted

As far as I know, "mixin" is a Ruby concept for handling multiple inheritance limitations, and does not have a built-in comparable way of doing it in C#, VB.NET, or Java.

Your suggestion of adding extension methods to an interface is an interesting one, and would provide much of the benefit of a mixin. I've never actually seen anyone do it that way before, so I'll have to put that in my bag of tricks...

As far as an example, I think the one you put in your comment above isn't a bad one; demonstrating that a Bat is a mammal that can fly. Without making your own contrived example like that, I can't think of anyplace where this just "occurs" in any of the core .NET libraries, other than Linq (sort of, though even IEnumerable requires that you define the GetEnumerator method in the implementing class, so it isn't a purely "empty" interface with only extension methods.)

Is there a problem with presenting your own contrived example?

share|improve this answer
"mixin" is a pretty generic concept which isn't specific to any modern language. – DeadMG Feb 19 '12 at 16:19
Agreed the concept of a mixin isn't language specific. I just assumed (possibly falsely) that the original question was talking about "ruby style mixins" since it is the most "modern" language that i know of that supports them. – rally25rs Feb 19 '12 at 16:29
Any language with both type polymorphism and inheritance supports mixins- even Java. It's been found in C++ since 1995 at least. – DeadMG Feb 20 '12 at 6:42
I hope those are scare quotes! Modern isn't what comes to mind when talking about a language that isn't just dynamically typed, but also allows any type to be monkey-patched. – Doval May 9 '14 at 13:42
@Doval I can see why you don't like those features, but I can't imagine it's because they're not "modern". Dynamic typing (as implemented in Ruby) and monkey patching aren't really what come to mind when I think of "old" languages. – KChaloux May 9 '14 at 14:28

Don't know if it's the best example, but I've got an example of using extension methods to approximate mixins on my blog:

Please note that you don't really get mixins by doing this, just something that sort of like mixins from the client codes perspective.

share|improve this answer
"Please note that you don't really get mixins by doing this, just something that sort of like mixins from the client codes perspective." What's the difference, and perhaps more importantly, what's the definition of mixins you're using? Everyone seems to have a different one. – Doval May 9 '14 at 13:53
@Doval: I would imagine that the difference is that a true mixin will have access to a class's internals. Extension methods do not. Extension methods are just syntactic sugar for static method calls, so all they have access to is a class's external API. – Robert Harvey May 9 '14 at 14:13
@RobertHarvey Interesting. When you say "a class's internals", do you mean something internal to the mixin, or the internals of the class being decorated? – Doval May 9 '14 at 14:16
@Doval: Extension methods won't have access to anything that a class's public methods don't already provide. – Robert Harvey May 9 '14 at 14:19
@Doval: I guess what I'm trying to say is that anything you can do with an extension method, you can already do with the public API of an ordinary class anyway, albeit with different syntax. Not directly answering your question, but without access to class internals I see any interaction between classes via the external API as ordinary composition, unless I'm missing something blindingly obvious about mixins. – Robert Harvey May 9 '14 at 14:25

I've been working on an open source Mixin framework for C# pMixins. It leverages partial classes and code generators to wire in the Mixin class into the Target:

//Mixin - Class that contains members that should be injected into other classes.
public class Mixin
   // This method should be in several class
   public void Method(){ }

//Target (Note: That it is partial) - Add members from Mixin
[pMixn(Target = typeof(Mixin)]
public partial class Target{}

//Example of using Target
public class Consumer
    public void Example()
        var target = new Target();

        // can call mixed in method

        // can implicitly convert Target to Mixin
        Mixin m = new Target();
share|improve this answer
Most examples of mixins that I've seen are either of the "Fooing the Bar" variety like this one, or involve animal taxonomies, neither of which adequately illustrates how mixins might be useful in a practical coding scenario. – Robert Harvey May 9 '14 at 15:12
@RobertHarver - Do you have an example of a practical coding scenario? – ppittle May 9 '14 at 15:15

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