Click here to Skip to main content
Licence CPOL
First Posted 7 May 2011
Views 8,389
Downloads 118
Bookmarked 12 times

re-mix - A Mixin Library for .NET

By | 7 May 2011 | Article
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:

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.

License

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

About the Author

StefanPapp

Other
Freelancer
Austria Austria

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionType of vacationPhoto PinmemberJono Stewart13:35 10 May '11  
AnswerRe: Type of vacationPhoto Pinmemberswenig1:19 19 May '11  
Generalvs Attached properties? PinmemberBill Seddon14:14 9 May '11  
GeneralRe: vs Attached properties? Pinmemberswenig1:22 19 May '11  
GeneralRe: vs Attached properties? PinmemberBill Seddon1:35 19 May '11  
GeneralRe: vs Attached properties? Pinmemberswenig1:49 19 May '11  
GeneralRe: vs Attached properties? PinmemberBill Seddon2:00 19 May '11  
GeneralRe: vs Attached properties? Pinmemberswenig2:03 19 May '11  
GeneralMy vote of 5 PinmemberRick Justice12:38 9 May '11  
GeneralMy vote of 5 PinmemberSeth Morris12:03 9 May '11  
GeneralMixin is a "concept" particularly loved in the Ruby / Ruby-on-Rails [ROR] world PinmemberJason Vogel7:29 9 May '11  
GeneralMy vote of 5 PinmemberMember 38483559:31 7 May '11  
General+ 5 PinmemberMember 4558667:31 7 May '11  
GeneralRe: + 5 PinmemberStefanPapp20:30 8 May '11  
GeneralMy vote of 4 PinmemberMichaelgor4:48 7 May '11  
GeneralRe: My vote of 4 Pinmemberswenig1:25 19 May '11  
GeneralMy vote of 2 PinmemberTL Wallace4:10 7 May '11  
GeneralRe: My vote of 2 PinmemberStefanPapp20:18 8 May '11  
GeneralAgreed... PinmvpDave Kreskowiak4:07 7 May '11  
GeneralRe: Agreed... PinmemberStefanPapp20:20 8 May '11  
NewsImprovement ideas... PinmemberSuper Lloyd3:56 7 May '11  
GeneralRe: Improvement ideas... PinmemberStefanPapp20:20 8 May '11  
GeneralNot what i thought it was going to be about PinmemberDaveAuld2:56 7 May '11  
GeneralRe: Not what i thought it was going to be about PinmemberPIEBALDconsult4:03 7 May '11  
GeneralRe: Not what i thought it was going to be about PinmemberStefanPapp20:25 8 May '11  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 7 May 2011
Article Copyright 2011 by StefanPapp
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid