Click here to Skip to main content
Click here to Skip to main content

Tagged as

re-mix - A Mixin Library for .NET

, 7 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to implement mixins with C#.


Mixins are a powerful concept to improve OOP structures. Unfortunately, mixins are not part of the .NET Framework.

In the article Mixin in C# 3, the author Ayende Rahien presented his ideas to implement mixins with C# 3.0. His use case “adding tags to objects” is a perfect reference sample for mixins.

In this article, we rewrite this sample, because Ayende Rahien's article was just about a concept, he did not implement the sample.

We will also use a different technology. Extension Methods are syntactic sugar for static methods and they lack two important functionalities required for mixins: they cannot store states to objects and they cannot be part of an interface. Ayende Rahien referred to using Castle.DynamicProxy as a possible solution to that problem.

In this article, we will use the re-mix library to implement the sample. re-mix is a library specially designed to bring mixins to .NET. It provides a rich set of additional functionality for mixins. re-mix can be downloaded from its CodePlex page.

Using the Code

Let’s jump right into the code:

public interface ITaggable
    void TagWith(string[] tags);
    string AllTags ();

public class TaggableMixin : ITaggable
    private readonly List<string> _tags = new List<string>();

    public void TagWith(string[] tags)
        _tags.AddRange (tags);

    public string AllTags()
        return _tags.Aggregate(string.Empty, (current, s) => current + “ “ + s);

This code shall be mixed into a target class. As you can see, this is pure standard C# code.

We will now see how the class TaggableMixin can be “turned into a mixin”. Let’s have a look at a reference implementation that uses TaggableMixin:

[Uses (typeof (TaggableMixin))]
public class Photo
    public string Name { get; set; }
var vacationPhoto = ObjectFactory.Create<Photo>();
vacationPhoto.Name = "HolidayPic1";
ITaggable t = ((ITaggable) vacationPhoto);
t.TagWith(new[] {"Holiday", "Private", "Sunset"});
Console.WriteLine("Photo {0} is tagged with {1}", 
                  vacationPhoto.Name, t.AllTags());

This is the first time in the source code that we are using the re-mix library. The “target class” (i.e., the class that should be extended with the mixin’s functionality) gets annotated by a Uses attribute which is provided by re-mix. This code tells the mixin engine that the target class uses TaggableMixin.

We also have to tell the mixin engine that we want to instantiate a “mixed object” of Photo and its mixins. For this we have to call the ObjectFactory.Create method. If we use the standard constructor, we would create a POCO (plain old CLR object) photo object without any mixins.

Once the mixed object is instantiated, the last remaining issue is to access the mixin. For this, we have to cast the mixed object to its mixin interface. Then we are able to access every functionality provided by the mixin.

Points of Interest

This article is an appetizer for more. Among other things the following is possible with re-mix:

  • You can configure mixins declaratively by configuration:
    • A class uses a mixin
    • A mixin extends a class or class tree
    • An assembly connects classes with mixins
  • Add/remove mixins dynamically to/from the class
  • Use patterns to access mixin functionality without casts
  • Override methods of the target class in a mixin
  • Override mixin methods in the target class

It is out of the scope to explain these features in an introductory article. I recommend downloading the library from the CodePlex page. Try out some examples from the hands on lab! If you get stuck or run into problems, don't hesitate to ask the developers on the mailing list at this Google page.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Other Freelancer
Austria Austria
No Biography provided

Comments and Discussions

QuestionType of vacationPhoto PinmemberJono Stewart10-May-11 13:35 
AnswerRe: Type of vacationPhoto Pinmemberswenig19-May-11 1:19 
Generalvs Attached properties? PinmemberBill Seddon9-May-11 14:14 
GeneralRe: vs Attached properties? Pinmemberswenig19-May-11 1:22 
GeneralRe: vs Attached properties? PinmemberBill Seddon19-May-11 1:35 
GeneralRe: vs Attached properties? Pinmemberswenig19-May-11 1:49 
GeneralRe: vs Attached properties? PinmemberBill Seddon19-May-11 2:00 
GeneralRe: vs Attached properties? Pinmemberswenig19-May-11 2:03 
GeneralMy vote of 5 PinmemberRick Justice9-May-11 12:38 
GeneralMy vote of 5 PinmemberSeth Morris9-May-11 12:03 
GeneralMixin is a "concept" particularly loved in the Ruby / Ruby-on-Rails [ROR] world PinmemberJason Vogel9-May-11 7:29 
GeneralMy vote of 5 PinmemberMember 38483557-May-11 9:31 
General+ 5 PinmemberMember 4558667-May-11 7:31 
GeneralRe: + 5 PinmemberStefanPapp8-May-11 20:30 
GeneralMy vote of 4 PinmemberMichaelgor7-May-11 4:48 
GeneralRe: My vote of 4 Pinmemberswenig19-May-11 1:25 
GeneralMy vote of 2 PinmemberTL Wallace7-May-11 4:10 
GeneralRe: My vote of 2 PinmemberStefanPapp8-May-11 20:18 
GeneralAgreed... PinmvpDave Kreskowiak7-May-11 4:07 
GeneralRe: Agreed... PinmemberStefanPapp8-May-11 20:20 
NewsImprovement ideas... PinmemberSuper Lloyd7-May-11 3:56 
GeneralRe: Improvement ideas... PinmemberStefanPapp8-May-11 20:20 
GeneralNot what i thought it was going to be about PinmemberDaveAuld7-May-11 2:56 
GeneralRe: Not what i thought it was going to be about PinmemberPIEBALDconsult7-May-11 4:03 
GeneralRe: Not what i thought it was going to be about PinmemberStefanPapp8-May-11 20:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 7 May 2011
Article Copyright 2011 by StefanPapp
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid