Click here to Skip to main content
12,246,735 members (58,612 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

19.5K views
178 downloads
12 bookmarked
Posted

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

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)

Share

About the Author

StefanPapp
Software Developer (Senior) Teradata
Austria Austria
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionType of vacationPhoto Pin
Jono Stewart10-May-11 14:35
memberJono Stewart10-May-11 14:35 
AnswerRe: Type of vacationPhoto Pin
swenig19-May-11 2:19
memberswenig19-May-11 2:19 
Generalvs Attached properties? Pin
Bill Seddon9-May-11 15:14
memberBill Seddon9-May-11 15:14 
GeneralRe: vs Attached properties? Pin
swenig19-May-11 2:22
memberswenig19-May-11 2:22 
GeneralRe: vs Attached properties? Pin
Bill Seddon19-May-11 2:35
memberBill Seddon19-May-11 2:35 
GeneralRe: vs Attached properties? Pin
swenig19-May-11 2:49
memberswenig19-May-11 2:49 
GeneralRe: vs Attached properties? Pin
Bill Seddon19-May-11 3:00
memberBill Seddon19-May-11 3:00 
GeneralRe: vs Attached properties? Pin
swenig19-May-11 3:03
memberswenig19-May-11 3:03 
GeneralMy vote of 5 Pin
Rick Justice9-May-11 13:38
memberRick Justice9-May-11 13:38 
GeneralMy vote of 5 Pin
Seth Morris9-May-11 13:03
memberSeth Morris9-May-11 13:03 
GeneralMixin is a "concept" particularly loved in the Ruby / Ruby-on-Rails [ROR] world Pin
Jason Vogel9-May-11 8:29
memberJason Vogel9-May-11 8:29 
GeneralMy vote of 5 Pin
Member 38483557-May-11 10:31
memberMember 38483557-May-11 10:31 
General+ 5 Pin
Member 4558667-May-11 8:31
memberMember 4558667-May-11 8:31 
GeneralRe: + 5 Pin
StefanPapp8-May-11 21:30
memberStefanPapp8-May-11 21:30 
GeneralMy vote of 4 Pin
Michaelgor7-May-11 5:48
memberMichaelgor7-May-11 5:48 
GeneralRe: My vote of 4 Pin
swenig19-May-11 2:25
memberswenig19-May-11 2:25 
GeneralMy vote of 2 Pin
TL Wallace7-May-11 5:10
memberTL Wallace7-May-11 5:10 
GeneralRe: My vote of 2 Pin
StefanPapp8-May-11 21:18
memberStefanPapp8-May-11 21:18 
GeneralAgreed... Pin
Dave Kreskowiak7-May-11 5:07
mvpDave Kreskowiak7-May-11 5:07 
GeneralRe: Agreed... Pin
StefanPapp8-May-11 21:20
memberStefanPapp8-May-11 21:20 
NewsImprovement ideas... Pin
Super Lloyd7-May-11 4:56
memberSuper Lloyd7-May-11 4:56 
GeneralRe: Improvement ideas... Pin
StefanPapp8-May-11 21:20
memberStefanPapp8-May-11 21:20 
GeneralNot what i thought it was going to be about Pin
DaveAuld7-May-11 3:56
memberDaveAuld7-May-11 3:56 
GeneralRe: Not what i thought it was going to be about Pin
PIEBALDconsult7-May-11 5:03
memberPIEBALDconsult7-May-11 5:03 
GeneralRe: Not what i thought it was going to be about Pin
StefanPapp8-May-11 21:25
memberStefanPapp8-May-11 21: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 7 May 2011
Article Copyright 2011 by StefanPapp
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid