Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Article

re-mix - A Mixin Library for .NET

Rate me:
Please Sign up or sign in to vote.
3.90/5 (8 votes)
7 May 2011CPOL3 min read 35.7K   195   12   25
How to implement mixins with C#.

Introduction

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:

C#
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:

C#
[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.

License

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


Written By
Software Developer (Senior) Teradata
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionType of vacationPhoto Pin
Jono Stewart10-May-11 13:35
Jono Stewart10-May-11 13:35 
AnswerRe: Type of vacationPhoto Pin
swenig19-May-11 1:19
swenig19-May-11 1:19 
Generalvs Attached properties? Pin
Bill Seddon9-May-11 14:14
Bill Seddon9-May-11 14:14 
GeneralRe: vs Attached properties? Pin
swenig19-May-11 1:22
swenig19-May-11 1:22 
GeneralRe: vs Attached properties? Pin
Bill Seddon19-May-11 1:35
Bill Seddon19-May-11 1:35 
GeneralRe: vs Attached properties? Pin
swenig19-May-11 1:49
swenig19-May-11 1:49 
GeneralRe: vs Attached properties? Pin
Bill Seddon19-May-11 2:00
Bill Seddon19-May-11 2:00 
GeneralRe: vs Attached properties? Pin
swenig19-May-11 2:03
swenig19-May-11 2:03 
GeneralMy vote of 5 Pin
Rick Justice9-May-11 12:38
Rick Justice9-May-11 12:38 
GeneralMy vote of 5 Pin
Seth Morris9-May-11 12:03
Seth Morris9-May-11 12:03 
GeneralMixin is a "concept" particularly loved in the Ruby / Ruby-on-Rails [ROR] world Pin
Jason Vogel9-May-11 7:29
Jason Vogel9-May-11 7:29 
GeneralMy vote of 5 Pin
Member 38483557-May-11 9:31
Member 38483557-May-11 9:31 
General+ 5 Pin
RAND 4558667-May-11 7:31
RAND 4558667-May-11 7:31 
GeneralRe: + 5 Pin
StefanPapp8-May-11 20:30
StefanPapp8-May-11 20:30 
GeneralMy vote of 4 Pin
Michaelgor7-May-11 4:48
Michaelgor7-May-11 4:48 
GeneralRe: My vote of 4 Pin
swenig19-May-11 1:25
swenig19-May-11 1:25 
GeneralMy vote of 2 Pin
Terence Wallace7-May-11 4:10
Terence Wallace7-May-11 4:10 
GeneralRe: My vote of 2 Pin
StefanPapp8-May-11 20:18
StefanPapp8-May-11 20:18 
GeneralAgreed... Pin
Dave Kreskowiak7-May-11 4:07
mveDave Kreskowiak7-May-11 4:07 
GeneralRe: Agreed... Pin
StefanPapp8-May-11 20:20
StefanPapp8-May-11 20:20 
NewsImprovement ideas... Pin
Super Lloyd7-May-11 3:56
Super Lloyd7-May-11 3:56 
GeneralRe: Improvement ideas... Pin
StefanPapp8-May-11 20:20
StefanPapp8-May-11 20:20 
GeneralNot what i thought it was going to be about Pin
DaveAuld7-May-11 2:56
professionalDaveAuld7-May-11 2:56 
GeneralRe: Not what i thought it was going to be about Pin
PIEBALDconsult7-May-11 4:03
mvePIEBALDconsult7-May-11 4:03 
GeneralRe: Not what i thought it was going to be about Pin
StefanPapp8-May-11 20:25
StefanPapp8-May-11 20:25 

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

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